twig

{{ exception.message }} in Twig doesn't render HTML

…衆ロ難τιáo~ 提交于 2020-01-13 16:27:00
问题 I have this controller where \Exception is raised (I haven't figured out which SF2 Exception to use yet) upon certain condition. Here is it: <?php namespace My\AppBundle\Controller; use .... class MyController extends Controller { const EXCEPTION_MESSAGE = <<<EOF My <b>HTML</b> <br/> <small>Small phrase</small> EOF; public function indexAction() { // my logic if(in_array($data, $array)) throw new \Exception(self::EXCEPTION_MESSAGE); // the rest of my logic return .... } } And in app/Resources

How do I extend a twig template in the same directory of my other templates?

情到浓时终转凉″ 提交于 2020-01-13 11:29:32
问题 I have index.html.twig and base.html.twig in the same directory folder..I have the following scipts index.html.twig {% extends('base.html.twig') %} {% block body %} helo body {{ parent() }} {% endblock %} {% block footer %} This footer {% endblock %} base.html.twig <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>{% block title %}Welcome!{% endblock %}</title> {% block stylesheets %}{% endblock %} <link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}" /> </head>

Using twig variable to dynamically call an imported macro sub-function

 ̄綄美尐妖づ 提交于 2020-01-13 08:11:03
问题 I am attempting if use a variable to call a specific macro name. I have a macros file that is being imported {% import 'form-elements.html.twig' as forms %} Now in that file there are all the form element macros: text, textarea, select, radio etc. I have an array variable that gets passed in that has an elements in it: $elements = array( array( 'type'=>'text, 'value'=>'some value', 'atts'=>null, ), array( 'type'=>'text, 'value'=>'some other value', 'atts'=>null, ), ); {{ elements }} what im

Using twig variable to dynamically call an imported macro sub-function

守給你的承諾、 提交于 2020-01-13 08:10:12
问题 I am attempting if use a variable to call a specific macro name. I have a macros file that is being imported {% import 'form-elements.html.twig' as forms %} Now in that file there are all the form element macros: text, textarea, select, radio etc. I have an array variable that gets passed in that has an elements in it: $elements = array( array( 'type'=>'text, 'value'=>'some value', 'atts'=>null, ), array( 'type'=>'text, 'value'=>'some other value', 'atts'=>null, ), ); {{ elements }} what im

Using twig variable to dynamically call an imported macro sub-function

岁酱吖の 提交于 2020-01-13 08:10:10
问题 I am attempting if use a variable to call a specific macro name. I have a macros file that is being imported {% import 'form-elements.html.twig' as forms %} Now in that file there are all the form element macros: text, textarea, select, radio etc. I have an array variable that gets passed in that has an elements in it: $elements = array( array( 'type'=>'text, 'value'=>'some value', 'atts'=>null, ), array( 'type'=>'text, 'value'=>'some other value', 'atts'=>null, ), ); {{ elements }} what im

Twig - use quotation mark as separator for join filter

和自甴很熟 提交于 2020-01-13 05:50:46
问题 I pass my template an array of strings which I would like to convert to a jaavascript array: Controller file (php): $myVar = array('a','b','c'); Desired html: var myVar = ["a","b","c"]; I try the following code (twig): var myVar = ["{{ myVar | join('","') }}"]; But the twig generator converts the quotation marks to html entities and this is the result: var myVar = ["a","b","c"]; Some idea? 回答1: You need to apply the raw filter: var myVar = ["{{ myVar | join('","') | raw }}"]; 来源: https:/

How to use the PHP template engine in Twig instead of the Twig syntax within Silex

眉间皱痕 提交于 2020-01-12 18:53:52
问题 In Silex I am able to use Twig templates but I want to use the PHP engine of Twig, instead of the Twig syntax. For example this guide describes how to do it for Symfony but not Silex. My Silex index.php looks like: $app->register(new Silex\Provider\TwigServiceProvider(), array( 'twig.path' => __DIR__.'/views', )); $app->get('/', function() use ($app) { return $app['twig']->render('index.html.php', array( 'name' => 'Bob', )); }); My index.html.php looks like: <p>Welcome to the index <?php echo

How to use the PHP template engine in Twig instead of the Twig syntax within Silex

╄→尐↘猪︶ㄣ 提交于 2020-01-12 18:53:32
问题 In Silex I am able to use Twig templates but I want to use the PHP engine of Twig, instead of the Twig syntax. For example this guide describes how to do it for Symfony but not Silex. My Silex index.php looks like: $app->register(new Silex\Provider\TwigServiceProvider(), array( 'twig.path' => __DIR__.'/views', )); $app->get('/', function() use ($app) { return $app['twig']->render('index.html.php', array( 'name' => 'Bob', )); }); My index.html.php looks like: <p>Welcome to the index <?php echo

Disable HTML escaping when manually rendering a Twig string

时光总嘲笑我的痴心妄想 提交于 2020-01-12 13:47:14
问题 I have the following code that renders a string into HTML output. How can I stop it from escaping the text for HTML? $template = '{{ who }} bar'; $params = array('who' => "Foo's"); $twig = new \Twig_Environment(new \Twig_Loader_String); var_dump($twig->render($template, $params)); Outputs: string(14) "Foo's bar" How can I make it output this instead? string(14) "Foo's bar" I understand that changing '{{ who }} bar' to '{{ who|raw }} bar' will fix the problem, but I want to solve this at the

Symfony2: How to properly include assets in conjunction with Twig template inheritance?

狂风中的少年 提交于 2020-01-12 07:49:06
问题 I'm currently developing a web application using Symfony 2.1.0. I've read through the Templating chapter of the book and I'm trying to include assets (right now, it's just a single stylesheet) in my web pages. I'm using the Three-level inheritance system that is mentioned in the book, and my application structure currently looks like this: app/Resources/views/ base.html.twig: base template, containing title , stylesheets and body blocks. src/My/PageBundle/Resources/views layout.html.twig: