How can i replace some chars (Goal is a simple double slash to one slash) in a connected twig string?
{{ config_basehost ~ navigationElement.imgSrc }} // Connect 2 Strings
Replace works like:
{{ config_basehost|replace({"a": "b"}) }} // Replace all "a" with "b"
But how can i replace something in a connected string?
{{ {{ config_basehost ~ navigationElement.imgSrc }}|replace({"a": "b"}) }} // Output: http://example.com/img/cats.jpg|replace({"a":"b"})
As you see, the replace is at the end of my "generated" URL. Same as:
{{ config_basehost ~ navigationElement.imgSrc }}|replace({"a": "b"}) // Without bracers
The double slashes only occures connecting string 1 with string 2. So, string 1 has a slash at last position inside the string, and string 2 at first position. I could replace the last char or the first char from one of these strings, yeah. But that's not the question :)
{{ (config_basehost ~ navigationElement.imgSrc)|replace({"a": "b"}) }} - try this.
Use brackets. Simple :)
{{ STRING|replace("en": "ar") }}
Replaces all occurrences of 'e' and 'n' in a string
{{ STRING|replace({"en": "ar"}) }}
Replaces all occurrences of 'en' with ar in a string
Related
I am trying to print this string
str = "container.insert({{ {0} ,{{ {{ {1}, {{ {2} ,{3} ,{4} , {5} }} }} }} }});"
str.format(prim_ident,country_ident,final[0],final[1],final[2],final[3])
fileWrite.write(str)
However the output of above that I get is
container.insert({{ {0} ,{{ {{ {1}, {{ {2} ,{3} ,{4} , {5} }} }} }} }});
Two problems the first problem is that i am getting double curly braces. I only want to show a single curly brace. But i read that you have to use double curly braces when u would like the curly brace to be present in the string the other problem is my format is not working (i.e) {0},{1} etc are not being replaced by their equivalent values. Can anyone please tell me what I am doing wrong ?
str.format() does modify the string in-place, you still need to save it to a variable, try adding str = :
str = str.format(prim_ident,country_ident,final[0],final[1],final[2],final[3])
In the following example, this condition includes all type elements of an array that do not include _.
{% for type in array %}
{% if '_' not in type) %}
Instead, I would like to include all elements that do not end with _any2letters, where "any2letters" is actual any 2 letters. I examined Twig documentation and wasn't able to find the required syntax.
It was solved using:
{% if not (type matches '/_[a-z]{2}$/') %}
This solution takes advantage of PHP regex syntax.
"/" are used in Twig to define borders of regular expressions.
"_" is my underscore as is; [a-z]{2} means "2 lowercase letters".
And finally $ means that the preceding symbols are in the end of the
string.
{{ (vendorData.description) ? vendorData.description : "<em>No Description Entered</em>"|raw }}
When the value is not present I see:
<em>No Description Entered</em>
Printed literally on the screen in the web browser.
Raw should force the characters to be literal, not > < etc.
Why does this not work on a "created string" but if I do it on a string variable it works?
You need to place brackets around the whole statement like so:
{{ ((vendorData)
? vendorData
: "<em>No Description Entered</em>")|raw }}
Here is a working twigfiddle to show it working:
https://twigfiddle.com/fs2oc2
You can use twigfiddle to experiment with your code.
From feedback in comments section:
here is a twig example to show what you need: https://twigfiddle.com/hjyslr
How can I check if a string variable is null or empty, or full with space characters in Twig? (Shortest possible, maybe an equivalent to CSharp's String.IsNullOrWhiteSpace() method)
{% if your_variable is null or your_variable is empty %}
should check whether the variable is null or empty.
If you want to see if it's not null or empty just use the notoperator.
{% if foo is not null and foo is not empty %}
See the docs:
empty
null
"is" operator
logical operators like "not"
Perhaps you might be interested in tests in twig generally.
There are already good answers, but I give my 2 cents too:
{% if foo|length %}
I was inspired by #GuillermoGutiƩrrez's filter trick.
But I think |length is safer as the "0"|trim expression will evaluates to false.
References :
length
codepad
boolval
I'd rather use just trim and empty:
{% if foo|trim is empty %}
{% if foo|trim is not empty %}
empty evaluates to true if the foo variable is:
null
false
empty array
empty string
{% if foo|trim %} seems to be enough (assuming that foo is the variable to check). If foo is not null, trim removes whitespaces. Also, if handles empty string or null as false, and true otherwise, so no more is required.
References:
trim
How can one use the equivalent of the implode function in a Twig environment?
For example, the contents of the variable $data1 = "input your name" and the variable $data2 = "input your address".
How to create a variable $result = "input your name, input your address" in Twig?
I'm guessing you're looking for the join filter. It works exactly like implode does in php. Quoting from the manual page:
The join filter returns a string which is the concatenation of the items of a sequence:
{{ [1, 2, 3]|join }}
{# returns 123 #}
The separator between elements is an empty string per default, but you can define it with the optional first parameter:
{{ [1, 2, 3]|join('|') }}
{# returns 1|2|3 #}