Welcome to Geeklog, Anonymous Saturday, December 21 2024 @ 07:35 am EST
Geeklog Forums
Closures in caching Template
Status: offline
remy
Forum User
Full Member
Registered: 06/09/03
Posts: 162
Location:Rotterdam & Bonn
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:
<tr><td>{lang_username}:</td><td>{username}</td></tr>
// becomes
<tr><td>{$LANG_USER[name]}:</td><td>{username}</td></tr>
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:
Advanced actions are closed by standard {!endaction} constructs.
Complex conditions can also contain calls to any global function:
{!if COM_isAnonUser({uid}) !}
The parameter to the function is even a templated variable. Very good.
But further below in the wiki doc (in the table), it is documented that advanced actions are coded like
{!!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.
$tpl->set_var('foo', $foo);
Assuming this would work, would this output 'hello'?
i.e. {!!echo {foo} !!}
i.e. {!!echo {$foo} !!}
i.e. {!!echo $foo !!}
An alternative is, of course, to put the code between <?php and ?>.
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:
Text 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>
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:
Text 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}) !}
The parameter to the function is even a templated variable. Very good.
But further below in the wiki doc (in the table), it is documented that advanced actions are coded like
Text 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.
Text Formatted Code
$foo = function() { return 'hello'; };$tpl->set_var('foo', $foo);
Assuming this would work, would this output 'hello'?
Text 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 ?>.
16
14
Quote
Status: offline
remy
Forum User
Full Member
Registered: 06/09/03
Posts: 162
Location:Rotterdam & Bonn
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.
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.
10
15
Quote
Status: offline
remy
Forum User
Full Member
Registered: 06/09/03
Posts: 162
Location:Rotterdam & Bonn
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.
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.
12
14
Quote
All times are EST. The time is now 07:35 am.
- Normal Topic
- Sticky Topic
- Locked Topic
- New Post
- Sticky Topic W/ New Post
- Locked Topic W/ New Post
- View Anonymous Posts
- Able to post
- Filtered HTML Allowed
- Censored Content