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:

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

Attachment: nullAttrs.patch
Description: Binary data

PHPTAL mailing list

Reply via email to