Jump to content

Template tags, revisited

A while ago, I posted about the new template tags system in IP.Board 3.0.0.

After some initial feedback on the syntax and having developed the system further, I felt it was worth revisiting in our blog.

The template tag system is still based on PHP classes (extends and implements, PHP5 fans) which act as plug-ins. These plug-ins are only run when the templates are cached which makes the system very fast and very efficient.

The new tag syntax is very straightforward and easy to remember: {parse plugin="data" option="value" option2="value2"}

The updated system also allows you to embed tags, which we'll see later.

First off, here are the tags which are currently being used in IP.Board 3.0.0

This tag is used to parse an in-line PHP expression which returns a value. This can be any inbuilt PHP function or any IP.Board function.

{parse expression="intval($data['count'])"}
{parse expression="IPSText::alphanumerical_clean( $data['value'] )"}

This makes it very easy to format data on the fly and removes the need for obsessive intval'ing in your code to ensure that a correct value is displayed such as a zero rather than nothing at all!

This tag is used to insert any other template bit from IP.Board.

{parse template="pageLinks" group="skin_global" params="$data['start'], $data['end']"}

The benefit of this tag is immediately obvious. You don't have to 'hack' the PHP sources to re-use template bits.

This tag is used to set and alter a template variable.

To set up a variable with they key 'className':
{parse variable="className" default="tdRow1" oncondition="$data['foo'] == 'bar'" value="tdRow2"}

To alter the variable based on different data:
{parse variable="className" oncondition="$data['foo'] == 'foo'" value="tdRow3"}

To use the variable:
{parse variable="className"}

Lets look at a practical example:

{parse variable="className" default="row1" oncondition="$data['banned'] === TRUE" value="rowBanned"}
<foreach loop="$key => $data">
<div class="{parse variable="className"}">

While Rikki was working on the new IP.Board skin, he mentioned that it would be very handy to have some kind of tag that could handle the 'zebra' striping (the alternate row colours when viewing a forum list, etc).

To set up a striping tag:
{parse striping="someKey" classes="tdrow1, tdrow2, tdrow3"}

To use a striping tag:
{parse striping="someKey"}

When parsed, this tag will cycle through the classes, repeating the cycle if required.

Let's look at a practical example:

{parse striping="rows" classes="row1, row2"}
<foreach loop="$data as $key => $value">
<div class="{parse striping="rows"}">$key = $value</div>

It can be difficult to adhere to strict standards when dealing with templates and piece-meal code. Often you compromise and have javascript and CSS scattered throughout the finished document.

IP.Board allows you to add content to the document <head> very quickly and simply:
{parse addtohead="/js/someScript.js" type="javascript"}
{parse addtohead="/css/someCSS.css" type="css"}
{parse addtohead="<script>alert('boo')</script>" type="raw"}

In previous versions of IP.Board, all the date processing was performed inside the PHP code which made it very hard to customize the date formats beyond what was allowed in the Admin CP.
Now, IP.Board allows you to process dates using the date tag in templates.

{parse date="now"}
{parse date="1765346750" relative="1" format="long"}
{parse date="-1 hour" format="manual{d, m, Y}"}

IP.Board 3 allows a very easy way to format URLs without having to reply on special PHP variables.

{parse url="showtopic=1" base="public"}
{parse url="showforum=5" base="publicWithApp"}
{parse url="photo1.jpg" base="upload"}

Format Number
IP.Board 3 allows a quick and easy to to access the built in "format_number" function.

{parse format_number="123456"}

To keep our tags uniform, we've removed the old 'macro' tag (<{MACRO_HERE}>) and replaced it with the new 'replacements' tag:

{parse replacement="macroKey"}

Of course, the real power in this system is that you can very quickly and simply create your own template tags and drop them in the template tags folder for them to be available instantly.

  • Create New...