Twig concatenation syntax - twig

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 }) }}

Related

Fetch url parameters in twig

this is my URL:
192.168.0.101/user1/opc_autopart/upload/index.php?route=product/category&path=20
I want to fetch path parameter and below is my code
{{ app.request.get('path') }}
This code is not working
You may use:
{{ app.request.query.get('path') }}
to get the path query string in twig.
You would better define a default value, in case there is no path parameter:
{{ app.request.query.get('path')|default('1') }}

How to put a dynamic custom attribute in HTML(PUG) in VueJS 2?

I am using VueJs 2 with Pug and I have the following code:
.mb-3(v-for="(payments, index) in historyPayments" :key="index" no-body='')
b-btn(variant="default" block='' href='#' v-b-toggle.accordion2016="") // I need to change accordion2016 using a dynamic variable
| {{ index }}
In my iterator I have data that I would like to put as an attribute in an HTML tag.
The current attribute I have is: v-b-toggle.accordion2016, but I would like to change it to something like in the following code:
.mb-3(v-for="(payments, index) in historyPayments" :key="index" no-body='')
b-btn(variant="default" block='' href='#' "`v-b-toggle.#{index}`"="")
| {{ index }}
How could I change the attribute dynamically using Pug? I have tried but I get syntax error.
You should use v-bind like this:
v-bind="{ [`v-b-toggle.${index}`]: '' }"
By using ES6 template string syntax and single element array, you can bind a variable in an attribute. Finally you only need to use v-bind to bind attrs dinamically in components.
You could use v-b-toggle to toggle a value and have your href as a computed value which is returned based on your toggled value
I.E:
computed: {
computedHref () {
return toggledValue ? "first-href-link" : "second-href-link"
}
}
And just use computedHref value in your href like so
b-btn(variant="default" block='' href='computedHref')
If you use bootstrap vue, then you can do it like this:
.mb-3(v-for="(payments, index) in historyPayments" :key="index" no-body)
b-btn(variant="default" block href='#' v-b-toggle="`accordion${index}`")
| {{ index }}

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

How to get route link in Slim3 Twig?

I defined my route so:
$app->get('/about', function ($request, $response, $args) {
return $this->view->render($response, 'about.twig');
})->setName('about.page');
I'm interested to get route link by name like: {% get_route('about.page') %}
How can I achieve this?
In Slim3 there is the path_for(name) function. F.ex:
{{ path_for('about.page') }}
Reference: http://www.slimframework.com/docs/features/templates.html
The slim/twig-view component exposes a custom path_for() function to your Twig templates. You can use this function to generate complete URLs to any named route in your Slim application. The path_for() function accepts two arguments:
1 A route name
2 A hash of route placeholder names and replacement values
Note: The path_for uses the function of the router which is $router->pathFor(..)
Why not to use {%
{% is a control structur in twig, Message: Unknown "path_for" tag in "base.twig" at line XX. is displayed because there is no such tag as control structur defined so twig doesn't know that this is actually a function.
So use the output structur in twig {{.
Is this possible?
Yes.
IIRC
{{ path_for('about.page') }}
Reference:
https://github.com/slimphp/Twig-View/blob/master/src/TwigExtension.php#L37

urlencode in twig path function

I have this route:
_view_tag:
pattern: /topic/{tid}
defaults: {_controller: "MyCoreBundle:ViewTag:index" }
And I want to show url like this: example.com/topic/Web+development. I use href="topic/{{ topicname|url_encode() }}". It is works, but of course it is not proper way, so I change to href="{{ path('_view_tag', {'tid': topicname|url_encode() } ) }}". But it is not showing example.com/topic/Web+development, it shows example.com/topic/Web%2Bdevelopment.
I also try this:
{% set _tid = topicname|url_encode() %}
<a href="{{ path('_view_tag', {'tid': _tid } ) }}" ...
But still not working
My question is, how to make it show example.com/topic/Web+development using twig path function?
The path function takes care of url-encoding for you. Your problem is that your space got encoded twice: first to a +, then that got converted to %2b. This will work:
path('_view_tag', { 'tid': topicname } )

Resources