Development Wiki

Difference between revisions of "Tutorial.Template syntax"

From phpBB Development Wiki

m (Note/Warning:: this is a subsection of the preceding)
Line 39: Line 39:
===Detailed information===
For more detailed information regarding the assigning of template variables have a look at the articles that are made for these functions: [[template.assign_var|template::assign_var]], [[template.assign_vars|template::assign_vars]] and [[template.assign_block_vars|template::assign_block_vars]]
==File naming==
==File naming==

Revision as of 12:07, 23 September 2008

This is a very brief guide to how templates have changed in 3.0.

Assigning data

The template class (PHP side) itself has not changed much since 2.0.x, so if you're already familiar with the API you can skip this part. However, thanks to the improved Sytax elements like automatic language strings, conditionals or BEGINELSE you can assign less redundant data and move some of the logic into the template files.


Assign a single variable:


Assign multiple variables:

'FOO' => $foo,
'BAR' => $bar,
'BAZ' => $baz


Blocks are used to assign any number of items of the same type, e.g. topics or posts ("foreach loop").

while($row $db->sql_fetchrow($result))
$template->assign_block_vars('loopname', array(
'FOO' => $row['foo'],
'BAR' => $row['bar']

Nested loops:

while($topic $db->sql_fetchrow($result))
$template->assign_block_vars('topic', array('TOPIC_ID' => $topic['topic_id']));

$post $db->sql_fetchrow($result))
$template->assign_block_vars('', array('POST_ID' => $post['post_id']));

Detailed information

For more detailed information regarding the assigning of template variables have a look at the articles that are made for these functions: template::assign_var, template::assign_vars and template::assign_block_vars

File naming

Firstly templates now take the suffix ".html" rather than ".tpl". This was done simply to make the lifes of some people easier wrt syntax highlighting, etc.

Syntax elements


The basic syntax for simple (non-block) vars remains the same as with 2.0.x. That is variables take the form {X_YYYYY} with the data being assigned from the source. Note that unlike 2.0.x most language strings are not assigned from the source. When a language variable is found {L_YYYYYY} phpBB first looks if an assigned variable exists with that name. If it does, it uses that. If not it looks if an exsting string defined in the language file exists. This should reduce the need to assign loads of new lang vars in Mods.


The basic block level loop remains and takes the form:

<!-- BEGIN loopname -->
markup, {loopname.X_YYYYY}, etc.
<!-- END loopname -->

However this has now been extended with the following additions. Firstly you can set the start and end points of the loop. For example:

<!-- BEGIN loopname(2) -->
<!-- END loopname -->

Will start the loop on the third entry (note that indexes start at zero). Extensions of this are:

  • loopname(2,4): Starts loop on third values, ends on fourth
  • loopname(-4): Starts loop fourth from last value
  • loopname(2, -4): Starts loop on third value, ends four from end

Note that the indexing method may change since it's not really consistent at this time :)

A further extension to begin is BEGINELSE:

<!-- BEGIN loop -->
<!-- BEGINELSE -->
<!-- END loop -->

This will cause the markup between BEGINELSE and END to be output if the loop contains no values. This is useful for forums with no topics (for example) ... in some ways it replaces "bits of" the existing "switch_" type control (the rest being replaced by conditionals, see below).

Including files

Something that existed in 2.0.x which no longer exists in 2.2.x is the ability to assign a template to a variable. This was used (for example) to output the jumpbox. Instead (perhaps better, perhaps not but certainly more flexible) we now have INCLUDE. This takes the simple form:

<!-- INCLUDE filename -->

You will note in the 2.2 templates the major sources start with INCLUDE overall_header.html or INCLUDE simple_header.html, etc. In 2.0.x control of "which" header to use was defined entirely within the code. In 2.2.x the template designer can output what they like. Note that you can introduce new templates (i.e. other than those in the default set) using this system and include them as you wish ... perhaps useful for a common "menu" bar or some such. No need to modify loads of files as with 2.0.x


A contentious decision has seen the ability to include PHP within the template introduced. This is achieved by enclosing the PHP within relevant tags:

<!-- PHP -->
echo "hello!";
<!-- ENDPHP -->

You may also include PHP from an external file using:

<!-- INCLUDEPHP somefile.php -->

it will be included and executed inline.

A note, it is very much encouraged that template designers do not include PHP. The ability to include raw PHP was introduced primarily to allow end users to include banner code, etc. without modifing multiple files (as with 2.0.x). It was not intended for general use ... hence will not make available template sets which include PHP. And by default templates will have PHP disabled (the admin will need to specifically activate PHP for a template).

Conditionals/Control structures

The most significant addition to 2.2.x are conditions or control structures, "if something then do this else do that". The system deployed is very similar to Smarty. This may confuse some people at first but it offers great potential and great flexibility with a little imagination. In their most simple form these constructs take the form:

<!-- IF expr -->
<!-- ENDIF -->

expr can take many forms, for example:

<!-- IF loop.S_ROW_COUNT is even -->
<!-- ENDIF -->

This will output the markup if the S_ROW_COUNT variable in the current iteration of loop is an even value (i.e. the expr is TRUE). You can use various comparison methods (standard as well as equivalent textual versions noted in square brackets) including:

  • == [eq]
  •  != [neq, ne]
  • <> (same as !=)
  •  !== (not equivalent in value and type)
  • === (equivalent in value and type)
  • > [gt]
  • < [lt]
  • >= [gte]
  • <= [lte]
  • && [and]
  • || [or]
  •  % [mod]
  •  ! [not]
  • +
  • -
  • *
  • /
  • << (bitwise shift left)
  • >> (bitwise shift right)
  • | (bitwise or)
  • ^ (bitwise xor)
  • & (bitwise and)
  • ~ (bitwise not)
  • is (can be used to join comparison operations)

Basic parenthesis can also be used to enforce good old BODMAS rules. Additionally some basic comparison types are defined:

  • even
  • odd
  • div

Beyond the simple use of IF you can also do a sequence of comparisons using the following:

<!-- IF expr1 -->
<!-- ELSEIF expr2 -->
<!-- ELSEIF exprN -->
<!-- ELSE -->
<!-- ENDIF -->

Each statement will be tested in turn and the relevant output generated when a match (if a match) is found. It is not necessary to always use ELSEIF, ELSE can be used alone to match "everything else".

So what can you do with all this? Well take for example the colouration of rows in viewforum. In 2.0.x row colours were predefined within the source as either row color1, row color2 or row class1, row class2. In 2.2.x this is moved to the template, it may look a little daunting at first but remember control flows from top to bottom and it's not too difficult:

<!-- IF loop.S_ROW_COUNT is even -->
<tr class="row1">
<!-- ELSE -->
<tr class="row2">
<!-- ENDIF -->

This will cause the row cell to be output using class row1 when the row count is even, and class row2 otherwise. Big deal you say, 2.0.x did that! True, but here you are not limited to using class row1 or row2 ... you can use any defined class, set your own inline style, etc. What's more you are not limited to two row colours at all ... e.g.

<!-- IF loop.S_ROW_COUNT > 10 -->
<tr bgcolor="#FF0000">
<!-- ELSEIF loop.S_ROW_COUNT > 5 -->
<tr bgcolor="#00FF00">
<!-- ELSEIF loop.S_ROW_COUNT > 2 -->
<tr bgcolor="#0000FF">
<!-- ELSE -->
<tr bgcolor="#FF00FF">
<!-- ENDIF -->

This will output the row cell in purple for the first two rows, blue for rows 2 to 5, green for rows 5 to 10 and red for remainder. So, you could produce a "nice" gradient effect, for example.

What else can you do? Well, you could use IF to do common checks on for example the login state of a user:

<!-- ENDIF -->

This replaces the existing (fudged) method in 2.0.x using a zero length array and BEGIN/END.

User Variables

You can also define simple (boolean, integer or double) variables from inside the template. This is for example useful if you dont want to copy&paste complex IF expressions over and over again:

<!-- IF expr1 -->
<!-- DEFINE $COLSPAN = 3 -->
<!-- ELSEIF expr2 -->
<!-- DEFINE $COLSPAN = 4 -->
<!-- ELSE -->
<!-- DEFINE $COLSPAN = 1 -->
<!-- ENDIF -->


<tr><td colspan="{$COLSPAN}">...</td></tr>
<tr><rd colspan="{$COLSPAN}">...</td></tr>


The DEFINE keyword has some bizarre restrictions:

-- There must be exactly one space before and after the "=".

-- When defining strings, you must use single quotes.

in other words:

<!-- DEFINE $COLSPAN = 3 -->  //GOOD
<!-- DEFINE $COLSPAN=3 -->         //BAD
<!-- DEFINE $COLSPAN  =  3 -->     //BAD 

<!-- DEFINE $CLASS = 'class1' -->  //GOOD
<!-- DEFINE $CLASS = "class1" -->  //BAD
User variables can be cleared (unset) using