I like the idea, but I think it is antithetical to TAL. You can defer the logic 
to php code to define a variable (what I strive to do) or use another template 
system that already has this functionality (as XSLT does exactly)
Sent via BlackBerry by AT&T

-----Original Message-----
From: Robert Goldsmith <rgoldsm...@names.co.uk>
Sender: phptal-boun...@lists.motion-twin.com
Date: Fri, 12 Jul 2013 15:35:03 
To: Template Attribute Language for PHP<phptal@lists.motion-twin.com>
Reply-To: Template Attribute Language for PHP <phptal@lists.motion-twin.com>
Subject: [PHPTAL] Conditional attributes

Hello :)

There are many occasions where the decision to set an attribute or what value 
and attribute should have is based on quite complex calculations - such as the 
state of multiple other variables. When you have multiple of these attributes 
you really only have 4 options, none of which are great:
1) Create a lot of defines in order to calculate intermediate values
2) Create lots of tal blocks with conditionals
3) Try to come up with a statement that does what you need through chaining
4) Write a custom tale.

While these just about cope if you have 1 attribute that needs calculating it 
becomes much more complex when you have multiple.

I was thinking that in many of these cases it would be convenient to have a new 
element called something like <tal:attribute />

The new element would have a required name attribute (which could be variable, 
just like normal) and an optional value attribute and would optionally take 
content. Like a tal:block and tal:inline, it would have tal:condition and 
tal:define attributes.

The behaviour of the element would be to apply its calculated content as an 
attribute with the specified name to the first parent object that is output.

Here's a basic example:

<div id="wibble">
        <tal:attribute name="class">ping</tal:attribute>

The result of this would be:
<div id="wibble" class="ping" />

The following would result in the same:
<div id="wibble">
        <tal:attribute name="class" value="string:ping" />

Obviously the simple example is pointless but shows how I'd considered it 
working. You'd be able to have as many tal:attribute nodes as you want (with 
some sort of handling for repeated names).

 By bubbling the assignment of the attributes up to the first parent output 
node you could easily apply complex logic to your attributes and keep then 
self-contained. The work needed to calculate one attribute would all be bundled 
up in the defines and conditionals surrounding it and not mixed in with other 
attributes (unless shared, of course).

What do people think?

Robert Goldsmith

PHPTAL mailing list
PHPTAL mailing list

Reply via email to