Quick links: Content - sections - sub sections
EN FR

All template modifiers bundled with Jelix. Modifiers are functions which modify the display of the content of a variable in a template.

cat

This modifier allow to concatenate a string to a variable.

Here is a variable $foo having the value 'bar':


$rep->body->assign('foo', 'bar');

If we want to concatenate the string 'hello' to the variable $foo:


<p>{$foo|cat:'hello'}</p>

The result will be:


<p>barhello</p>

count_*

Several modifiers allow to count things into a variable:

  • count_characters: to count characters
  • count_paragraphs: to count paragraphs
  • count_sentences: to count sentences
  • count_words: to count words.

An example. Here is a variable $foo having the value 'bar':


$rep->body->assign('foo', 'bar');

In the template:


<p>$foo contains {$foo|count_characters} characters</p>

The result:


<p>$foo contains 3 characters</p>

With the other modifiers:


<p>{$foo|count_paragraphs} paragraphs</p>
<p>{$foo|count_sentences} sentences</p>
<p>{$foo|count_words} words</p>

eschtml

This is an important modifier: it escapes the content of a variable for HTML. In fact, it executes htmlspecialchars().

Use it on untrusted contents, to avoid XSS issues or else!

indent

This modifier add indentation into a text of a variable.

Here is a text that we inject into a template:


$foo = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Vivamus ornare, enim bibendum mollis interdum." 
$rep->body->assign('foo', $foo);

Let's display it into the template:

code smarty>

<pre>

{$foo|indent}

</pre> </code>

The result will be:


<pre>
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
    Vivamus ornare, enim bibendum mollis interdum.
</pre>

There are two optional parameters:

  • the number of character to repeat. By default it is 4.
  • the character to repeat. By default, a space.

{$foo|indent:"15"}

Will be:


               Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
               Vivamus ornare, enim bibendum mollis interdum.

And


{$foo|indent:"5":"-"}

will be:


-----Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
-----Vivamus ornare, enim bibendum mollis interdum.

jdatetime

The jdatetime modifier permit to convert a date string in a format to another one. This modifier use the jDateTime class.

Without parameter, this modifier expect a database string format (jDateTime::BD_DTFORMAT) and return a date in selected language format (jDateTime::LANG_DTFORMAT).


  <p>this date are {$myDate|jdatetime}.</p>

if $myDate='2017-04-12 01:05:26' an if language are en, then it display


 this date are 12/04/2017 01h05mn26s.

To use another formats, you have to use one of this string in modifier parameter :

parameter constant in jDateTime class format
'lang_date' jDateTime::LANG_DFORMAT date format of current application lang (with en, this is mm/dd/yyyy)
'lang_datetime' jDateTime::LANG_DTFORMAT date and time format of current application lang
'lang_time' jDateTime::LANG_TFORMAT time format of current application lang
'db_date' jDateTime::DB_DFORMAT date of classic database style : "YYYY-MM-DD"
'db_datetime' jDateTime::DB_DTFORMAT date and time of classic database style : "YYYY-MM-DD HH:mm:SS"
'db_time' jDateTime::DB_TFORMAT timeof classic database style : "HH:mm:SS"
'iso8601' jDateTime::ISO8601_FORMAT format ISO8601
'timestamp' jDateTime::TIMESTAMP_FORMAT integer, number of seconds since 1/1/1970 (timestamp)
'full_lang_date' jDateTime::FULL_LANG_DATE full format date (i.e. day of week and month in letter) respecting language conventions (ex: thursday, may 27th 2017)

The first parameter is the format of date string to convert, the second one, the format to display.


  <p>this date are {$myDate|jdatetime:'db_date':'timestamp'}.</p>

if $myDate='2006-04-15' then it display


 this date are 1145052000.

mailto

this modifier permits to display a link to the user's email in the form of your choice :

several options can be used for that:

  • displaying an email in a classic form

{mailto array('address'=>'me@domain.com')}
  • displaying an email encoded in javascript

{mailto array('address'=>'me@domain.com', 'encode'=>'javascript')}
  • displaying an email encoded in hexa

{mailto array('address'=>'me@domain.com', 'encode'=>'hex')}
  • displaying an email following by the subject of this onejavascript

{mailto array('address'=>'me@domain.com', 'subject'=>'Hello to you!')}
  • displaying an email with 'cc' on severals email

{mailto array('address'=>'me@domain.com', 'cc'=>'you@domain.com,they@domain.com')}
  • additionals options are available, like a CSS class

{mailto array('address'=>'me@domain.com', 'extra'=>'class="mailto"')}

number_format

This modifier format a number to be displayed like explains in the PHP doc number_format

For example :


{number_format $number, 2, '.', ''}

regexp_replace

It replaces a string by an other by using a regular expression.


$myVar = 'TOTOO';

{$maVar|regex_replace:'/O/':'ii'}

The string TOTOO will be transformed to tiitiiii.

spacify

It insert a letter between each letters.


{assign $mytext = 'Hello_everybody'}
{$mytext|spacify:}

Result: H e l l o _ e v e r y b o d y.


{assign $mytext = 'Hello_everybody'}
{$mytext|spacify:'.'}

Result: H.e.l.l.o._.e.v.e.r.y.b.o.d.y.

printf

Transform a string by using the sprintf function.


{assign $mytext = 'Hello everybody'}
{$mytext|sprintf:'my format %s'}

Result : my format Hello everybody.

strip

It removes extra space from the string.


{assign $mytext = 'Bonjour  tout      le   monde'}
{$mytext|strip}

Result : Bonjour tout le monde


{assign $mytext = 'Bonjour tout le     monde'}
{$mytext|strip:';'}

Result : Bonjour tout le;monde

truncate

Modifier that truncates a string to a certain length if necessary, optionally splitting in the middle of a word, and appending an ending string like '...'.


{assign $mytext = 'Bonjour tout le monde'}
{$mytext|truncate}

Result: Bonjour tout le monde


{assign $mytext = 'Bonjour tout le monde'}
{$mytext|truncate:10}

Result: Bonjour...


{assign $mytext = 'Bonjour tout le monde'}
{$mytext|truncate:10:'[...]'}

Result: Bonjour[...]

If you are agree to cut at the middle of a word:


{assign $mytext = 'Bonjour tout le monde'}
{$mytext|truncate:10:'...':true}

Result: Bonjour to...

truncatehtml

Modifier that cuts a html formated string and close all opened tags so that it doesn't inpact on the rest of the page.

You should use this modifier in a zone so that the return value is cached.


{$mytext|wordwrap} 
{$mytext|truncatehtml:150:"\n<a href="...">read full article</a>"} 
{$mytext|truncatehtml:45} 

wiki

This modifier convert a string having wiki content, to html. It uses the jWiki class.

By default it convert from the "wr3" syntax to html.


  <p>{$message|wiki}</p>

If $message is


===== title =====

A word in __bold__.

The result will be


<h1>title</h1>

<p>A word in <strong>bold</strong></p>

html:nl2br

It replace all line return characters by the html element <br/>.

For example, here is a text:


$foo = "Lorem ipsum dolor sit amet,\nconsectetuer adipiscing elit." 
$rep->body->assign('foo', $foo);

And a template:


<p>{$foo|nl2br}</p>

The result is:


<p>Lorem ipsum dolor sit amet,<br/>consectetuer adipiscing elit.</p>