Welcome to Geeklog, Anonymous Thursday, April 18 2024 @ 08:49 pm EDT

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:
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>

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:
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}) !}

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
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);
 

Exclamation 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 ?>.
 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.
 Quote

Status: offline

Laugh

Site Admin
Admin
Registered: 09/27/05
Posts: 1468
Location:Canada
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


One of the Geeklog Core Developers.
 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.
 Quote

All times are EDT. The time is now 08:49 pm.

  • 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