I have the folowing long variable in Twig to read the src attribute of an image in RSS feed:
<img src="{{item.get_item_tags("http://www.w3.org/2005/Atom","link")[0]['child']['http://search.yahoo.com/mrss/']['content'][0]['child']['http://search.yahoo.com/mrss/']['thumbnail'][0]['child']['http://www.w3.org/2005/Atom']['img'][0]['attribs']['']['src']}}"/>
I want to make the code more cleaner and readable, so initially, I defined two variables to act as parameter of get_item_tags() and the other as a path for the array:
{% set param1 = 'http://www.w3.org/2005/Atom' %}
{% set arrayPath = '[0]['child']['http://search.yahoo.com/mrss/']['content'][0]['child']['http://search.yahoo.com/mrss/']['thumbnail'][0]['child']['http://www.w3.org/2005/Atom']['img'][0]['attribs']['']['src']' %}
What I want to get, but it generate error, is:
<img src="{{item.get_item_tags({{param1}}, "link"){{arrayPath}}}}" />
I don't use symfony and Twig version is 1.16.0
When you set a variable in twig and want to use it inside another function either in {{ }} or {% %} you do not need to use {{ }} again for the set variable.
Furthermore, you cannot set a variable as index of another variable then concat them; so you need to change it to:
{% set param1 = 'http://www.w3.org/2005/Atom' %}
{% set output = item.get_item_tags(param1, "link") %}
{% set yk = 'http://search.yahoo.com/mrss/' %}
<img src="{{ output[0]['child'][(yk)]['content'][0]['child'][(yk)]['thumbnail'][0]['child']['http://www.w3.org/2005/Atom']['img'][0]['attribs']['']['src'] }}" />
Related
I need to run a google images scraper depending on one of the twig variables. How can i execute the following code inside my .twig file.
{$google_image = new get_google_image_class;
$google_image->destination = '.'; // directory to save thse images
$google_image->limit = 1; // limit the number of images, 0 for random
$google_image->display = true; // output images to the page
$google_image->GetImage($manufacturer);
echo '<p>Retrieved images: '.$google_image->retrieved.'</p>';
}
I need to run it with the following variable {{ manufacturer }} :
<img alt="{{ manufacturer }}" src= "www.antonio.ecommercefull.com/catalog/controller/product/{{ manufacturer }}-1.jpg">
You can try:
{% set google_image = new get_google_image_class %}
{% $google_image.destination = '.' %}
{% $google_image.limit = 1 %}
{% $google_image.display = true %}
{% $google_image.GetImage($manufacturer) %}
<p>Retrieved images: {{ $google_image.retrieved }} </p>
But I think you should write code PHP in file Controller. And using:
<p>Retrieved images: {{ $google_image.retrieved }} </p>
As not expected I'm able to access non-witelisted functions.
Policy config:
$policy = new \Twig\Sandbox\SecurityPolicy(); // should disallow all
$sandbox = new \Twig\Extension\SandboxExtension($policy);
$twig->addExtension($sandbox);
Twig template:
{{ include(template_from_string(data), sandboxed=true) }}
where data contains the external code beeing restricted:
{% if totalAmount %}
total: {{ totalAmount|number_format }}
{% endif %}
As I understand the not very detailed docs, I shouldn't have access to either if, totalAmount and number_format. How to restrict that?
To enable a global sandbox mode u'd need to pass true as the second argument of the SandboxExtension constructor otherwise you'd need to use the {% sandbox %}-tag in places where you don't trust the code that is ran.
$policy = new \Twig\Sandbox\SecurityPolicy();
$sandbox = new \Twig\Extension\SandboxExtension($policy, true);
$twig->addExtension($sandbox);
{% sandbox %}
{% include(template_from_string(data) %}
{% endsandbox %}
I am trying to pass query string parameters through my url to a node twig template (node--template.html.twig) I have tried
{% set queryParams = app.request.query.all %}
However, nothing is showing when passed.
You are almost there.
{% set queryParams = app.request.query.all %}
This statement will not show anything. Because it is only supposed to assign query variables array to queryParams
If you want to display, you have multiple ways after the above statement.
For example, if you just want to display the value of user_id query variable from URL.
{% set queryParams = app.request.query.all %}
{{ queryParams["user_id"] }}
Another example, if you want to loop through all query variables:
{% set queryParams = app.request.query.all %}
{% for key, value in queryParams %}
{{ key }} => {{ value }}
{% endfor %}
Hi how use this part of code with twig,
$this->assign('title', 'Home');
not
echo $this->assign('title', 'Home');
I tried,
{% set assign = ('title', 'Home') %}
{% set this.assign = ('title', 'Home') %}
{% set assign = {'title', 'Home'} %}
{{ assign('title', 'Home') }}
But still don't work
Thanks you
I don't know about using $this in the context of a template (it would refer to some generated class instance), but you can perform arbitrary operations without printing by using the do statement.
The do tag works exactly like the regular variable expression ({{ ... }}) just that it doesn't print anything:
{% do 1 + 2 %}
To access the view itself when using TwigView, use the _view variable:
{% do _view.assign('title', 'Home') %}
How to call a variable's method in expression syntax in twig tempting system.
see the example below
{{ myObj.someMethod() }} {# this print the output of this method #}
i don't want above code because its printing output of this method.
but i want this
{% myObj.someMethod() %}
but its is giving me error that
Unknown tag name "myObj" in "
{% myObj.someMethod() %}" at line 2
even with above error method is also being called.
In twig syntax {{ some variable}} will print the result in variable you need to set variable and then use where you want
{% set myvar = myObj.someMethod() %} /* this will store the result returned from function */
{{ myvar }} /* this will print the result in myvar */