sFire PHP Framework

Template engine: View helpers

You can register view helpers to extend the functionality in the view. To create a new view helper, you can add one in the controller.

Note: You can also use Helpers if you wan't the functionality to be available not only in the view.

Custom template helpers

In the example below we are going to create currency/number format helper which will format numbers to currency amounts. This helper will accept two arguments; the number to be formatted and the currency as a string.

Controller:

$this -> template('format', function($number = 0, $currency = '$') {
    return $currency . number_format($number, 2, '.', ',');
});

As you can see, the template method accepts two arguments. The first argument is the name of the new method as a string and the second argument is an (anonymous) function which will be executed when the view helper is called from the view.

To use this method in the view, you can call the method by name:

@format(25, '$') //Will output $25.00

Note: built-in template functions can not be overwritten: @else, @elseif, @for, @foreach, @endif, @endforeach, @fails, @form, @helper, @partial, @passes, @router and @translation