Override twig variable in included template in Symfony 3 - symfony

I'm trying to override a variable in the included template.
Can I do this in a Symfony3 & Twig?
My twig template looks like this:
{% set foo = 'bar' %}
{% include 'first.html.twig' %}
{% include 'second.html.twig' %}
// first.html.twig
{{ foo }}
{% set foo = 'second' %}
// second.html.twig
{{ foo }}
I get such a result:
bar bar
But I expect:
bar second

The following Twig code:
{% set a = 42 %}
{{ include("first.twig") }}
Will be compiled to this one:
// line 1
$context["a"] = 42;
// line 2
echo twig_include($this->env, $context, "first.twig");
And twig_include prototype is:
# lib/Twig/Extension/Core.php
function twig_include(Twig_Environment $env, $context, $template, $variables = array(), $withContext = true, $ignoreMissing = false, $sandboxed = false)
So variables are passed by copy, not by reference in included templates. Thus your changes in included templates won't be reflected to including templates.
Moreover, since Twig 2.0, you can't call TwigEnvironment::addGlobal once twig runtime is initialized, so you can't glitch using simple extensions.
All in all, you can understand that if you need to update variables cross templates, it means some template contains business logic and Twig isn't build for that. You need to prepare the whole context in controllers.

Alternatively you may call a PHP class method from TWIG. Example of a page-counter needed when generating a pdf.
Custom class :
class PageCounter
{
private $pageNumber = 0;
public function incrementPageCounter()
{
$this->pageNumber ++;
return $this->pageNumber;
}
}
Controller:
....
$twigVariables = [
...
'pageCounter' => new PageCounter()
];
return $this->render('template.html.twig', $twigVariables);
Twig template (object pageCounter available from any included template)
{{ pageCounter.incrementPageCounter() }} / {{totalPages}}

You just need to do the check and override the variable with another variable :))
{% if name is defined %}
{% set foo = name %}
{% else %}
{% set foo = 'bar' %}
{% endif %}
{% include 'first.html.twig' %}
{% include 'second.html.twig' %}
// first.html.twig
{% set name = 'first' %}
// second.html.twig
{% set name = 'second' %}

Why not override your variable with your include tag/function like :
{% include 'first.html.twig' with {'foo': 'second'} %}
or :
{ include('first.html.twig', {foo: 'second'}) }}

Related

OroPlatform: override oro_datetime_widget options

Context
I am actually trying to change the default placeholder for the time input of the OroDateTimeType::class.
I want to have, for example, the text Horaires instead of Temps.
Here is my form field in my FormType :
->add('expirationDate', OroDateTimeType::class, [
'label' => 'app.subscription.fields.expirationDate',
])
And in my twig view :
form_row(form.expirationDate)
Issue
At the beginning, I have tried to used the Symfony 4 placeholder option for DateTime type : https://symfony.com/doc/4.4/reference/forms/types/date.html#placeholder. It doesn't work because OroDateTime use a different datepicker and it override the Symfony values on load :
{# vendor/oro/platform/src/Oro/Bundle/FormBundle/Resources/views/Form/fields.html.twig #}
{% block oro_datetime_widget %}
{% set dateValidation = {'Date' : {}} %}
{% set timeValidation = {'Time' : {}} %}
{% if required %}
{% set dateValidation = dateValidation|merge({'NotBlank' : {}}) %}
{% set timeValidation = timeValidation|merge({'NotBlank' : {}}) %}
{% endif %}
{% if attribute(attr, 'class') is defined %}
{% set attrClass = attr['class'] %}
{% else %}
{% set attrClass = '' %}
{% endif %}
{% set options = {
view: 'oroui/js/app/views/datepicker/datetimepicker-view',
nativeMode: isMobileVersion(),
dateInputAttrs: {
placeholder: 'oro.form.choose_date'|trans,
id: id,
name: id,
class: 'input-small datepicker-input ' ~ attrClass,
'data-validation': dateValidation|json_encode(constant('JSON_FORCE_OBJECT')),
'aria-live': 'assertive',
autocomplete: 'off',
autocorrect: 'off',
autocapitalize: 'off'
},
datePickerOptions: {
altFormat: 'yy-mm-dd',
changeMonth: true,
changeYear: true,
yearRange: years|default('-80:+1'),
showButtonPanel: true
},
timeInputAttrs: {
placeholder: 'oro.form.choose_time'|trans,
id: 'time_selector_' ~ id,
name: 'time_selector_' ~ id,
class: 'input-small timepicker-input ' ~ attrClass,
'data-validation': timeValidation|json_encode(constant('JSON_FORCE_OBJECT'))
},
timePickerOptions: {
}
} %}
{% set id = 'hidden_' ~ id %}
{% set attr = attr|merge({
'data-page-component-module': 'oroui/js/app/components/view-component',
'data-page-component-options': options|json_encode(constant('JSON_FORCE_OBJECT'))
}) %}
{{ block('datetime_widget') }}
{% endblock oro_datetime_widget %}
If I change the value timeInputAttrs.placeholder from the options variable. It works.
But, I want to pass this variable to my specific form field, not globally.
UPDATE
I finally choose to change the oro.form.choose_time translation in my project globally.
So, in my Resources/translations/messages.fr_FR.yml I've created these lines :
oro:
form:
choose_time: Horaires
auth:
description:
main: Baltimore
Then, I've understand that translations are generated in a file located in var/cache/dev/translations/catalogue.fr_FR :
<?php
use Symfony\Component\Translation\MessageCatalogue;
$catalogue = new MessageCatalogue('fr_FR', array (
'messages' =>
array (
'oro.form.choose_time' => 'Temps',
'oro.auth.description.main' => 'Baltimore',
Here, I can see that the oro.auth.description.main change is applied, but the value for the key oro.form.choose_time is still the same.
Maybe I have a command to run ?
The easiest way to override any text in the Oro application UI is to override a translation for the message used to render it. As the form placeholder is translated as well, you can use this technic. If it's the only customization you need for the form, follow this guide.
If you want to override an HTML, you can extend the template by following the template overriding guide.
But, as you want to modify the label for a single form, then the best way would be to extend the form type and override the single form field with new options.

Set Twig layout in controller

I have multiple subdomains, each with its own layout. Some controllers are shared across subdomains (e.g login), some not. What I'd like to do is to set layout according to domain, so that I would not need to write in each template:
{% if app.request.domain == 'one' %}
{% set layout = '::layout-one.html.twig' %}
{% elseif app.request.domain == 'two' %}
{% set layout = '::layout-two.html.twig' %}
...
{% endif %}
{% extends layout %}
Is it possible to set default layout in controller (or somewhere)? E.g:
class FooController
{
function fooAction()
{
...
$templating = $this->get('templating');
$templating->setLayout($layout);
return $templating->renderResponse($view, $parameters, $response);
}
}
If you have a separate config file for each of the domains, you can put the layout config in there and have it available in twig as a global variable:
config_one.yml
twig:
globals:
base_layout: '::layout-one.html.twig'
Then in twig you can just do:
{% extends base_layout %}
You can set layout variable in your FooController:
class FooController
{
function fooAction()
{
...
return $this->render($template, array(
'layout' => $layout
));
}
}
And then use it in your template:
{% extends layout %}

Import a file with constants in Twig?

Is it possible to import a file with a bunch of constants in another twig file?
Such as :
{% set VAR1 = constant("...:var1") %}
{% set VAR2 = constant("...:var2") %}
{% set VAR3 = constant("...:var3") %}
...
And then, in another file use those constants?
I have tried include, import, use and embed.
None of these seem to fit the situation.
By design, you can't import variables from another context, as {% include %} without parameter will pass your current context by copy, not by reference.
This is the exact same thing for {% macro %} with the _context variable as parameter, so you cannot create new variables in the context from inside a macro and then {% import %} your macro file anywhere.
But, you can create variables from an extended file, and use them in the child file.
For example:
a.html.twig
{% set test = 'Hello!' %}
b.html.twig
{% extends 'a.html.twig' %}
{{ test }}
Will display "Hello!" when rendering b.html.twig, or any other files that extends a.html.twig.
If, by design, you can't extends a parent template in your application, you can create a Twig extension and implement the getGlobals() method (doc).
Example (for me in Fuz/TestBundle, replace all namespaces to fit your needs):
The Twig extension :
PHP Fuz/TestBundle/Twig/Extension/AppGlobalsExtension.php
<?php
namespace Fuz\TestBundle\Twig\Extension;
class AppGlobalsExtension extends \Twig_Extension
{
public function getGlobals()
{
return array(
'var1' => \Fuz\TestBundle\Component\Service::SOME_CONSTANT_A,
'var2' => \Fuz\TestBundle\Component\Service::SOME_CONSTANT_B,
'var3' => \Fuz\TestBundle\Component\Service::SOME_CONSTANT_C,
// ...
);
}
public function getName()
{
return 'appglobals';
}
}
The configuration :
YML Fuz/TestBundle/Resources/config/services.yml
parameters:
fuz_tools.twig.appglobals_extension.class: Fuz\TestBundle\Twig\Extension\AppGlobalsExtension
services:
fuz_tools.twig.appglobals:
class: '%fuz_tools.twig.appglobals_extension.class%'
tags:
- { name: twig.extension }
You can now use {{ var1 }}, {{ var2 }} ... anywhere in your application views.
This should do the trick for you (with with), just pass variables:
{% include "ACMETestBundle::test.html.twig" with {var1: VAR1, var2: VAR2, ... } %}

Dynamic twig variable names

Given an array of variables sent to a twig template, such as:
$form = $this->createForm( new ServiceItemType()
, $entity
, array( 'attr'=>
array(
'em' => $this->EM()
,'group' => true
) ) );
I want to capture the variables for easy access in twig. However:
{% for key, value in form.vars.attr %}
{% set key = value %}
{% endfor %}
remaps the key variable in the for loop.
twig objects to:
{% for key, value in form.vars.attr %}
{% set {{key}} = value %}
{% endfor %}
And stack as I am aware never seems to address set. Would anyone who knows, please indicate how to accomplish this variable assignment?
I know this syntax works
{% render "..." with {(key): value} %}
Did you try the following syntax? As of March, Friday 22nd this syntax didn't work so you need to use a work around.
{% set (key) = value %}
An alternative to that would be to include a template and pass and form.vars.attr.
{% include "YourAwesomeBundle:Controller:template.html.twig" with form.vars.attr %}
You can also merge form.vars.attr with another array using the merge function.
{% set vars = {} %}
{% set vars = vars|merge(form.vars.attr) %}
{% include "YourAwesomeBundle:Controller:template.html.twig" with vars %}
Within the included template you will be able to use the variable em and group.

Symfony 'trans' domain inside Twig template

I'd like to do this:
$this->get('translator')->trans('notice.unregistered', array(), 'index');
Inside Twig template, so I don't have to pass this as an argument. How?
You can also do using trans filter :
{{ 'translationkey'|trans({},'domain') }}
The solution is:
{% trans from "domain" %}text{% endtrans %}
You can add custom functions to change domains inside your templates.
Add your functions:
$getTextdomain = new Twig_SimpleFunction('get_textdomain', function () {
return textdomain(NULL);
});
$setTextdomain = new Twig_SimpleFunction('set_textdomain', function ($domain) {
textdomain($domain);
});
$twig->addFunction($getTextdomain);
$twig->addFunction($setTextdomain);
Then use it:
{% set originalDomain = get_textdomain() %}
{{ set_textdomain('errors') }}
{% trans "My error message" %}
{{ set_textdomain(originalDomain) }}

Resources