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])
Related
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 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
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 #}
I pass my template an array of strings which I would like to convert to a jaavascript array:
Controller file (php):
$myVar = array('a','b','c');
Desired html:
var myVar = ["a","b","c"];
I try the following code (twig):
var myVar = ["{{ myVar | join('","') }}"];
But the twig generator converts the quotation marks to html entities and this is the result:
var myVar = ["a","b","c"];
Some idea?
You need to apply the raw filter:
var myVar = ["{{ myVar | join('","') | raw }}"];
Maerlyn's answer will work, however the drawback with it is that the values in the myVar array will be outputted raw too, which, depending on the origin of that variable, could lead to vulnerabilities in your website, such as XSS.
I found two ways to do this while maintaining the escaping on the array values. The first is using a loop with an if statement to check whether it's the last iteration to determine whether we need to output the "glue" used in the join or not:
var myVar = [{% for val in myVar %}"{{ val }}"{% if loop.last == false %},{% endif %}{% endfor %}]
The second way is to let PHP your PHP handle everything, including the escaping, and then output the raw string in Twig:
$arr = array_map(
function($value) {
return '"' . htmlspecialchars($value, ENT_QUOTES, 'UTF-8') . '"';
},
$arr
);
$myVar = '[' . implode(',', $arr) . ']';
Then pass the $myVar variable to your view, and then you can just do:
{{ myVar|raw }}