Hello :)

I don't know if someone has replied to you directly but this is what Tal 
Expression Modifiers are for:
http://phptal.org/manual/en/split/custom-modifiers.html

Understanding and writing them can be a little tricky as the documentation is 
not extensive but the key points to remember are:
1) Your modifier function must output a text string that will be executed as 
php at a later date - not the actual output you expect to see in the template
2) You can request parsing of modifier chunks using phptal_tales() and request 
returning of a single value (e.g. the value of a variable or string statement) 
using phptal_tale()
3) In my experience, you need to handle chaining yourself by looking for the 
'|' symbol, splitting the input string and (if appropriate), using 
phptal_tales() to handle the bits after the '|'

in your case, you'd prob. want to have a function called phptal_tales_lorem 
which is used as <p tal:content="lorem:string:100" /> that would output a 
string of php that will generate 100 chars of text.

The following is *untested* :)

function phptal_tales_lorem($src, $nothrow)
{
        $break = strpos($src, '|');
        if ($break !== false) {
                $src = substr($src, 0, $break);
        }
        return 'substr(str_repeat("Hello World",ceil(' . phptal_tale($src, 
$nothrow) . '/11)),0,' . phptal_tale($src, $nothrow) . ')';
}

This code strips anything in the src string after the '|' if it exists (as we 
don't want to chain) then returns a string of php that will repeat 'Hello 
World' enough times to generate at least the required number of chars before 
then substringing this to get the exact number of chars requested. It uses 
phptal_tale to process the $src string as this could be a 'string:100' or a 
'php:...' or a tal variable. This means you can be flexible in the length of 
placeholder text generated at runtime. The alternative would be to just use the 
$src string directly but then you would be forcing the user to specify only an 
int at template design time.

If your php string gets too complex remember you can always just write a 
function in php and call it from the php string returned by your tales modifier.

Robert

On 4 Apr 2010, at 18:53, Cameron Manderson wrote:

> Hi, 
> 
> Could phptal support a series of namespaced nodes/attributes that trigger 
> code?
> 
> e.g. <php:echo text="Hello World" /> or <example:lorem length="100" /> 
> triggering off php code to convert into a processor command of <?php echo 
> "Hello World"; ?>
> 
> I understand that it might not directly be the purpose of this library, but I 
> want to know whether this sort of extension would be easy to implement. 
> 
> What would be a starting point to implementing this sort of functionality?
> 
> Thanks,
> Cameron
> _______________________________________________
> PHPTAL mailing list
> PHPTAL@lists.motion-twin.com
> http://lists.motion-twin.com/mailman/listinfo/phptal


_______________________________________________
PHPTAL mailing list
PHPTAL@lists.motion-twin.com
http://lists.motion-twin.com/mailman/listinfo/phptal

Reply via email to