[PHPTAL] Imploding tal:attributesfrom source array
Kornel Lesinski
kornel at aardvarkmedia.co.uk
Tue Jan 29 12:43:36 CET 2008
On 29 Jan 2008, at 07:10, Werner wrote:
> 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}" />
This particular construct doesn't work, because conents of
tal:attributes is parsed (analyzed) and compiled only once.
It is possible to extend TAL/PHPTAL to support variable number of
attributes, just nobody did that yet :)
> 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...?
Nope, AFAIK that's the TAL's "right" way:
<img tal:attributes="src imgAttr/src | nothing; class imAttr/class |
nothing; etc..." />
You can put it in a macro to avoid repeating it.
I've attached a patch (apply with: cd /your/phptal/dir/; patch -p0 < /
path/to/nullAttrs.patch) that makes 'nothing' keyword omit attribute
entirely (so you won't get class="" if image has no class name). I'll
make it part of official distribution later.
Alternatively you can construct entire tag using PHP:
${structure php:makeImgHTML(imgAttr)}
--
regards, Kornel
-------------- next part --------------
A non-text attachment was scrubbed...
Name: nullAttrs.patch
Type: application/octet-stream
Size: 5855 bytes
Desc: not available
Url : http://lists.motion-twin.com/pipermail/phptal/attachments/20080129/bb6f7f2e/nullAttrs.obj
More information about the PHPTAL
mailing list