Brief instructions for integrating Twig template engine and Slim Micro Framework
Having stumbled upon a post once How traffic jams arise , I decided to look in more detail at what Slim Micro Framework is and how I can use it in my projects.
The first step was to choose a template engine. After a short search, the choice fell on Twig and now you can proceed with integration.
The essence of integration is to connect the Slim Framework Extras add-on that implements functionality for integration with several popular template engines, in particular with Twig, through the extension of the base class Slim_View , written by the developers of the framework.
Now you need to include the code in the project, the index.php file (the location and file names may vary):
Passing parameters to View has remained unchanged. An example of a router and a callback function for it used in the application (the code is simplified in some places):
An example of a piece of code from the template.php template:
Related materials:
A simple implementation of the Slim Framework Extras + Twig bundle can be seen here Posters on the topic ASOIU . If necessary, ready to demonstrate all the code (just a hundred lines describing the application logic).
I apologize in advance for possible errors and omissions when writing the post. This is my first article. It took just a couple of days to master the material, so I will consider for courtesy any useful links to materials and tips on the topic. Thanks.
The first step was to choose a template engine. After a short search, the choice fell on Twig and now you can proceed with integration.
The essence of integration is to connect the Slim Framework Extras add-on that implements functionality for integration with several popular template engines, in particular with Twig, through the extension of the base class Slim_View , written by the developers of the framework.
Now you need to include the code in the project, the index.php file (the location and file names may vary):
//Require the Slim Framework
require_once 'Slim/Slim.php';
//http://twig.sensiolabs.org/doc/intro.html#basic-api-usage
require_once 'thirdparty/Twig/Autoloader.php';
Twig_Autoloader::register();
//Require the custom View
require_once 'views/TwigView.php';
//Init Slim app with the custom View
$app = new Slim(array(
'view' => new TwigView()
));
$app->run();
Passing parameters to View has remained unchanged. An example of a router and a callback function for it used in the application (the code is simplified in some places):
// Определяем роутер
$app->get('/:id', 'show_gallery');
/**
* Callback-функция для роутера
*
* @global Slim $app
* @param $photo_id
*/
function show_gallery( $photo_id ) {
global $app;
// Ваш код
...
// Передаем параметры в шаблон
$app->view()->appendData( array( ‘photo' => $photo ) );
// Отрисовываем шаблон
// по умолчанию файлы шаблонов находятся в папке /templates в корне вашего проекта
$app->render('template.php');
}
An example of a piece of code from the template.php template:
Related materials:
A simple implementation of the Slim Framework Extras + Twig bundle can be seen here Posters on the topic ASOIU . If necessary, ready to demonstrate all the code (just a hundred lines describing the application logic).
I apologize in advance for possible errors and omissions when writing the post. This is my first article. It took just a couple of days to master the material, so I will consider for courtesy any useful links to materials and tips on the topic. Thanks.