Jump to content
Matt
 Share


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

Expression
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!

Template
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.

Variable
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"}">
</foreach>

Striping
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>
</foreach>

AddToHead
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"}

Date
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.

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

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

Examples:
{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.

Example:
{parse format_number="123456"}

Replacement
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.

 Share

Comments

Recommended Comments





Is that more powerful ? As in it was much easier to type <{MACRO}> than that ;)

Replacement
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"}
Link to comment
Share on other sites

looking good, i guess lindsey if your building a parser to continually look for {parse something="" } throughout it'd be easier to make everything use it to save keep writing in find and replace functions in the parser.

Link to comment
Share on other sites

[quote name='Alεx' date='Aug 5 2008, 11:58 AM']Glad you revisited it, looking extremely good, already had a few discussions about AddToHead, seems to be extremely useful :)

Kudos to all of you :)
Yes, I'm pretty excited about AddToHead as well, will be really nice to be able to add CSS and JS files that way. :)

Link to comment
Share on other sites

I personally would've liked just a simple update of the current system as that seems a lot easier to understand, but I can see that this is the way forward. More learning though for me :(

Congrats IPS :)

Link to comment
Share on other sites

It's good. But I hope skins will have only html and a bit of {parse} code. I think it's not good to do lot of php functions in templates like
May be I wrong or don't understand main theme...

{parse expression="intval($data['count'])"}
{parse expression="IPSText::alphanumerical_clean( $data['value'] )"}
imho, this must be in source files, not in skins. Because I afraid of designers that will use lot of this functions and will make some trubles for mod developers or mod developers will use lot of {parse} code and make troules for other.
Link to comment
Share on other sites

  • Management

I would say it's better to have display logic in the templates rather than have it at the 'business' level which is what we're trying to achieve.

If we 'hardcode' a lot of data like we do now, you'll lose a lot of flexibility in the process.

Link to comment
Share on other sites

actually i didnt like it so much. so far i am excited about the changes i see in IPB3 but didnt like those tag so much. i think you are bringing php programming to much into the skin..i think that php codes should be aside from the design code.

Link to comment
Share on other sites

  • Management

Like I mentioned earlier, we're just bringing presentation logic into the presentation layer. It's not so much PHP, of course. The 'expression' tag is the only one that can really be called a PHP tag.

Link to comment
Share on other sites

I can tell you that the tags Matt described have already enabled me to do things that previously would have required core code edits. These new tags offer so much flexibility to skinners. I'm personally very excited to see what people might come up with.

Link to comment
Share on other sites

I really like the add to head tag, and the template insertion tag (going to be very nice to re-use template bits so easily!). This syntax is much less confusing than the previously announced IPB3 template expressions :)

Link to comment
Share on other sites

[quote name='Lindsey_' date='Aug 5 2008, 11:19 AM']

Is that more powerful ? As in it was much easier to type <{MACRO}> than that ;)


yes it sure will be more powerful Lindsey

Replacement
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"}
Link to comment
Share on other sites

[quote name='Matt' date='Aug 6 2008, 01:19 AM']Like I mentioned earlier, we're just bringing presentation logic into the presentation layer. It's not so much PHP, of course. The 'expression' tag is the only one that can really be called a PHP tag.

i just think the skin codes becomes more complicated due all the new tags and i think there will be many new things to learn in order to be familiar with the system and develop for it.

Link to comment
Share on other sites

  • Management

Like anything new, there is a learning curve. However, it's not that steep and after a few days of testing and experimentation, you'll embrace the new tags.

Of course, it's worth keeping in mind that you don't *have* to understand the tags to edit the template HTML.

The new system is much more powerful and removes the need to hardcode a lot of presentation logic (such as classnames, etc) within the PHP files.

With regards to the <{macro}> tag; this was changed simply so that it fell inline with the other tags. It is no more or less powerful than before.

Link to comment
Share on other sites

  • Management

I should also address the point as to whether allowing more power (PHP expressions, etc) within the skin files represents a security risk or not.

I will take the stance that you (as the admin) are completely responsible for your own board. It is your responsibility to ensure that any modifications or added skins are safe to use.

In any case, it's very hard for a software vendor to police against the potential actions of a malicious modification or skin set.

Link to comment
Share on other sites

Also, IPB skins have supported PHP for a while already, so we don't believe there's any additional security risk from 2.x. As should be the case with anything on the internet, only download files from trustworthy places :)

Link to comment
Share on other sites

Is all HTML in the templates going to be based round this PHP, or is it still going to be standard with all the <strong> etc (normal) tags, with just these in places like the board index where the rows alternate?

Link to comment
Share on other sites

Yeah the templates are still HTML, just with these special tags interspersed in it. We also have some ideas about how to make template editing really easy to use which we'll discuss in future.

Link to comment
Share on other sites

  • Management

As Rikki said, it's still HTML mark-up. The parse tags are just little blocks within the code. In fact, they're no more complex that what you're used to, really.

IP.Board 2.x
<strong><a href='{$this->ipsclass->base_url}showtopic=1'>Title</strong>

IP.Board 3.0.0
<strong><a href='{parse url="showtopic=1" base="public"}'>Title</strong>

Link to comment
Share on other sites




Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...