Documentation
Using Twig Templates
Introduction
If you know or used Django template engine or smarty template engine you will like Twig templates. It is very fast, secure, flexible maintained by sensio lab. By default cygnite integrated to supports beautiful Twig templates. You can also use twig template system instead of plain php views just by making configuration change in your controller class. The twig layouts gives you more flexibility to extends its blocks.
Using Controller Layout
You need to specify $templateEngine property equal to true and layout name in your controller class in order to use twig templates as view page instead of plain php views. For example:
namespace Apps\Controllers;
use Cygnite\Mvc\Controller\AbstractBaseController;
class HomeController extends AbstractBaseController
{
// src/Apps/Views/layouts/main/base.html.twig
//protected $layout = '';
protected $templateEngine = true;
/* you can change default template extension here */
//protected $templateExtension = '.html.twig';
protected $autoReload = true;
// Activate debugging of your twig template by changing it true or false
protected $twigDebug = true;
public function __construct()
{
parent::__construct();
}
public function indexAction()
{
//Apps/Views/home/index.html.twig
$view = $this->render('home.index', [
'title' => 'Cygnite Framework - Crud Application'
], true);
return Response::make($view);
}
}
In the above example you can see we activated twig template engine by simply changing $templateEngine as true. Cygnite has shipped with sample twig layout page inside /Apps/Views/layouts/main/base.html.twig.
Creating Simple View Page Using Twig Layout
Let us create a simple view page called index.html.twig and render it in twig base layout.
//apps/views/home/index.html.twig
{% extends 'layout/main/base.html.twig' %}
{% block title %}
Cygnite Framework - Simple Crud Operation
{% endblock %}
{% block content %}
Your Contents goes here
{% endblock %}
{% block footer %}
Your footer section if you want to override parent layout
{% endblock %}
Extending Twig
Cygnite is flexible enough to allow you to extend Twig core functionality or write your own extensions or functions.
Extending Twig By Creating Custom Extension
Extending Twig Extension is simple enough using cygnite view wrapper. You need to register an extension by using the addExtension() method on Template object. Let us see how to extend twig extension below.
use Twig\Extension;
use Apps\Extensions\Twig\Extension;
class Project extends Twig_Extension
{
public function getName()
{
return 'project';
}
}
And register it in your controller.
namespace Apps\Controllers;
use Cygnite\Mvc\Controller\AbstractBaseController;
class HomeController extends AbstractBaseController
{
protected $layout = 'layout.main.base';
protected $templateEngine = true;
/* you can change default template extension here */
//protected $templateExtension = '.html.twig';
protected $autoReload = true;
// Activate debugging of your twig template by changing it true or false
protected $twigDebug = true;
public function __construct()
{
parent::__construct();
}
public function indexAction()
{
// Adding custom extension to twig template
$this->getTemplate()->addExtension(new \Apps\Resources\Extensions\TwigExtension\Project());
//Apps/Views/home/index.html.twig
$view = $this->render('home.index', [
'title' => 'Cygnite Framework - Crud Application'
], true);
return Response::make($view);
}
}
For more detailed information about twig extension you can read beautiful documentation by sensio lab here.
Extending Twig By Creating Custom Functions
Adding a function is similar to adding a new filter function to twig engine. You can write your own custom functions which can be accessible in the twig template. This can be done by calling the addFunction() method on the Template instance:
namespace Apps\Controllers;
use Cygnite\Mvc\Controller\AbstractBaseController;
class HomeController extends AbstractBaseController
{
protected $layout = 'layout.main.base';
protected $templateEngine = true;
/* you can change default template extension here */
//protected $templateExtension = '.html.twig';
protected $autoReload = true;
// Activate debugging of your twig template by changing it true or false
protected $twigDebug = true;
public function __construct()
{
parent::__construct();
}
public function indexAction()
{
// Adding custom function to twig template
$path = 'your-input-with-dash';
$this->getTemplate()->addFunction('mySimpleFilter', function () use ($path)
{
// .. we will replace dash with underscore and return it
});
//Apps/Views/home/index.html.twig
$view = $this->render('home.index', [
'title' => 'Cygnite Framework - Crud Application'
], true);
return Response::make($view);
}
}
You can find more detail information of twig in Twig Template Documentation.