3
1.{% set workTitle = craft.request.getLastSegment()  %}
2.{% if currentUser %}
3.     {{workTitle}}
4.{% else %}
5.  {% redirect "/visit/index?artwork=workTitle" %}
6.{% endif %}

Each line of code are working fine except the line 5. I set a value on line 1. Line 3 returns the set value but on line 5 when am trying to redirect it doesn't return correct path. Redirect returns the below path instead of the returning the set value of workTitle.

http://abc.dev/visit/index?artwork=workTitle

6

I think:

{% redirect "/visit/index?artwork="~workTitle %}
3
  • Beat me to it👍 – Selvin Ortiz Apr 10 '17 at 6:26
  • Apparently not. @SelvinOrtiz I think you had me by 3-4 seconds. Might want to add the initial "{" to your code. – AbbeyDesign Apr 10 '17 at 6:30
  • And you took the time to explain what you were doing, mine was a limp "I think". Sad. – AbbeyDesign Apr 10 '17 at 6:32
5

Update line 5 to concatenate the path/URL with the variable workTitle.

{% redirect "/visit/index?artwork=" ~ workTitle %}

2
  • FWIW: You can also interpolate the variable, like this: {% redirect "/visit/index?artwork=#{workTitle}" %}. This syntax might be preferable if you have many variables to include in the query params. – August Miller Feb 4 '18 at 23:58
  • Also… the url Twig helper might come in handy! {% redirect url('/visit/index', { artwork: workTitle }) %}. The added benefit here is that it discards null values, and keeps your URLs nice and trim. – August Miller Feb 5 '18 at 0:00

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.