If I use a variable that hasn't been put into the scope, PHPTAL throws an exception. Is there any way of making PHPTAL fall back to graceful defaults, for example evaluating to false in a boolean context, to a blank in a string context, etc?
Asked
Active
Viewed 1,081 times
2 Answers
5
You can add "| nothing
" to TALES expressions or use isset()
in php:
expressions.
<p tal:attributes="class php:isset(class)?class:NULL"
tal:content="variable | nothing" />
If you have larger bit of code that relies on a certain variable, then use exists:
modifier:
<div tal:condition="exists:variable">
…
</div>
If you want to fake existence of any variable, it can be done, but I don't recommend it (it will hide typos):
class FakeAll extends stdClass
{
function __get($var){return "";}
function __isset($var){return true;}
}
$p = new PHPTAL();
$p->getContext()->setGlobal(new FakeAll());

Kornel
- 97,764
- 37
- 219
- 309
0
also shorter version:
<input type="text" name="id" value="${data/formvalues/id|nothing}" />

Michal - wereda-net
- 940
- 9
- 14