The Template Parser

Bonfire ships with a powerful template parser called Lex, which was originally devleoper by Dan Horrigan and then taken over and maintained by the PyroCMS team.

Enabling the Parser

The parser can be enabled by setting the parse_views option of the Template library to TRUE.

Template::$parse_views = TRUE;

Now all views will be rendered using the template parser. Lex does allow for PHP to be parsed within the layouts at the same time as the Lex tags so you can keep the parser turned on at all times without any issues.

Tag Basics

To display data in Lex, you must use a tag. Tags are variables that are wrapped in double curly braces.

Note that whitespace before or after the braces does not matter, though more advanced uses will require whitespace.

To provide data for your tags, use the Template::set(); method.

// In the controller
Template::set('name', 'Bonfire');

// In the View
    // Display: Bonfire

You can access elements of an array within a tag by using a period (.) and the array key name.

// In the controller
$user = array(
    'name' = 'Darth Vader'
);
Template::set('user', $user);

// In the View
Your name is .

Looping Tags

When tags contain an array of data, like blog posts, you can loop through the data by using opening and closing tags. Any array values will then be accessible by their key name.

// In the controller
$posts = array(
    array(
        'title' => 'Post 1',
        'url'   => 'blog/post-1'
    ),
    array(
        'title' => 'Post 2',
        'url'   => 'blog/post-2'
    )
);
Template::set('posts', $posts);

// In the view

    <h1><a href=""></a></h1>

Conditional Tags

Conditionals in Lex are simple and easy to use, allowing for the standard if, elseif and else. It also adds unless and elseunless.

unless and elseunless are the EXACT same as using and respectively.

    <p>My name is   .</p>



    <p>You are admin!</p>

    <p>You are a normal user.</p>

    <p>I don't know what you are.</p>

Undefined variables in conditionals are translated to NULL. This means you can do things like and not worry about whether it exists or not.

Modules, Libraries and Templates

The most useful part of using the template library is that we can can module methods and library methods directly from our views. This is done by calling the module name followed by a colon and the name of the method.

    <li>  -  - </li>

Calling modules is still in its infancy and you might run into a few issues, but here are some guidelines to follow:

Full Docs

These documentations are just to get your feet wet in using the template parser in your projects. For full documentation, see the Lex GitHub site.