Subject: Closures in caching Template

Posted on: 15/01/14 06:09pm
By: remy

I'm searching for some clues on the usage of closures in geekLog Templates.

First, the documentation of what is doable with the caching Template is somewhat obscure. It should be possible to eliminate all language variables in the software:
PHP Formatted Code
// and the template
<tr><td>{lang_username}:</td><td>{username}</td></tr>
// becomes
<tr><td>{$LANG_USER[name]}:</td><td>{username}</td></tr>

Exclamation Than I do assume that {$myVar} also works when it is in global scope. So far so good.
Reading carefully, there is another great feature about global functions:
PHP Formatted Code
So while a simple if might look like this{!if var}, the advanced if can look like this{!if {var} == 'a' || {var} == 'b' !}.
Advanced actions are closed by standard {!endaction} constructs.
Complex conditions can also contain calls to any global function:
{!if COM_isAnonUser({uid}) !}

Exclamation The parameter to the function is even a templated variable. Very good.
Question But further below in the wiki doc (in the table), it is documented that advanced actions are coded like
PHP Formatted Code
Advanced Actions
{!!if condition !!}      The condition can contain any template construct that does not end with !}.


Now, to the quest: I would like to set a templateVariable to contain a anonymous function.
PHP Formatted Code
$foo = function() { return 'hello'; };
$tpl->set_var('foo', $foo);
 

Exclamation Assuming this would work, would this output 'hello'?
PHP Formatted Code
{!!echo condition !!}    Echo's the complex condition to the cached PHP output.
i.e. {!!echo {foo} !!}
i.e. {!!echo {$foo} !!}
i.e. {!!echo $foo !!}

An alternative is, of course, to put the code between <?php and ?>.

Re: Closures in caching Template

Posted on: 18/01/14 11:41am
By: remy

Abandoned my quest because the Template class processes the variable values: it tries to preserve some characters by escaping them. What cannot be done if the value is not text.
Decided to go ahead with a light custom version of the class and it seems to work.

Beside this all, it turns out that the standard Template class restricts php code to the files.
So, a regular template variable can only contain text and php code will not be eval-ed.

Re: Closures in caching Template

Posted on: 18/01/14 12:03pm
By: Laugh

Sorry remy I meant to get back to you.

As you found out php code will not be evaluated in a template variable (this is the same for autotags).

Tom


Re: Closures in caching Template

Posted on: 18/01/14 12:23pm
By: remy

That conclusion would be worth to take into the wikidoc. The current text is ambiguous on this point.

The root of my quest was, btw, a templating need without any file being input.
My tVars come from php and should behave as a macro for the html dialect targeted.
If used that way in the standard Template class, I get stuck with doubled escapeQuotes, as if addslashes() ran twice. My lightweight Templet class does only preg_replacing and leave the advanced syntax of tVars nicely be. The output from the Templet class are fed again as ordinary tVars to the Template class.

Geeklog - Forum
https://www.geeklog.net/forum/viewtopic.php?showtopic=95511