Twig template cannot include php template - php

I encountered a problem, which for me is quite not clear and hard to understand. I have tried to make calendar widget, which is supposed to be display on every page on my site. So, I think it should be average template (no arguments, no every site). I tried to do it as twig template. I managed to render calendar, but I had a problem with acquiring date object (which is necessary to get proper arguments for rendering calendar). After a short while, I tried to make a php template, which will be included by main twig template (layout.html.twig). It does not success. I have enabled php engine in config.yml, but does not help - php template is in fact included, but as a normal file, not php file (not parsed as php script), (but php engine is working, I tried to render php template from a controller it works). I have read also it is possible to include a result from another controller in template, but for me it is not proper solution, I have not tried that (php template should fit to this problem).
What should I do to solve this problem?
Main config.yml
imports:
- { resource: parameters.ini }
- { resource: security.yml }
framework:
#esi: ~
translator: { fallback: %locale% }
secret: %secret%
charset: UTF8
router: { resource: "%kernel.root_dir%/config/routing.yml" }
form: true
csrf_protection: true
validation: { enable_annotations: true }
templating: { engines: ['twig', 'php'] }
session:
default_locale: %locale%
auto_start: true
# Twig Configuration
twig:
debug: %kernel.debug%
strict_variables: %kernel.debug%
# Assetic Configuration
assetic:
debug: %kernel.debug%
use_controller: false
# java: /usr/bin/java
filters:
cssrewrite: ~
# closure:
# jar: %kernel.root_dir%/java/compiler.jar
# yui_css:
# jar: %kernel.root_dir%/java/yuicompressor-2.4.2.jar
# Doctrine Configuration
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
orm:
auto_generate_proxy_classes: %kernel.debug%
auto_mapping: true
# Swiftmailer Configuration
#swiftmailer:
# transport: %mailer_transport%
# host: %mailer_host%
# username: %mailer_user%
# password: %mailer_password%
jms_security_extra:
secure_controllers: true
secure_all_services: false
# services:
# TpsaMailer:
# class: Tpsa\TestBundle\Controller\MailerController
layout.html.twig
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
{% block stylesheets %}
<link rel="stylesheet" type="text/css"
href="{{ asset('bundles/tpsablog/css/main.css') }}">
{% endblock %}
{% block javascripts %}
<!-- empty javascripts -->
{% endblock %}
<title>
{% block title %}
{% trans %}blog.programisty.duga{% endtrans %}
{% endblock %}
</title>
</head>
<body>
<div id="all">
<div id="top">
{% block top %}
<div style="float: left" class="right">
<img style="float: left;
vertical-align: middle; margin: 8px
8px 8px 0px" src="{{ asset('bundles/tpsablog/images/glider.png') }}">
<h4>{% trans %}blog.programisty.duga{% endtrans %}</h4>
<div style="font-size: 8px">
{% trans %}ciekawosc.wiedza.niewygodne{% endtrans %}
<!-- Ciekawość i wiedza... To, co jest niewygodne dla
władzy -->
</div>
</div>
<div style="float: right">
{% trans %}strona.glowna{% endtrans %}
{% trans %}o.mnie{% endtrans %}
{% trans %}napisz.do.mnie{% endtrans %}
{% if is_granted('IS_AUTHENTICATED_FULLY') %}
{{ app.user.username }}
{% trans %}tagi{% endtrans %}
{% trans %}wiadomosci{% endtrans %}
{% trans %}wyloguj{% endtrans %}
{% else %}
{% trans %}zaloguj{% endtrans %}
{% endif %}
<div style="text-align: center; margin: 10px 0px">
<a href="{{ path('TBB_rss') }}">
<img src="{{ asset('bundles/tpsablog/images/rss.png')
}}" alt="rss channel">
</a>
<a href="http://www.facebook.com/duga.chernobyl"
target="_blank">
<img src="{{ asset('bundles/tpsablog/images/facebook.png') }}"
alt="facebook">
</a>
<a href="http://www.youtube.com/user/DugaEye"
target="_blank">
<img src="{{ asset('bundles/tpsablog/images/youtube.png')
}}" alt="youtube">
</a>
</div>
</div>
<div style="clear:both"></div>
{% endblock %}
</div>
<div id="frame">
<div id="left">
{% block content %}
{% trans %}TODO{% endtrans %}
{% endblock %}
</div>
<div id="right">
{% block panel %}
<div style="text-align: left">
<div style="text-align: center">
<h4>{% trans %}profil.duga.eye{% endtrans %}</h4>
<img style="width: 100px" src="{{
asset('bundles/tpsablog/images/photo.jpg')
}}">
</div>
<div style="font-weight:900; margin-top: 10px">
<ul>
<li>{% trans %}wiek{% endtrans %}: 21</li>
<li>{% trans %}miejsce{% endtrans %}: /dev/null</li>
<li>{% trans %}zainteresowania{% endtrans %}: {% trans %}programowanie.hacking.filozofia{% endtrans %}</li>
<li>{% trans %}email{% endtrans %}: Mail
</ul>
</div>
</div>
<h3>{% trans %}reklamy{% endtrans %}</h3>
{% include '::calendar.html.php' %}
{% endblock %}
</div>
</div>
<div id="footer">
{% block footer %}
{% trans %}footer{% endtrans %}
{% endblock %}
</div>
</div>
</body>
</html>
How to get proper parameters offset, number, koniec, aktualny from php Date object? (now it is hard coded)
calendar.html.twig
{% include '::calendar.html.php' %}
{% set offset = 1 %}
{% set number = 28 %}
{% set koniec = 7 - ((offset + number) % 7) %}
{% set aktualny = 13 %}
<table border="0" style="text-align: center">
<thead>
<tr>
<td>{% trans %}pn{% endtrans %}</td>
<td>{% trans %}wt{% endtrans %}</td>
<td>{% trans %}sr{% endtrans %}</td>
<td>{% trans %}czw{% endtrans %}</td>
<td>{% trans %}pt{% endtrans %}</td>
<td>{% trans %}sob{% endtrans %}</td>
<td>{% trans %}nie{% endtrans %}</td>
</tr>
<tbody>
{% if offset % 7 != 0 %}
<tr>
{% for i in range(0,offset-1,1) %}<td><br></td>{% endfor %}
{% endif %}
{% for i in 1..number %}
{% if (i+offset)%7 == 1 %}<tr>{% endif %}
<td>
{% if i == aktualny %}
<span style="color: red">{{ i }}</span>
{% else %}
{{ i }}
{% endif %}
</td>
{% if (i+offset)%7 == 0 %}</tr>{% endif %}
{% endfor %}
{% if koniec < 7 %}
{% for i in 1..koniec %}
<td><br></td>
{% endfor %}
</tr>
{% endif %}
</tbody>
</table>
php template which should be executed as php template by including it in twig template , but it is not parsed and executed as the one template.
calendar.html.php
ppp<?php echo ('ala') ?>ooo
Simply includes 'pppooo' in source, not visible being handled as html tag.
deps file if it is needed
[symfony]
git=http://github.com/symfony/symfony.git
version=v2.0.9
[twig]
git=http://github.com/fabpot/Twig.git
version=v1.5.1
[monolog]
git=http://github.com/Seldaek/monolog.git
version=1.0.2
[doctrine-common]
git=http://github.com/doctrine/common.git
version=2.1.4
[doctrine-dbal]
git=http://github.com/doctrine/dbal.git
version=2.1.5
[doctrine]
git=http://github.com/doctrine/doctrine2.git
version=2.1.5
[swiftmailer]
git=http://github.com/swiftmailer/swiftmailer.git
version=v4.1.5
[assetic]
git=http://github.com/kriswallsmith/assetic.git
version=v1.0.2
[twig-extensions]
git=http://github.com/fabpot/Twig-extensions.git
[metadata]
git=http://github.com/schmittjoh/metadata.git
version=1.0.0
[SensioFrameworkExtraBundle]
git=http://github.com/sensio/SensioFrameworkExtraBundle.git
target=/bundles/Sensio/Bundle/FrameworkExtraBundle
version=origin/2.0
[JMSSecurityExtraBundle]
git=http://github.com/schmittjoh/JMSSecurityExtraBundle.git
target=/bundles/JMS/SecurityExtraBundle
version=origin/1.0.x
[SensioDistributionBundle]
git=http://github.com/sensio/SensioDistributionBundle.git
target=/bundles/Sensio/Bundle/DistributionBundle
version=origin/2.0
[SensioGeneratorBundle]
git=http://github.com/sensio/SensioGeneratorBundle.git
target=/bundles/Sensio/Bundle/GeneratorBundle
version=origin/2.0
[AsseticBundle]
git=http://github.com/symfony/AsseticBundle.git
target=/bundles/Symfony/Bundle/AsseticBundle
version=v1.0.1

You cannot mix-and-match twig and php in a single Response (to do so would be circumventing part of the point of twig, which is to prevent designers from creating too much logic in the view).
I think the Symfony documentation should/could be clearer about this (at the moment it basically says "enable them both and do what you like").
If you embed another controller then you should be able to serve up a different Response and that Response can be php based.

NOTE: snippets below are totally non-tested.
http://twig.sensiolabs.org/doc/functions/date.html
The function date seems to create \DateTime object.
{% set now = date() %}
{% set offset = date(now.format('Y/m/01')).format(w) %} {# weekday of 1st day #}
{% set number = now.format('t') %} {# days in month #}
{% set koniec = 7 - ((offset + number) % 7) %}
{% set aktualny = now.format('n') %} {# today #}
However, if you wants to include original php file (say 'calendar.php') in twig,
you have to write extension to get it work.
class CalendarExtension extends \Twig_Extension
{
private $pathToPhp; //store that where the php file is
public function setPhpFile($pathToPhp)
{
$this->pathToPhp = $pathToPhp;
}
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('calendar', array($this, 'showCalendar'))
);
}
public function showCalendar([put arguments here if you need])
{
ob_start();
include ($this->pathToPhp);
return ob_get_clean();
}
}
To make above work, you should create "tagged" service in container.
in app/config/config.yml
services:
calendar_twig_extension:
class: __Namespace__\CalendarExtension
calls:
- [setPhpFile, [__path to your calendar.php__]]
tags:
- [name: twig.extension]
words that double-underscored should be replaced:
_ _ Namespace _ _ : Namespace of CalendarExtension
_ _ path to your calendar.php _ _ : full path to your calendar.php. You can use parameters like %kernel.root_dir% and so on to manage your path project-relative.
With these, you finally can simply write
{{ calendar([arguments for CalendarExtension::showCalendar]) }}

Have you considered rendering your calendar php template by using {% render 'ApplicationBundle:Controller:action' %} and in the action rendering the php template?
You might also render your calendar php in the action that render calendar twig and pass the output of the php template as a simple twig variable.
Note: to display such a var, don't forget to do {{ var|raw }} if there is any html tag inside.
Note2: as of symfony2.2, the render parameter as changed to {% render url('route_name') %}

Related

Twig: Pass Twig Markup to an Include

I want to pass a macro(icon()) to a nested template of mine, through passing the markup as text.
But due to the |raw filter, Twig wont render the markup as Twig. Is there a way to achieve this? Basically I get this rendered {{ icon("phone-call") }}
main.html.twig
{% embed "Block/context_box_column_lane.html.twig" with {
'title': 'Test',
'subtitle': 'Subtitle',
'text': '
<p class="context-box-text">{{ icon("phone-call") }}</p>
'
} %}{% endembed %}
context_box_column_lane.html.twig
{% extends "Block/section_sidebar.html.twig" %}
{% block section_content %}
{% embed 'Block/context-box.html.twig' %}{% endembed %}
{% endblock %}
context-box.html.twig
{% from "#html/macros/icons.twig" import get as icon %}
<div class="b-context-box {% if has_border %}has-border{% endif %}">
{% if subtitle %}
<h3 class="section-subtitle">{{ subtitle|trans }}</h3>
{% endif %}
{% block content %}
{% if text is defined %}
{{ text|trans|raw }}
{% endif %}
{% endblock %}
</div>

Manually switch _locale in symfony 4

I'm absolutely stuck in getting a solution to manually switch the _locale variable in Symfony 4.
I followed these steps, but now I have absolutely no idea how to make a simple switch button in the nav section. I also took a look a this question, but this seems to be an older Symfony version..
Can anyone please help me climb out of this dark hole and explain to me how I can integrate a simple _locale switch button, or at least point me in the right direction?
The answer is slightly different from this answer which is not applicable in Symfony 4. Start with editing the services.yaml file in the config directory.
{# project/config/services.yaml}
# ...
parameters:
# ...
app_locales: [nl_NL, en_EN]
twig:
# ...
globals:
locales: %app_locales%
# ...
Then add a template to integrate the switch button somewhere in your base template.
{# project/templates/_locale_switcher.html.twig #}
{% set route = app.request.attributes.get('_route') %}
{% set route_params = app.request.attributes.get('_route_params') %}
{% set params = route_params|merge(app.request.query.all) %}
{# You may want to not print a flag/link for current view, the "if" here let
you handle it #}
{% for locale in locales if locale != app.request.locale %}
<li>
<a href="{{ path(route, params|merge({ _locale: locale })) }}">
<img src="{{ asset('img/flags/' ~ locale ~ '.jpg') }}" alt="{{
locale }}">
</a>
</li>
{% endfor %}
And finally integrate this brandnew template in your base template.
{# project/templates/base.html.twig #}
{% include '_locale_switcher.html.twig' %}
EDIT for Symfony 4.3.4+
As per the answer of Charles beneath, the locales value in services.yaml file should be inserted with quotes to avoid an unvalid YAML error:
{# project/config/services.yaml}
# ...
parameters:
# ...
app_locales: [nl_NL, en_EN]
twig:
# ...
globals:
locales: "%app_locales%"
# ...
As of latest Symfony (5.3.9), I strongly suggest one follows the documentation:
https://symfony.com/doc/current/the-fast-track/en/28-intl.html
Towards the end, do a bit of tweaking to generate a nice dropdown menu correctly based on your available languages:
<ul class="navbar-nav me-auto mb-2 mb-md-0">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false" id="locales">
<i class="fa fa-globe" aria-hidden="true"></i>
{{ app.request.locale|locale_name(app.request.locale)|u.title }}
</a>
<ul class="dropdown-menu" aria-labelledby="locales">
{% for locale in locales|split('|') %}
<li>
<a {% if app.request.locale == locale %}
class="dropdown-item active"
{% else %}
class="dropdown-item"
{% endif %}
href="{{ path(app.request.get('_route', 'app_index'), app.request.get('_route_params', [])|merge({_locale: locale})) }}">
{{ locale|locale_name(locale)|u.title }}
</a>
</li>
{% endfor %}
</ul>
</li>
</ul>
PS: The above is using Bootstrap 5.
Here is what works for me on latest symfony 4 version
services.yaml:
parameters:
locale: 'en'
app_locales: en|fr
twig:
globals:
locales: '%app_locales%'
locale template:
{# project/templates/_locale_switcher.html.twig #}
{% set route = app.request.attributes.get('_route') %}
{% set route_params = app.request.attributes.get('_route_params') %}
{% set params = route_params|merge(app.request.query.all) %}
{# You may want to not print a flag/link for current view, the "if" here let
you handle it #}
{% for locale in locales|split('|') %}
{% if locale != app.request.locale %}
<li>
<a href="{{ path(route, params|merge({ _locale: locale })) }}">
<img src="{{ asset('img/flags/' ~ locale ~ '.jpg') }}" alt="{{ locale }}">
</a>
</li>
{% endif %}
{% endfor %}

Symfony FileType customize

I'm trying to customize the rendering of the File Input in Symfony with twig.
plugins.krajee.com/file-basic-usage-demo
I follow the documentation : https://symfony.com/doc/current/form/form_customization.html
In my config.yml
# Twig Configuration
twig:
form_themes:
- 'bootstrap_3_layout.html.twig'
I try all methode...
1) Custome inside the the view in my view.html.twig (this is a copy past of the existing bootstrap layout, I just add class="file")
{% form_theme form _self %}
{% block _user_file_widget -%}
{% if type is not defined or type not in ['file', 'hidden'] %}
{%- set attr = attr|merge({class: (attr.class|default('') ~ ' form-control')|trim}) -%}
{% endif %}
{%- set type = type|default('text') -%}
<input class="file" type="file" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %}/>
{%- endblock _user_file_widget %}
And in my FileType
$builder->add('imageFile', FileType::class, array(
'required' => false,
'label' =>false,
'block_name' => 'file',
));
That gives me an error
The merge filter only works with arrays or "Traversable", got "NULL" as first argument.
2) Create an external file
In Ressources/View/Form/fields.html.twig
{% block form_widget_simple -%}
{% if type is not defined or type not in ['file', 'hidden'] %}
{%- set attr = attr|merge({class: (attr.class|default('') ~ ' form-control')|trim}) -%}
{% endif %}
{%- set type = type|default('text') -%}
<input class="file" type="file" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %}/>
{%- endblock form_widget_simple %}
And in the view
{% form_theme form 'form/fields.html.twig' %}
But all fields are customized...
3) Add class in the rendering in the view
{{ form_widget(form.imageFile, {'attr': {'class': 'file', 'type': 'file'} }) }}
And for this 2 last methods I get this:
I am open to all solution !
Thank you for your help.
I found it !
It's working as I want using kartik-v/bootstrap-fileinput.
After installation, I add some assetic in my config.yml
kartik_js:
inputs:
- "%kernel.root_dir%/../vendor/kartik-v/bootstrap-fileinput/js/fileinput.js"
- "%kernel.root_dir%/../vendor/kartik-v/bootstrap-fileinput/js/locales/fr.js"
- "%kernel.root_dir%/../vendor/kartik-v/bootstrap-fileinput/themes/explorer/theme.js"
kartik_css:
inputs:
- "%kernel.root_dir%/../vendor/kartik-v/bootstrap-fileinput/css/fileinput.min.css"
- "%kernel.root_dir%/../vendor/kartik-v/bootstrap-fileinput/themes/explorer/theme.css"
Then in the view I add the css and js
{% block stylesheets %}
{{ parent() }}
{% stylesheets '#kartik_css' combine=true %}
<link href="{{ asset_url }}" type="text/css" rel="stylesheet" />
{% endstylesheets %}
{% endblock %}
{% javascripts '#kartik_js' combine=true %}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
The form widget
{{ form_widget(form.imageFile)}}
And the Javascrip
$("#fos_user_profile_form_imageFile").fileinput({
language: 'fr',
showUpload: false,
allowedFileExtensions: ['jpg', 'png', 'gif']
});
The Result !
Is Amazing
I hope this will help some one :)
Have a good day !

Remove percent sign on a percent field type

I've been googling around but I couldnt find the way to remove the percent sign that the Form Component (Symfony) adds to the percent field type when rendering a form. I've found something related to redefined the percent widget but I couldn't get it how this works, neither where to change it.
This is what I found:
You can overwrite the percent_widget block from the default layout to
something like this:
{% block percent_widget %}
{% spaceless %}
{% set type = type|default('text') %}
{{ block('form_widget_simple') }}
{% endspaceless %}
{% endblock percent_widget %}
My current rendering code inside the template is:
<div class="form-group">
{{ form_label(edit_form.discountRate, 'Discount', {'label_attr':{'class':'col-sm-2 control-label'}}) }}
<div class="col-sm-10">
<div class="input-group">
{{ form_widget(edit_form.discountRate, {'attr': {'class':'form-control'}}) }}
<div class="input-group-addon">%</div>
</div>
</div>
</div>
PD: Using the input-group class from boootstrap 3, I append that percent sign the way I like to render it, but how I delete the one that appends Symfony?
You have to override the default form theme of symfony 2.
First, open your app/config/config.yml file, and edit the twig configuration to have something like this :
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
form:
resources:
- YourBundleName:Form:my-form-theme.html.twig
(Its the form.resources part which is important). This configuration will tell Symfony 2 to use your custom theme.
You can also use {% form_theme form 'YourBundleName:Form:my-form-theme.html.twig' %} in your template if you don't want to add something in the config file, but I don't like to add this line in all my forms so I prefer to add it in global configuration.
Now you have to create a file src/YourBundlePath/Resources/views/Form/my-form-theme.html.twig with the following content :
{% extends 'form_div_layout.html.twig' %}
{% block percent_widget -%}
{% set type = type|default('text') %}
{{- block('form_widget_simple') -}}
{%- endblock percent_widget %}
And this should work.
If you open the theme file of Symfony 2 (/vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig) you can see that there is :
{% block percent_widget -%}
{% set type = type|default('text') %}
{{- block('form_widget_simple') -}} %
{%- endblock percent_widget %}
I removed the "%" on the 3d line in the override.
Regards
You cant remove it, its one of cons of symfony form % char is hardcoded
{% block percent_widget -%}
{% set type = type|default('text') %}
{{- block('form_widget_simple') -}} %
{%- endblock percent_widget %}
You can write your own block, where You will remove %
You just have to override the form theme in the template. There are several ways to do it.
I'm going to use the quickest.
Your template would end up like this:
{% form_theme edit_form _self %}
{% block percent_widget %}
{% spaceless %}
{% set type = type|default('text') %}
{{ block('form_widget_simple') }}
{% endspaceless %}
{% endblock percent_widget %}
<div class="form-group">
{{ form_label(edit_form.discountRate, 'Discount', {'label_attr':{'class':'col-sm-2 control-label'}}) }}
<div class="col-sm-10">
<div class="input-group">
{{ form_widget(edit_form.discountRate, {'attr': {'class':'form-control'}}) }}
<div class="input-group-addon">%</div>
</div>
</div>
</div>
Well no need to rewrite templates, the idea is to make a front removal with jQuery/JS. Most of the time your input is embedded in a div, and twig is generating something you like this :
<div class="a">
<input ...>
%
</div>
'%' text can be accessed and removed with native JS lastChild/removeChild functions :
$('.a').removeChild($('.a').lastChild) (jQuery/JS mix)
document.getElementsByClassname(a).removeChild(document.getElementsByClassname(a).lastChild) (plain vanilla JS)

Symfony2 locale in route

I followed Symfony2 doc http://symfony.com/doc/2.0/book/translation.html#the-locale-and-the-url and added locale to my routes. But, I can't find the way to carry locale through routes as I place {{ path('myroute') }} in twig template, but locale always gets fallback value instead taking current locale.
I tried {{ path('myroute', {'_locale': _locale}) }} but I got error "Variable "_locale" does not exist".
Any idea?
Fixed with {{ path('address', {'_locale': app.request.attributes.get('_locale')}) }} thanks to this thread http://www.mail-archive.com/symfony-users#googlegroups.com/msg34838.html.
In Symfony2.1 the locale is stored in the request, so you have to use this:
{{ path('address', {'_locale': app.request.locale}) }}
Two pages:
localhost.lo/xx/about
localhost.lo/xx/hello/{name}
where xx - several locales described in routing.yml
-- routing.yml
home:
resource: "#JetInformBundle/Resources/config/routing.yml"
prefix: /{_locale}
requirements:
_locale: ^en|de|ru|uk|pl$
-- JetInformBundle routing.yml
hello:
pattern: /hello/{name}
defaults: { _controller: JetInformBundle:Default:index, name: 'alexander' }
about:
pattern: /about
defaults: { _controller: JetInformBundle:Default:about }
-- DefaultController.php
<?php
namespace Jet\InformBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller
{
public function indexAction($name, Request $request)
{
return $this->render('JetInformBundle:Default:index.html.twig',
array('name' => $name, 'matches' => $this->matchAction($request)));
}
public function aboutAction(Request $request)
{
return $this->render('JetInformBundle:Default:about.html.twig',
array('matches' => $this->matchAction($request)));
}
protected function matchAction(Request $request)
{
return $this->get('router')->match($request->getRequestUri());
}
}
-- index.html.twig
{% extends '::base.html.twig' %}
{% block body %}
<h1>{{ 'hello.name'|trans }} {{ name }}!</h1>
<h3>{{ 'your.locale'|trans }} [{{ app.request.get('_locale') }}]</h3>
{% include 'JetInformBundle:Default:locales.html.twig'
with {
'uripath': 'hello',
'params': {
'name': app.request.get('name')
}
}
%}
{% include 'JetInformBundle:Default:matches.html.twig'
with { 'matches': matches } %}
<div>
<p>{{ 'return.to'|trans }} About</p>
</div>
{% endblock %}
-- about.html.twig
{% extends '::base.html.twig' %}
{% block body %}
<h1>{% trans %}about.page{% endtrans %}</h1>
<h3>{% trans %}your.locale{% endtrans %} [{{ app.request.get('_locale') }}]</h3>
{% include 'JetInformBundle:Default:locales.html.twig'
with { 'uripath': 'about', 'params': {}} %}
{% include 'JetInformBundle:Default:matches.html.twig'
with { 'matches': matches } %}
<div>
<p>{% trans%}return.to{% endtrans%} Hello</p>
</div>
{% endblock %}
-- locales.html.twig
{% if not params %}
{% set params = {} %}
{% endif %}
<div class="langs">
<ul>
<li>
{% if app.request.get('_locale') == 'ru' %}
Русский
{% else %}
Русский
{% endif %}
</li>
<li>
{% if app.request.get('_locale') == 'en' %}
English
{% else %}
English
{% endif %}
</li>
<li>
{% if app.request.get('_locale') == 'uk' %}
Украiнська
{% else %}
Украiнська
{% endif %}
</li>
<li>
{% if app.request.get('_locale') == 'de' %}
Deutsch
{% else %}
Deutsch
{% endif %}
</li>
<li>
{% if app.request.get('_locale') == 'pl' %}
Polish
{% else %}
Polish
{% endif %}
</li>
</ul>
</div>
-- matches.html.twig
<h5>Matches</h5>
<ol>
{% for key, value in matches %}
<li>{{ key }} : {{ value }} </li>
{% endfor %}
</ol>
Shorthand notation:
{{ path('address', {'_locale': app.session.locale}) }}

Categories