5

I am trying to form a url parameter (for Google Maps API) from entry field data. Basically, this is what I would like to do:

{% set street = entry.address %}

{% set  city = entry.city %}

{% set urlParam = street.", ".city.", UT"  %}

Is this possible with Twig and, if so, what is the proper syntax for concatenating multiple variables and strings?

Thanks!

  • Can you clarify the title to say something along the lines as to where you are try to do this, such as "in a template". – Jason McCallister Nov 27 '14 at 19:30
9

Another option is to use the twig join filter:

{% set urlParam = [street, city, 'UT']|join(', ') %}

join concatenates the items in the sequence with the separator of your choice in between.

  • 1
    For things like addresses I usually use |filter|join(', ') so there are no repeated commas if any of the fields are null or empty. – Mike Pepper Nov 25 '14 at 20:58
  • Thanks, Marion - that seems to make a lot more sense than my approach! – Roi Agneta Nov 26 '14 at 22:29
  • mark me correct? – Marion Newlevant Nov 27 '14 at 19:31
8

Figured it out! Proper syntax is:

{% set urlParam = street ~", " ~city ~", UT"  %}
2

Just wanted to add another example to this since this post help point me in the right direction.

In my example, I am getting back a list of events from a 3rd party. All that is returned to me is the slug. But, you can see that I wanted the url to be prepended with /event-results/.

<a href="{{ url('/evnet-results/'~event.slug) }}">{{ event.title }}</a>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.