Coding Implode In Twig - twig

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 #}

Related

How to define "ends with any two letters after underscore" in Twig

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.

Twig RAW filter on literal string not working

{{ (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

Dotliquid: value from string

Is there a way to get the value from a string?
For example:
"SomeString" has the value "Edward".
Input:
{% assign variable = 'SomeString' %}
{{ variable }}
Output:
SomeString
Note: SomeString is a constructed string during runtime, so I in fact need to get the value from a string --> I can't remove the quotes in the assignment.
There is nothing in DotLiquid which allows to do that, however it is always possible to create your own Tag or to build the template at run time.
public sealed class RuntimeAssign : Tag
{
...
}
Template.RegisterTag<RuntimeAssign>("runtimeassign");

Strange Twig behaviour when using "in" operator

Today I noticed strange behaviour then trying to check if array has value.
I tried to do {% if key in array|keys %} ... {% endif %}
and condition was equal to true always.
I tried to do this later: {{ dump('a' in [0, 1, 2]) }}.
And guess what value was dumped? It was "true" somehow.
Do you guys have any idea why is it happening?
I can workaround it by using 'a' in [0, 1, 2]|join but that's not what I want to figure out.
It's not twig, it's php. The following code:
var_dump(in_array('a', array(0, 1, 2)));
prints:
bool(true)
When comparing a string to an int, the string gets converted to int. In this case, 'a' becomes 0, thus matching one of the array keys.
Try doing a var_dump("foobar" == 0) and you'll see it is true as well.
You can use foo['a'] is defined instead, demo here: http://twigfiddle.com/y126mg

Twig - use quotation mark as separator for join filter

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 }}

Resources