[PHPTAL] Array Syntax Question

Werner lists at mollentze.co.za
Thu Feb 15 21:01:22 CET 2007


Hi again -

Unfortunately none of your suggestions worked. I decided to write a 
modifier to do exactly what I wanted, mybe there is still a native way 
to do this, but I ran out of time.

For those of you who might need such functionality one day, here it is:

---

/**
 * arr Expression Modifier
 *
 * A modifier that allows PHPTAL to retrieve the values of template
 * array variables by using the value of other template variables as
 * keys.
 *
 * For example, you can use template code like this:
 *    arr:my_array[repeat/item/index]
 *
 * @param string $src
 *     The expression string
 * @param string $nothrow
 *     A boolean indicating if exceptions may be throw by phptal_path if
 *     the path does not exists.
 * @return string
 *     PHP code to include in the template
 *
 */

function phptal_tales_arr($src, $nothrow)
{
    $regex = '|^([a-zA-Z_]+[a-zA-Z0-9_]*)\[([^\]]+)\]$|';
    $matches = array();
    if (preg_match($regex, $src, $matches)) {
        return phptal_tales($matches[1], $nothrow) . '[' . 
phptal_tales($matches[2], $nothrow) . ']';
    } else {
        return phptal_tales($src, $nothrow);
    }
}

---

Kind Regards,
Werner




Werner wrote:
> Hi!
>
> Please tell me what the correct syntax is to do this:
>    tal:attributes="class myarray[repeat/item/index]"
>
> I'm trying to set the class attribute to the value of the same key in 
> the "myarray" array as the current key of the "item" array.
>
> Hope it makes sense, it's actually quite simple to grasp, but 
> seemingly hard to express in a template's code.
>
> Kind regards,
> Werner
>
> _______________________________________________
> PHPTAL mailing list
> PHPTAL at lists.motion-twin.com
> http://lists.motion-twin.com/mailman/listinfo/phptal
>




More information about the PHPTAL mailing list