[PHPTAL] Imploding tal:attributesfrom source array

Werner lists at mollentze.co.za
Tue Jan 29 08:10:03 CET 2008


Hi Anton

Unfortunately this does not seem to work. It seems that there needs to 
be at least one attribute key to be specified before the attribute's 
value will be rendered.

E.g. if you use:
    <img tal:attributes="${imgAttr}" />
You get:
    <img/>

I ony can get something rendered if I specify an attribute name:
    <img tal:attributes="myattrs ${imgAttr}" />
You get:
    <img myattrs=" id some_id; class some_class; src img/beautiful.jpg;"/>

Why is this so hard? One would almost expect tal:attributes to be able 
to handle any assoc array right there in place? E.g.
    <img tal:attributes="${myAssocArray}" />

I guess this is not the intended way, eh? The only alternative seems to 
be to write a very long tal:attributes definition that would cater for 
any possible attribute, but I;m sure there must be a better approach...?

Kind Regards,
Werner

Anton Andrievskij wrote:
> In your PHP:
>
> *$imgAttr = '';
> foreach($attributes as $attrname => $attrvalue)
>   $imgAttr .= "$attrname $attrvalue;";
>
> $templateObject->imgAttr = $imgAttr;*
>
> In your template:
> *<img tal:attributes="${imgAttr}" />*
>
> Hope this may help.
> Anton Andryevskyy
>
> On Jan 29, 2008 12:30 AM, Werner <lists at mollentze.co.za 
> <mailto:lists at mollentze.co.za>> wrote:
>
>     Hi!
>
>     Has anyone found an easy (and readable) way to do the following by
>     using
>     tal:attributes ?
>
>     1) You have an $attributes array in php:
>            $attributes = array('id' => 'some_id', 'class' => 'some_class',
>     'src' => 'img/beautiful.jpg');
>
>     2) You want to apply all the attributes as an element's attributes:
>            <img id="some_id" class="some_class" src="img/beautiful.jpg"/>
>
>     Kind Regards,
>     Werner
>
>
>     _______________________________________________
>     PHPTAL mailing list
>     PHPTAL at lists.motion-twin.com <mailto:PHPTAL at lists.motion-twin.com>
>     http://lists.motion-twin.com/mailman/listinfo/phptal
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> PHPTAL mailing list
> PHPTAL at lists.motion-twin.com
> http://lists.motion-twin.com/mailman/listinfo/phptal
>   




More information about the PHPTAL mailing list