How to render content from string/database with twig? [duplicate]

see http://twig.sensiolabs.org/doc/functions/template_from_string.html and http://symfony.com/doc/current/cookbook/templating/twig_extension.html#register-an-extension-as-a-service {% include template_from_string(“Hello {{ name }}”) %} {% include template_from_string(page.template) %} Since the string loader is not loaded by default, you need to add it to your config. # src/Acme/DemoBundle/Resources/config/services.yml acme.twig.extension.loader: class: Twig_Extension_StringLoader tags: – { name: ‘twig.extension’ } Where Acme/acme is your application name and DemoBundle is the bundle you … Read more

Use custom delimiters in the current Twig template

As you can see, it’s not recommanded to use this feature Customizing the Syntax BTW here’s a quick and easy example to explain how to use custom delimiters in symfony: service.yml services: templating_lexer: public: true parent: templating.engine.twig class: Acme\YourBundle\Twig\TwigLexerEngine TwigLexerEngine namespace Acme\YourBundle\Twig; use Symfony\Bundle\TwigBundle\TwigEngine; class TwigLexerEngine extends TwigEngine { public function setTwigLexer($lexer) { $this->environment->setLexer($lexer); return … Read more

Symfony 2 load different template depending on user agent properties

Ok, so I don’t have a full solution but a little more than where to look for one 🙂 You can specify loaders (services) for templating item in app/config/config.yml framework: esi: { enabled: true } #translator: { fallback: %locale% } secret: %secret% router: resource: “%kernel.root_dir%/config/routing.yml” strict_requirements: %kernel.debug% form: true csrf_protection: true validation: { enable_annotations: true … Read more

Combining Assetic Resources across inherited templates

You can actually do the following: In layout.html.twig (or whatever your layout is) {% block stylesheets %} {% stylesheets ‘your_assets_here’ %} <link rel=”stylesheet” href=”https://stackoverflow.com/questions/6958970/{{ asset_url }}” /> {% endstylesheets %} {% endblock %} And in any template that extends that layout: {% block stylesheets %} {{ parent() }} {% stylesheets ‘additional_assets_here’ %} <link rel=”stylesheet” href=”https://stackoverflow.com/questions/6958970/{{ … Read more

Twig date difference

Since PHP 5.3 There is another option without to write an extension. This example show how to calc the plural day/days {# endDate and startDate are strings or DateTime objects #} {% set difference = date(endDate).diff(date(startDate)) %} {% set leftDays = difference.days %} {% if leftDays == 1 %} 1 day {% else %} {{ … Read more

Conflict on Template of Twig and Vue.js

Just change default delimiters for vue. Here’s how: Vue.js 1.0 Define delimiters globally (docs). Vue.config.delimiters = [‘${‘, ‘}’] Vue.js 2.0 Define delimiters for component (docs). new Vue({ delimiters: [‘${‘, ‘}’] }) Vue.js 3.0 Define delimiters for application (docs). Vue.createApp({ delimiters: [‘${‘, ‘}’] })