How do I get the fullpath for sonatamediabundle??
I can get the full path normally
{{ app.request.schemeAndHttpHost() ~ path('path_name') }}
howevere sonatamediabundle is like this
{% path pic.getMedia, 'big' %}
Is it possible to get the fullpath with the statement???
Related
So i am working on a Shopware shop, and i want to read a MediaEntity in Twig. To do so, i am creating a string with the node path (adding the product ID as a variable), which just works fine.
To actually access the MediaEntity, i need to convert this string into a real node path. How do i do that? Or is there maybe another way to create this path?
Here's my code:
{% block component_product_box %}
{{ parent() }}
{% set coverIds = "context.extensions.#{product.coverId}.elements" %}
{{ dump() }}
{% endblock %}
I tried it roughly and something like this should work:
{% set coverIds = _context['extensions'][product.coverId]['elements'] %}
This should solve your problem, I hope.
If you really need to work with a string and "dots" notation, this could be of help:
How to use Twig's attributed function to access nested object properties
I have a string in twig that needs to be replaced with some words.
{% set myvar = 'Hello World' %}
{% set firstvar = 'Hello' %}
{% set secondvar = 'Test' %}
If I want to replace Hello with the test below code doesn't work.
{{ myvar|replace({"{{firstvar}}":"{{secondvar}}"}) }}
My expected output is : Test World
What is the wrong with the code. I ve searched everywhere but I couldn't find it.
Is it possible to call an executable function inside a Twig conditional statement?
I have a path function and want to output the path if the name variable is empty. Right now I have these two options:
{% path file, 'reference' %} // calling path function
{{ file.name ?: file.path }} // Conditional
But I would like something like:
{% file.name ?: path file, 'reference' %}
Looks like path is a tag instead of a function. If it was a function, you'd use it like this:
{% path(file, 'reference') %}
In comparison, Twig has a function dump, and in Symfony you can use a tag with the same name. Here's how you'd use them:
{{ dump(foo) }} {# function #}
{% dump foo %} {# tag #}
You see the difference?
If path was a function, both of these would probably be possible:
{{ file.name ?: path(file, 'reference') }}
{% do file.name ?: path(file, 'reference') %}
Both are the same, except the second one doesn't print anything.
Because path seems to be a tag, I don't think it's possible to do what you asked. (It's also possible that it's both a tag and a function. If that's the case, use the function instead of the tag.)
Edit: Are you using Symfony? There's a Twig function path in Symfony, but I don't think there's a Twig tag path. Are you sure your code ({% path file, 'reference' %}) is correct?
Is there an equivalent to this in Twig :
<?php
$a = 'hello';
$b = '<h1>'.$a.'</h1>';
echo $b;
?>
I try this but without success :
{% set a = 'hello' %}
{% set b = <h1>{{a}}</h1> %}
I'm new to twig and couldn't find a way to do this.
To concatenate in Twig you need the tilda symbol, ~
Eg.
{% set a = 'hello' %}
{% set b = '<h1>'~a~'</h1>' %}
Or you can use string interpolation
E.g.
{{ "<h1>{a}</h1>" }}
You can concatenate and dump with the raw filter:
{% set a = 'hello' %}
{% set b = '<h1>' ~ a ~ '</h1>' %}
{{ b|raw }}
Here a working solutions
How would you append more data to the same variable in Twig? For example, this is what I'm trying to do in Twig:
var data = "foo";
data += 'bar';
I have figured out that ~ appends strings together in Twig. When I try {% set data ~ 'foo' %} I get an error in Twig.
The ~ operator does not perform assignment, which is the likely cause of the error.
Instead, you need to assign the appended string back to the variable:
{% set data = data ~ 'foo' %}
See also: How to combine two string in twig?
Displaying dynamically in twig
{% for Resp in test.TestRespuestasA %}
{% set name = "preg_A_" ~ Resp.id %}
{% set name_aux = "preg_A_comentario" ~ Resp.id %}
<li>{{ form_row(attribute(form, name)) }}</li>
{% endfor %}
You can also define a custom filter like Liquid's |append filter in your Twig instance which does the same thing.
$loader = new Twig_Loader_Filesystem('./path/to/views/dir');
$twig = new Twig_Environment($loader);
...
...
$twig->addFilter(new Twig_SimpleFilter('append', function($val, $append) {
return $val . $append;
}));
Resulting in the following markup:
{% set pants = 'I\'m wearing stretchy pants!' %}
{% set part2 = ' and they\'re friggin\' comfy!' %}
{% set pants = pants|append(part2) %}
{{ pants }}
{# result: I'm wearing stretchy pants! and they're friggin' comfy! #}
IMHO I find the above sample more intuitive than the ~ combinator, especially when working on a shared codebase where people new to the syntax might get a bit mixed up.