# Template

The template system of Wolff allows you to write cleaner PHP code in your views, avoiding things like the PHP tags.

It only works in the views and is completely optional, so you can write normal PHP code if you want to.

The template system can be completely disabled if the template_on key in the system/config.php file is equal to false.

Variables

Load

When loading a view from a controller, you can pass as parameter an associative array with data in it:

$data['message'] = 'Hello world';
$this->load->view('page', $data);

Display

Then in the view you can print the variables that are in that array using the brackets tag, this way:

{{ $message }}

This will print the 'message' key in the data array, keep in mind that the content will be automatically escaped for your safety.

If you don't want your data to be escaped use the following tags:

{! $message !}

That is the equivalent to:

<?php echo $data['message']; ?>

All the variables in the data array are accessible from the view without needing to refer to the data array. This $variable is the equivalent to this $data['variable'].

Comments

The template system have an advantage over the common comments and that is that the comments of the template system aren't included in the final HTML returned to the user.

{# This is a simple comment #}

You can do multiline comments too.

{# This is a
multiline
comment #}

Tags

In Wolff you can use the replacement of the old php tags, which are {% for <?php and %} for ?>.

Instead of this:

<?php foreach ($array as $key => $value): ?>
    //code
<?php endfor ?>

You can do this:

{% foreach ($array as $key => $value): %}
    // code
{% endfor %}

Or even better use the complete syntax of the template:

{ foreach $array as $key => $value }
    // code
{ foreach }

Conditionals

To write conditional statements, put your variable/condition before an interrogation symbol, all of this inside brackets.

{ $foo? }
    // code
{?}

That is the equivalent to this:

<?php if ($foo): ?>
    // code
<?php endif; ?>

To write else if:

{ $foo? }
    // code
{ else $foo2 }
    // more code
{?}

Loops

You can write foreach loops the following way:

{ foreach $array as $key=>$value }
    // code
{ foreach }

That is the equivalent to this:

<?php foreach ($array as $key=>$value): ?>
    // code
<?php endforeach; ?>

You can also write foreach loops this way:

{ foreach $array as $element }
    // code
{ foreach }

You can write traditional for loops too:

{ for i in (0, 10) }
    {{$i}}
{for}

The same but using variables:

{ for i in (0, strlen($text) )}
    {{$i}}
{for}

Functions

The template system of Wolff has some abbreviated functions to make the code cleaner.

To use a function only write its' name followed by a vertical bar and then the variable. Like this:

{{ strtoupper($title )}}

In this case, that will print the $title variable in uppercase.

{{ str_repeat($title , 3)}}

In this case, that will print the $title variable three times.

List

This are the available functions and their PHP equivalent:

Import

Instead of using the html script and link tags for importing external files, you can include them using the template tags.

{% style="styles.css" %}

Equivalent to: <link rel="stylesheet" type="text/css" href="styles.css"/>

{% script="scripts.js" %}

Equivalent to: <script type="text/javascript" src="scripts.js"></script>

{% icon="img.svg" %}

Equivalent to: <link rel="icon" href="img.svg">

Escape

You may want to escape the template tags, in that situation only prefix your text/tag with an ~ symbol, like this:

~{{ $message }}

That will leave this in the HTML returned to the client:

{{ $message }}

Include

Instead of using the php include function. You can include other views using the load method.

Example:

<div>
    @load(header.php)
</div>

That will put all the header.php content inside the div tags.

Extending the template

You can extend the template engine and make your own tags or rules using regular expressions and the custom function.

The custom content must be defined in the system/Definitions/Templates.php file.

The custom function takes a closure, which must take a parameter that is suposed to be the view content and finally it must return it.

If you add the following code to the system/Templates.php file:

Template::custom(function ($content) {
    return preg_replace('/\!\!(.*?)\!\!/', '<?php echo "message: $1" ?>', $content);
});

Now the following tags in the views !! $str !! will be replaced by <?php echo "message: $str" ?>.