Advanced CoTemplate features

CoTemplate's expert features revealed

This page is not translated yet. Showing original version of page. You can sign up and help with translating.

Much of Cotonti's flexibility is derived from it's templating engine CoTemplate (previously known as XTemplate). Recent additions to the engine include nested logic blocks (conditionals and loops) and callbacks. Simple IF statements will be familiar to most developers, but there's more than just the regular equals comparison. Here's an overview of what's possible:

Name Operator Description
Equal == Values are equal
Not Equal != Values are not equal
Identical === Values are equal, and they are of the same type
Not Equal != Values are not equal
Not Identical !== Values are not equal, or they are not of the same type
Less Than < Left is smaller than right
Greater Than > Left is larger than right
Less Than or Equal <= Left is smaller or equal to right
Greater Than or Equal >= Left is larger or equal to right
Has HAS Left (array) contains right (string)
Contains ~= Left (string) contains right (string)
Addition + Adds two values
Subtraction - Subtracts two values
Multiplication * Multiplies two values
Division / Devides two values
Modulus % Calculates modulus of two values
AND AND Groups two conditions, both must be true
OR OR Groups two conditions, at least one must be true
Exclusive OR XOR Groups two conditions, only one may be true
NOT ! Negates (inverts) boolean

These operators can be used in IF conditions in your template files. Here's an example that combines several operations:

<!-- IF {PHP.i} % 2 == 0 AND {PHP.usr.isadmin} -->
The row is even and the user has administrative rights.
<!-- ENDIF -->

Since Siena 0.9.0, which includes CoTemplate 2.5, conditional blocks can be nested. This means that you can put conditions within conditions, or loops within conditions, etc. This can also be useful if you want to have an ELSE IF (which isn't normally supported) by putting an IF statement within the ELSE block.

A very powerful feature that is included with CoTemplate 2.5 is callbacks. A callback is a PHP function call from within your template, which can take a regular template tag as parameter. Instead of the template tag's value, the return value of the callback function is inserted into your HTML. Any template tag can accept a callback function, including PHP variable calls such as {PHP.myvar} (which refers to $myvar in the script). This is achieved by appending a pipe | sign to the template tag, as well as the name of a PHP function (including user defined functions and Cotonti's core functions). The function call itself is the same as in regular PHP code, meaning any parameters are enclosed in parentheses. There is one exception however which involves the callback value. Template callbacks have one special variable named $this which contains the value of the template tag preceding the callback. By using this variable you gain the ability to pass on a template tag to a regular PHP function and use the result in your template. Here's some examples of interesting ways to use callbacks:

{PAGE_BEGIN_STAMP|cot_date('date_full', $this)} 2011-01-01
(depends on locale settings)
{LIST_ROW_BEGIN_STAMP|cot_date('date_text', $this)} January 1, 2011
(depends on locale settings)
Copyright {PHP|date('Y')} Copyright 2011
{PHP|cot_url('page', 'c=news')} page.php?c=news
(depends on URL rewrite rules)
{PHP.sys.xk|cot_url('users', 'm=logout&x=$this')} users.php?m=logout&x=1234567890abcdef
(depends on URL rewrite rules)

In the above list you also see the use of a 'PHP' tag. This basically does nothing, it has no value. It's a way to use a callback without linking it to a template tag.

Since the list of functions is enormous and can easily be extended by writing custom functions (for example in system/functions.custom.php), the possibilities with this are virtually endless. However, you should be aware that in many cases, the easiest solution is not the best. Putting logic in your template has some drawbacks. Firstly, it's slow if used a lot. For more complex situations you're better off writing a plugin instead. Secondly, it undermines the principles of MVC, in which logic should be kept seperate from layout. This can be a problem if you work with designers that have no knowledge of Cotonti or even PHP.

1. dedushka  2012-06-27 12:54

Как выполнить в теге функцию, не принимающую никаких параметров?

Вариант {|myfunc}, естесственно, не проходит...

2. Trustmaster  2012-06-27 17:22



Alleen geregistreerde gebruikers kunnen reacties plaatsen.