xs2 variables

PHP Variables in Templates

Submitted by CyberAlien, Mar 2007.
This short tutoral explains how to use different php variables inside phpBB templates. You must install eXtreme Styles mod on your forum, otherwise php code won’t work at all.

1. External Variables

Because template is included inside a function, all global variable must be declated with “global”. For example:

Code:
  1. echo $phpbb_root_path; // would show error – no such variable
     

  2.  
  3. global $phpbb_root_path;
     
  4. echo $phpbb_root_path; // would echo phpBB variable $phpbb_root_path 


Example for $_GET/$_POST/$_COOKIE variables when using old full length variables:
Code:
  1. if(isset($HTTP_GET_VARS[‘test’]))
     
  2. {
     
  3.  echo ‘test is set’;
     
  4. }
     
  5. else
     
  6. {
     
  7.  echo ‘test is not set’;
     
  8. } 
Code above will always echo “test is not set”, even if you use ?test=1 in url because array $HTTP_GET_VARS inside function is not the same as global array. Here is how to do it correctly:
Code:
  1. global $HTTP_GET_VARS;
     
  2. if(isset($HTTP_GET_VARS[‘test’]))
     
  3. {
     
  4.  echo ‘test is set’;
     
  5. }
     
  6. else
     
  7. {
     
  8.  echo ‘test is not set’;
     
  9. } 

2. Root Template Variables

There are three types of template variables in phpBB templates: root level variables like {U_LOGIN_LOGOUT}, {L_FAQ}, loop variables like {postrow.MESSAGE}, {catrow.forumrow.U_VIEWFORUM} and defined variables like {$VARNAME}



To access root level variables within php code in template files use $this->vars[‘VAR’]. For example, {U_LOGIN_LOGOUT} would be $this->vars[‘U_LOGIN_LOGOUT’];



To access defined variables within php code in template files use $this->_tpldata[‘DEFINE’][‘.’][‘VARNAME’]. For example, {$MYVAR} will be $this->_tpldata[‘DEFINE’][‘.’][‘MYVAR’];

3. Loop Template Variables

To access loop variable inside loop in php code in template use name of last loop, add “_item” to it. For example, {postrow.MESSAGE} would be $postrow_item[‘MESSAGE’];

For nested loops use only last loop item. For example. {catrow.forumrow.U_VIEWFORUM} would be the same as {forumrow.U_VIEWFORUM}: $forumrow_item[‘U_VIEWFORUM’];



Current loop iteration can be accessed as “_i”. For example, for “postrow” loop it would be $postrow_i

Number of loop iterations can be accessed as “_count”. For example, for “postrow” loop it would be $postrow_count




Share this tutorial

If you want to show this tutorial to someone else please use following code to link to this tutorial:

HTML Link:
PHP Variables in Templates” class=”text” onclick=”this.focus(); this.select();”/>
Forum BBCode:

Content of this tutorial may not be published anywhere else without author’s permission. Please link to this page instead.