📜 ⬆️ ⬇️

Integrating Twig Template Engine into Symfony 1.4

When trying to integrate Twig into Symfony, the following problems arose: there is practically no information, there is no integration guide, there is a mini documentation on Twig, which describes how to work with Twig, but not how to teach it to work with Symfony. I had to search a little and ask around in google groups.

I hope this material will be useful to someone.


Download sfTwigPlugin (there are extensions that “import” Symfony helpers into Twig, there is a view class that overrides sfPHPView).
')
Follow the instructions described here .

Place the plugin in the / lib / vendor / symfony / lib / plugins / sfTwigPlugin / directory.

We connect it to ProjectConfiguration.class.php:

$ this ->enablePlugins( 'sfDoctrinePlugin' , 'sfTwigPlugin' ); <br><br> * This source code was highlighted with Source Code Highlighter .


Save the plugin from twig-project.org to the / lib / vendor / Twig directory.

In the / config directory, create the module.yml file and define the View class, which will be responsible for displaying the Twig template pages:

all:<br> view_class: sfTwig<br> partial_view_class: sfTwig<br> <br> * This source code was highlighted with Source Code Highlighter .


The action that will display the template looks like this:

function executeIndex() {<br> // Symfony <br> require_once 'path_to_symfony_project/lib/vendor/symfony/lib/helper/AssetHelper.php' ;<br> require_once 'path_to_symfony_project /lib/vendor/symfony/lib/helper/TagHelper.php' ;<br> <br> $loader = new Twig_Loader_Filesystem( '/var/www/symfony-test/html/apps/blahg/modules/posts/templates' );<br> <br> $twig = new Twig_Environment($loader, array( 'cache' => false ));<br> <br> // <br> $lexer = new Twig_Lexer($twig, array(<br> 'tag_comment' => array( '/*' , '*/' ),<br> 'tag_block' => array( '[' , ']' ),<br> 'tag_variable' => array( '{' , '}' ),<br> ));<br> $twig->setLexer($lexer);<br> <br> // Symfony <br> $twig->addExtension( new Asset_Twig_Extension());<br> $twig->addExtension( new Tag_Twig_Extension());<br> <br> // <br> $ this ->postss = Doctrine::getTable( 'Posts' )<br> ->createQuery( 'a' )<br> ->leftJoin( 'a.Comments c' )<br> ->execute();<br> <br> // <br> $template = $twig->loadTemplate( 'indexSuccess.html' );<br> <br> // HTTP <br> $ this ->getResponse()->setContent($template->render(array( 'postss' => $ this ->postss->getData())));<br> <br> // <br> return sfView::NONE;<br>} <br><br> * This source code was highlighted with Source Code Highlighter .


Here is a self-sufficient example of the action, but in the future you will need to bring in the helpers and redefining the template tags.

In the / apps / <project_name> / modules / <module_name> / templates directory create:

layout.php

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > <br> < html xmlns ="http://www.w3.org/1999/xhtml" xml:lang ="en" lang ="en" > <br> < head > <br> { 'main'|use_stylesheet('first') }<br> { ''|include_stylesheets }<br> </ head > <br> < body > <br> [ block content ][ endblock ]<br> </ body > <br> </ html > <br><br> * This source code was highlighted with Source Code Highlighter .


indexSuccess.html

[ extends "layout.html" ]<br><br>[ block content ]<br><br> < h1 > Postss List </ h1 > <br> < table class ="posts-list" > <br> < thead > <br> < tr > <br> < th > Id </ th > <br> < th > Title </ th > <br> < th > Text </ th > <br> < th > Modified </ th > <br> < th > Comments count </ th > <br> </ tr > <br> </ thead > <br> < tbody > <br> [ for posts in postss ]<br> < tr > <br> < td > { posts.id } </ td > <br> < td >< a href ="" > { posts.title } </ a ></ td > <br> < td > { posts.text } </ td > <br> < td > { posts.modified_ } </ td > <br> < td > { posts.Comments.count } </ td > <br> </ tr > <br> [ endfor ]<br> </ tbody > <br> </ table > <br><br>[ endblock ] <br><br> * This source code was highlighted with Source Code Highlighter .


Now symfony helpers should work as Twig filters.

Until it turned out, some AssetHelper helpers could work, for example:
{ 'main'|use_stylesheet('first') }
{ ''|include_stylesheets }

only work together if you try to load the styles specified in the view.yml config
{ ''|include_stylesheets }
tags on the page will not appear.

PS Theme symfony and Twig for me is new, so if someone has valuable tips, I will be glad to hear them out. If the continuation of the story is interesting, I will write another article in the course of performing new tasks.

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


All Articles