I want to add a new parameter in the app.request from twig.
I have tried with this but am not getting the result I expected:
{% set app.request.query.set('script') = false %}
You should never change the request object in a Twig template. I don't think it's even possible actually.
If you just want to add a parameter and use it in the current template, just do something like this:
{% set parameters = app.request.query.all()|merge({'script': false}) %}
Related
I need help in including in a twig file, another twig file
For example, I have:
_cdn/widgets/filter/filter.php
_cdn/widgets/filter/view/filter.twig
And:
themes/tema02/view/search.twig
themes/tema02/search.php
I need to include the filter.twig in the search.twig
But when I use include, it does not render the filter.php
And it gets a lot of 'Undefined variable:'
So how do I do this?
First of all show your code . It's always easier to give right anwser when you see the code.
Use {% include %} for including twig in twig
https://twig.symfony.com/doc/2.x/tags/include.html
Based on your description you need to pass requred variables to included twig , something like this
{% include 'filter.twig' with {'foo': 'bar'} %}
OR
In filter twig you can put if ... is defined where you try to access variable
{% if foo is defined %}
code where you doing something with foo variable
{% endif %}
Hi guys its rather a very basic question, had chance to look several questions on stackoverflow but all in vain.
so i have this twig variable called "WordoftheDayfromDB ", to which i am passing from some data after querying DB in my controller via laravel pluck method. The controller exsits in plugin of octobercms. the content of the variable is shown below
{% set WordoftheDayfromDB = __SELF__.words %}
{{WordoftheDayfromDB}} # this output below object
["{\"id\":4,\"word_tr\":\"parazit\",\"slug_tr\":\"parazit\",\"word_gr\":\"\\u03c0\\u03b1\\u03c1\\u03ac\\u03c3\\u03b9\\u03c4\\u03bf\",\"slug_gr\":\"parasito\",\"pubordraft\":1,\"created_at\":\"2017-06-07 13:04:57\",\"updated_at\":\"2017-06-07 13:04:57\",\"deleted_at\":null,\"word_image\":\"\\\/cropped-images\\\/image2.jpg\",\"typeswb_id\":0}"]
can someone tell me a way to extract keys and values from the about twig variable.
what i already tried is following:
<pre> {{WordoftheDayfromDB.id}}</pre>
or
{% for item in WordoftheDayfromDB %}
{{item.word_tr}}
{% endfor %}
also some combination using {% if WordoftheDayfromDB is iterable %}.
I will appreciate your answer very much!
thank you for reading my question.
You can use the for loop so that the keys and values are both accessible like this:
{% for key, value in WordoftheDayfromDB %}
<li>{{ key }}: {{ value }}</li>
{% endfor %}
So the answer is rather complex then even i anticipated! i had to do a lot of digging with frustration to really get at the bottom of this matter.
First thing first, i was doing a cron job where i saved the data from a model in text type field. That is why if you see above result i.e
{% set WordoftheDayfromDB = __SELF__.words %}
{{WordoftheDayfromDB}} # this output below object
["{\"id\":4,\"word_tr\":\"parazit\",\"slug_tr\":\"parazit\",\"word_gr\":\"\\u03c0\\u03b1\\u03c1\\u03ac\\u03c3\\u03b9\\u03c4\\u03bf\",\"slug_gr\":\"parasito\",\"pubordraft\":1,\"created_at\":\"2017-06-07 13:04:57\",\"updated_at\":\"2017-06-07 13:04:57\",\"deleted_at\":null,\"word_image\":\"\\\/cropped-images\\\/image2.jpg\",\"typeswb_id\":0}"]
it outputs a JSON String, too bad can't iterate or do something with it.
To solve this,
Create json_decode filter in twig.
Apply the filter to value part of array.
Access Individual values of array with variable[keyname] method.
I created a twig filter json_decode
for creating filter see this Link
while in October, the creation to new twig extension is rather easy which is just give registerMarkupTags method in Plugin.php with filter array poiting to name and function name. See this link for extending twig in octobercms here
Now, the part we were waiting for, how to get the values and show them in twig template. Here it is going to be, by using above same example. This is what i did
{% set wordoftheday = __SELF__.words %}
{% for key, value in wordoftheday %}
{% set decoded = value|json_decode %}
# to get the indvisual values
{{ decoded['id'] }}
{{ decoded['created_at'] }}
{% endfor %}
I'm attempting to do something that was very easy in PHP, but not so easy in twig.
Basically, I need to call a class method, but I need to be able to define the method to call via a string.
I have 3 methods: getControlvs, getControlnc and getControltr. However, in order to call these methods, I need a seperate variable which determines which one to call.
This is what I'm attempting to call right now:
{% set neutPer = key.getControl~neutFaction %}
Where neutFaction can be either "vs", "nc", or "tr".
This only seems to fire key.getControl and then that's it, the concatenation is lost.
Any ideas?
Thanks in advance!
You can use the attribute twig function:
First concatenate the string as:
{% set method = "getControl" ~ neutFaction %}
Then call as
{{ attribute(key, method) }}
You can pass arguments also as:
{{ attribute(key, method, arguments) }}
In addition, the defined test can check for the existence of a dynamic attribute:
{{ attribute(object, method) is defined ? 'Method exists' : 'Method does not exist' }}
Try {% set neutPer = (key.getControl~neutFaction) %}
Or you could change this in your controller/service that passes the variable to twig so that you do not need to setup a concatenation.
Instead of doing this rendering of each slide in my TWIG like this (see line 6):
{# loop out the slides #}
{% for c in contents %}
{% set i=i+1 %} {# increase slide number #}
<div id="slide{{ i }}" class="slide" style="z-index:{{ i }};">
{# the slide itself, rendered by it's own template #}
{% include 'BizTVArchiveBundle:ContentTemplate:'~c.template~'/view.html.twig' with {'contents': c, 'ordernumber': i} %}
</div>
{% endfor %}
...Instead I would like to keep all of that logic in the controller, to just deliver to the view an array of ready slides. How can I do something like this (see line 9):
//do stuff...
foreach ($container->getContent() as $c) {
$content[$i]['title'] = $c->getTitle();
$content[$i]['sort'] = $c->getSortOrder();
$content[$i]['data'] = $c->getData();
$content[$i]['template'] = $c->getTemplate()->getId();
$content[$i]['duration'] = $this->extract_seconds($c);
$content[$i]['html'] = $this->render('BizTVArchiveBundle:ContentTemplate:'.$content[$i]['template'].'/view.html.twig', array(
'contents'=> $content[$i],
'ordernumber' => 99,
));
}
All it gives me at the moment (the contents of $content[$i]['html']) is
{"headers":{}}
Within a twig template, you can set the value of a printed variable like this:
{% set rendered %}
{{ var_to_print }}
{% endset %}
You can get content form rendered object like this:
$this->render('BizTVArchiveBundle:ContentTemplate:Some.html.twig', array())->getContent();
It's better if you include the template in the template, this way you keep the templating rendering in the view layer and the logic in the controller layer.
But well, if you really want it...
You can use 2 services to do that: twig is using the Twig_Environment or templating.engine.twig which is a templating layer build in Symfony2 for Twig, this can be easily switched ot templating.engine.php.
If you use the twig service, you can see the twig docs on how to use it:
$template = $this->get('twig')->render('/full/path/to/Resources/views/'.$content[$i]['template'].'/view.html.twig', array(...));
If you use the templating.engine.twig service, see the templating docs on how to use it, which is almost exact the same as the Twig_Environment.
That is because $this->render() returns a Response object.
Instead of $this->render(), use $this->renderView().
I want to use few php string functions in twig. For e.g.
How to write below code in twing?
if (!empty($message) && substr_count($message, 'blabla')) {
....
....
}
You can't, and that's the whole point of twig.
Template should only take care of displaying data.
In that specific case, you could create a TWIG variable, and simply pass TRUE or FALSE, so that you twig code would look like:
{% if display_message %}
...
{% enfif %}
You can look at the list of expressions that are available in the twig template, strstr_count is not part of them :( http://twig.sensiolabs.org/doc/templates.html#expressions
Use filters:
{% if message is not empty and ... %}
...
{% endif %}
I think there is not a substr_count equivalent for twig, you can either make the test and pass the result to the template or create a twig extension and implement it yourself
http://twig.sensiolabs.org/documentation