Welcome to Geeklog, Anonymous Saturday, November 23 2024 @ 11:47 am EST

Geeklog Forums

caching templates and php


Status: offline

remy

Forum User
Full Member
Registered: 06/09/03
Posts: 162
Location:Rotterdam & Bonn
I would like to use php in a few templates.
The wiki is clear about how to do that.

I need this to explode a template variable and loop around it to generate text.
I wonder what the scope is in the included php-code:

--is $this->setvar() available in php?
--how to process a template variable in php, like $myResult=explode({myTemplateVar)}
--are the variables created in php available in the template, like {myResult[0]}..and on
--or is {myResult} available after the php code executed?
 Quote

Status: offline

Laugh

Site Admin
Admin
Registered: 09/27/05
Posts: 1470
Location:Canada
You have access to the template variables like so (anonymous_user template variable is available to all templates):
Text Formatted Code

        <ul>
          {!if anonymous_user}
            <li class="menuitem"><a href="/users.php" title="{$LANG01[58]}">{$LANG01[58]}</a></li>
          {!else}
            <li class="menuitem"><a href="/users.php?mode=logout" title="{$LANG01[35]}">{$LANG01[35]}</a></li>
            <li class="menuitem"><a href="/usersettings.php" title="{$LANG01[48]}">{$LANG01[48]}</a></li>
          {!endif}        
          {menu_elements}
        </ul>
 


Another way to do this in just php would be by calling a function like so:

Text Formatted Code

          <?php
          if (COM_isAnonUser()) {
              echo '<li class="menuitem"><a href="/users.php" title=" Login">Login</a></li>';
          } else {
              echo '<li class="menuitem"><a href="/usersettings.php" title="My Account">My Account</a></li>';
          }
          ?>
 


These 2 examples are meant to be used in the header with the menu items. The first example is the recommended way to do it.

There are ways to change template variables. Take a look at "Other Actions" in the wiki.

As far as using the setvar function I am assuming you want to access the current variables of the template you are in. The best way you probably can do this is by checking out the "Other Actions" in the wiki. You would have to test to see if $this->setvar() does work in this case as I am not sure.

One of the Geeklog Core Developers.
 Quote

Status: offline

remy

Forum User
Full Member
Registered: 06/09/03
Posts: 162
Location:Rotterdam & Bonn
The proposed trial-and-error method to find out what is possible is not so smart. When I detect in such way a behavior that I can use, I will not be sure if this behavior is a feature or based on a bug. When a new release is coming out, I would be f**cked.

But anyway. The following works (with an attitude):

In my template:
Text Formatted Code

<?php $myVar = explode(" ", $this->get_var('myTemplateVar');
          $this->set_var($myVar); ?>
{0} / {1} / {2}
 


When myTemplateVar="A B C", then the output is "//B/C"
So, it works, but the first element disappears.
 Quote

Status: offline

remy

Forum User
Full Member
Registered: 06/09/03
Posts: 162
Location:Rotterdam & Bonn
And the following is something I really like, and looks like what I was after. Not sure if this "feature" will last forever, though.

In the plugin module a variable is defined, say $myPluginData=array();
Than, in the top template some value is taken from a templateVariable like

Text Formatted Code
<?php global $myPluginData;
$myPluginData['myvar']=$this->get_var('myTopTemplateVar');
 ?>


Than, in the dependent template that is pulled in like:

Text Formatted Code
<?php global $myPluginData;
$this->set_var('myDetailTemplateVar', $myPluginData['myvar']);
 ?>


This comes out very handy in a list design when you need a value from the header to use in a calculation or selection in the detail lines. It is even possible this way to cumulate a column of the list into a sum in the footer.

Monkey Pretty good feature and nice template engine, indeed.
 Quote

Status: offline

Laugh

Site Admin
Admin
Registered: 09/27/05
Posts: 1470
Location:Canada
The proposed trial-and-error method to find out what is possible is not so smart. When I detect in such way a behavior that I can use, I will not be sure if this behavior is a feature or based on a bug. When a new release is coming out, I would be f**cked.


Well I didn't code the template cache (I integrated it into Geeklog) and I haven't tried absolutely everything with it. The reason I said you needed to try it was while I thought it would work I hadn't actually done it before so I didn't want to promise anything. Php in a template file will be able to access anything that the template class at that time has access to.

From your code examples it looks like you are using the standard methods to access the template variables, etc. These function may get added to but the core functionality will not change (unless we give plenty of advance notice not that I see this happening unless we ever completely change the theme system) since this is how Geekog and all it's plugins access the template class.
One of the Geeklog Core Developers.
 Quote

Status: offline

remy

Forum User
Full Member
Registered: 06/09/03
Posts: 162
Location:Rotterdam & Bonn
Laugh, it is not personal.
Quote by: Laugh

Php in a template file will be able to access anything that the template class at that time has access to.


Well, this is an answer to my question. Bit late, but nevertheless a decent statement. Add it to the docs !
Quote by: Laugh

From your code examples it looks like you are using the standard methods to access the template variables, etc.


Hmm, and this gives me fear again. I tried and tried, error-ed and error-ed, and the "standard" seems to work, assuming the cache is a template class instance. Obviously there are other methods than the "standard". Beats me.
 Quote

Status: offline

Laugh

Site Admin
Admin
Registered: 09/27/05
Posts: 1470
Location:Canada
By standard I mean the api. Nothing to fear.
One of the Geeklog Core Developers.
 Quote

Status: offline

Laugh

Site Admin
Admin
Registered: 09/27/05
Posts: 1470
Location:Canada
This has now been fixed. !empty was used to check for varnames in the template class which removes all "", nulls and 0s. I have added a check to allow for 0's to be included as variable names. In a template I ran the following code and I was able to set, unset and clear template variables when they were named 0

Text Formatted Code

<?php
$myVar = explode(" ", "A B C");
$this->set_var($myVar);
$this->set_var("test", "fun");
?>
<br>set_var: {0} / {1} / {2} / {test} <br />
<?php
$myVar = explode(" ", "0 1 2 test");
$this->clear_var($myVar);
?>

<br>clear_var: {0} / {1} / {2} / {test} <br />          
<?php
$myVar = explode(" ", "A B C");
$this->set_var($myVar);
$this->set_var("test", "fun");
?>
<br>set_var: {0} / {1} / {2} / {test} <br />
<?php
$myVar = explode(" ", "0 1 2 test");
$this->unset_var($myVar);
?>

<br>unset_var: {0} / {1} / {2} / {test} <br />

One of the Geeklog Core Developers.
 Quote

All times are EST. The time is now 11:47 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