I'm attempting to pass an array to a parameter in a twig path.
I need to do include two values for the same key.
I've attempted to do something like this
path('path_name', {'status': array('req', 'ord')}) }}
but twig does not seem to like arrays in paths.
I've tried a number of other options. This doesn't err but of course the second key-value overwrites the first
path('path_name', {'status': 'req', 'status': 'ord'}) }}
It doesn't seem to like this either:
path('path_name', {('status':'req') and ('status': 'ord')})
Any suggestions? I'm hoping to avoid rewriting my methods and stay DRY
For simple lists, you need to use [] to declare your array in Twig.
The following expression will add both parameters to your query string:
{{ path('path_name', {'status': ['req', 'ord']}) }}
The generated path will include status[0]=req&status[1]=ord in its query string.
Related
Someone can explain this?
In twig, I need to detect if certain controller is loaded. Depending on it, it should display a HTML block or another.
I have this comparison in twig template:
{% if app.request.attributes.get("_controller") == 'App\Controller\DefaultController::index' %}
Even when controller and action is actually "App\Controller\DefaultController::index" expression evaluates to false showing the block intended to be shown when the controller is other.
Besides explanation, I need to solution please.
by the way, when I print the controller name, using
{{ app.request.attributes.get("_controller") }}
I can check this strange behaviour.
Regards
Jaime
Twig is reading the backslashes in your string as escape characters. To make it work you must escape the backslashes. Like this:
'App\\Controller\\DefaultController::index'
Lets say I have this:
{%trans%}html.string{%endtrans%}
that prints this string
This is a string with some <i>html</i> tags.
I want to apply the Twig filter striptags to obtain this:
This is a string with some html tags.
As you can see I have "removed" the italic tags.
So I need a way to do something like:
{%trans%}html.string|striptags{%endtrans%}
Obviously this is not the correct way of applying filters to the translated string, so, how can I do this?
{{"html.string"|trans|striptags}}
note that you always have to apply the trans filter first.
I am attempting to use the EvaluateJsonPath processor in Nifi, and am having trouble with the jayway jsonpath syntax.
My object looks like the following:
{"text":"my stuff", "tags":["abc", "xyz", "beq"]}
I want to route messages based on the tags - I want everything containing "xyz" to be routed one way, and everything not containing it to be routed another way.
Using http://jsonpath.herokuapp.com/ I've been testing and trying to figure out the syntax to filter based on a json object containing an array of strings matching. I can match based on overt index (so $.[?(#.tags[1] =~ /xyz/i)] works just fine), but I can't guarantee the order or number of objects in the tags field.
Is there a way to do this in the jayway json module? I saw filter the Json according to string in an array in JSONPATH which I've tried, but it doesn't appear to work in the simulator above.
I do not know how to do this in one EvaluateJsonPath processor step. But it can certainly be done in a two-step process:
Use EvaluateJsonPath to filter "xyz" tags out of the tags array, using a JsonPath expression like $.tags[?(# =~ /xyz/i)] and setting the processors return-type to json so an array may be returned. This will result in ["xyz"] for a match and [] for non-matching files
Use RouteOnAttribute to route based on the resulting array, with an expression like ${matchingTags:toLower():contains('xyz')}.
It might also be worth considering evaluating the JSON as text against a regular expression to match the tag.
Is there a way to replace a GET parameter value from twig?
For example, I have a page at this address:
http://localhost/app_dev.php/test/?param1=40&sort=name
And in my twig I want to build 3 links like this:
http://localhost/app_dev.php/test/?param1=40&sort=name
http://localhost/app_dev.php/test/?param1=40&sort=address
http://localhost/app_dev.php/test/?param1=40&sort=code
For now I added the "&sort" parameter once again at the end on the URL, but this solution is actually a "patch" and it sucks!
address
In this example I only have 2 parameters, but in reality I have around 6 parameters, because the link that's generated it's obtained by submitting a .
This should solve your problem:
{{ path(app.request.attributes.get('_route'),
app.request.query.all|merge({'sort': 'address'})) }}
It gets the current route and all query parameters which are merged with the one you like to update before they are appended.
Symfony/Twig path function accept optional params. If these params are part of the route, they're handled by router but if they're not, they are passed as GET parameters.
So, if your corresponding route is, for example, my_route :
address
i checked out the documentation for the replace filter in twig. my problem is that, suppose i have a variable say contvariable, and the content passed through that variable from the controller is dynamic
return $this->render('RodasysFormstudyBundle:Default:addclientname.html.twig', array('contvariable' =>$sometext));
this $sometext variable will contain texts like
$sometext='%Sun% rises in the East';
the text inside the %% should be displayed as an input field in the browser. I did not find any examples in the web like to replace the content inside the %% (what ever the content be whether its sun or the moon). Is this possible to do this using replace filter or should i follow some other method such as to replace the content in controller before sending it to twig..
please help..
You could do something like that (with the 'raw' filter) :
{{ "%foo% rises in the East"|replace({'%foo%': "<input type='text' name='"~foo~"' value='"~foo~"'/>"})|raw }}
foo is a variable sent by your controller, with the value of your choice.