📜 ⬆️ ⬇️

A brief guide to integrating the Twig template and Slim Micro Framework

Stumbled upon a post once. How traffic jams arise , I decided to take a closer look at what the Slim Micro Framework is and how I can use it in my projects.

The first thing was to choose a template engine. After a brief search, the choice fell on Twig and now you can begin to integrate.

The essence of integration is to connect the Slim Framework Extras add-on , which implements the functionality for integration with several popular templating engines, in particular with Twig, through the extension of the base class Slim_View , written by the framework developers.

Now you need to include the code in the project, the index.php file (the location and file names may differ):
')
//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 remained unchanged. An example of a router and a callback function for it, used in the application (the code is simplified in places):

 //   $app->get('/:id', 'show_gallery'); /** * Callback-   * * @global Slim $app * @param <type> $photo_id */ function show_gallery( $photo_id ) { global $app; //   ... //     $app->view()->appendData( array( 'photo' => $photo ) ); //   //        /templates     $app->render('template.php'); } 


Example of part of the code from template.php:

 <div class="container"> <a href="{{ photo.l_url }}" class="ajax" title="{{ photo.title }}"> <img class="current" src="{{ photo.m_url }}" alt="{{ photo.title }}" /> </a> </div> 


Materials on the topic:

A simple implementation of the Slim Framework Extras + Twig bundle can be found here Posters on ASOIU . If necessary, I am ready to demonstrate all the code (only one hundred lines describing the application logic).

I apologize in advance for possible mistakes and omissions when writing a post. This is my first article. It took only a couple of days to master the material, therefore I will consider any useful links to materials and advice on the topic as courtesy. Thank.

Source: https://habr.com/ru/post/140712/


All Articles