Coding Implode In Twig
Asked Answered
C

1

11

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?

Curbing answered 7/11, 2012 at 9:47 Comment(1)
I'm not sure I really get your question. I'm assuming you're talking about the Twig Template Engine for PHP, and want to assign the concatenated strings to a new variable? In that case it would be something like {% set result = data1 ~ ", " ~ data2 %}. But then I don't really get why you posted your question with '$'s in front of the variable names... If just want to output the two string, you could always do something like {{data1}}, {{data2}}Upright
E
41

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 #}
Elata answered 9/11, 2012 at 10:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.