Twig - if one of array value in array - twig

I try to do something like this, anyone knows how to handle it ?
this is what i currently have :
{{ ('1' in app.request.pathInfo or '2' in app.request.pathInfo or '3' in app.request.pathInfo) ? 'active' : '' }} X
this is what i want :
{{ (['1','2','3'] in app.request.pathInfo) ? 'active' : '' }} V

Inverse logic works ;)
{{ app.request.pathInfo in ['frame_brand', 'frame_elevation', 'frame_model'] ? 'active' }}

You can try this:
array_intersect(['1','2','3'], app.request.pathInfo);

Related

Twig concatenation syntax

I'm struggling to find the correct Twig concatenation syntax.
Here's what I've got:
{{ this.page.baseFileName|page({ (__SELF__.pageParam): page }) }}
it gives the url with the pagination number at the end of the url:
http://sites.local12/category/style/rock/2
Now I want to insert a colon before the pagination number like this:
http://sites.local12/category/style/rock/:2
How to achieve that ?
Ok I've got it. I was confused by the "page" filter (I'm new to Twig).
{{ this.page.baseFileName|page( {(__SELF__.pageParam): ':' ~ page }) }}

Translation of a Label containing an url

In my symfony3 project, I have a form field with label "I accept the cgu" with a link on the 'cgu'.
How can I translate the label containing the link, as I must generate the link with {{path('')}} in twig ?
I tried something like that :
{{ form_row(form.valid, {'label' : 'annonces.form.valide_cgu_cgv' | trans ({'cgu_link' : {{ path('page_statique', {'page' : 'cgu'})}}, 'cgv_link' : {{ path('page_statique', {'page' : 'cgv'})}} }) |raw }) }}
but it does not work...
Any idea ?
Thanks all !
When you are under {{ }}, you're writting an expression, so you can't put nested {{ }}.
You can try with:
{{
form_row(form.valid, {
'label' : 'annonces.form.valide_cgu_cgv' | trans({
'cgu_link' : path('page_statique', {'page' : 'cgu'})
'cgv_link': path('page_statique', {'page' : 'cgv'})
})
})
}}

reuse variable as string

This question maybe kind of silly but I'm a newbie for symfony anyway.
prescenario I pass a variable from controller into index.html.twig by doing this
return $this->render('index/index.html.twig', [ 'department'=>$departments,'URILink'=>$URILink,'departmentDetail'=>$departmentDetails,'contentCell'=>$this->mContentCell ]);
After using {% dump %} it shows me
"department" => array:3 [▶]
"URILink" => "http://localhost/index/department/"
"departmentDetail" => array:1 [▶]
"contentCell" => "department.html.twig"
Then I need to reuse the variable contentCell as string in template to form syntax similar to this ;
<div>{{ include ('department.html.twig'),[departmentDetail:departmentDetail]</div>
For my first attempt I tried this,
<div> {{ include ({{ContentCell}}),[departmentDetail:departmentDetail]}} </div>
Unfortunately it showed me this error
A hash key must be a quoted string, a number, a name, or an expression
enclosed in parentheses (unexpected token "punctuation" of value "{".
Any idea how could I use the variable
contentCell as string value appropriately?
try this:
{% include contentCell with { departmentDetail : departmentDetail} %}
Answer here pass data to twig
You can include a template like this per:
{{ include('YourBundle:ControllerName:yourAction.html.twig', {'variableName': yourData}) }}
Or like this per http://twig.sensiolabs.org/doc/tags/include.html
{% include 'template.html' with {'foo': 'bar'} %}
after try'n try at last I found the the trick how to do it
i take similar analogy for
{{dump(var)}}
so attempt to do this
{{include (contentCell,{'departmentDetail' : departmentDetail}) }}
and it work like charm :) nice

Symfony Translations

i'm using the last version of symfony (3.1.3)
i want to trans multi vars.
but i didn't find and good solution
right now i'm doing:
{% block h1 %}{{ 'service.create'|trans }} {{ ('service'|trans) }}{% endblock %}
I try :
{% block h1 %}{{ 'service','service.create'|trans }} }} { %endblock %}
but no luck.
I also try with
{% trans %}service.create|service{% endtrans %}
thanks
You can use parameters in your translation like this:
{{ 'service.create'|trans }}
{{ 'service.create'|trans({'%separator%': 'any text'}) }}
And in your messages.ru.yml
...
service:
create: ... %separator% ...
...
If your separator is in html, add raw like this :
{{ 'service.create'|trans({'%separator%': '<br>'})|raw }}
You have more details in symfony doc here
You can translate strings in backend:
public function indexAction($name)
{
$translated = $this->get('translator')->trans('Hello '.$name);
return new Response($translated);
}
Or in twig templates:
<h1>{{ 'service.create'|trans }}</h1>
Each time you create a new translation resource (or install a bundle that includes a translation resource), be sure to clear your cache so that Symfony can discover the new translation resources:
php bin/console cache:clear
More info: http://symfony.com/doc/current/translation.html
I needed to just trans multi vars.
twig not support that...
If you have a "service.yml.en" file or something like that :
{{ service.create|trans({}, "service") }}
Here the translator will search in your service.yml.en file and load the correct translation key.
If you want to do advance translations, you can use vars like that (for example) :
{{ (className|lower ~ "." ~ field)|trans({}, className|lower) }}
Here if you have a "user.yml.en" and you want to load something like "user.width", it'll work.
Good luck :)

What is the idea of | in twig

I have a question, I have this code :
{% set texte_article = 'Simple text' %}
{% set url_article = 'simple/url' %}
What is the idea of text_article|twitter_share..., I don't understand what do |. Can you help me please ? Thx in advance
And what is the difference between : {{ 40|lipsum }} and {{ lipsum(40) }} ?
the filter method is :
public static function getShareLink($s_url)
{
$a_params = array(
'url' => 'url',
'hl' => 'share'
);
return self::URL . http_build_query($a_params, '', '&');
}
| is to apply a Twig fliter.
I guess you have a twitter_share_link function in your project which need url_article as parameter

Resources