7

I'm trying to make the jump from EE to Craft but struggling in this area (and thinking I may be approaching this all wrong).

In EE, I often use url segments (in combination with Freebie to ignore them) for certain logic in templates (ie. adding a last segment of 'thanks' to show a thank you message).

I cannot figure out how to do this well in Craft - or even if it is the right approach.

I have a channel entry with a slug of 'contact' and want to add 'thanks' to the url to conditionally display a thank you message on that page. I know I could add a route so that 'contact/thanks' will still load the correct template for that channel but I then run into the issue of that URL not matching the entry slug and no entry data is available for {{entry.whateverFields}}.

So then I did this to try to get around the issue by putting all my entry data in a for loop:

{% for entry in craft.entries.section('standard').slug(craft.request.firstSegment) %}
    {{entry.title}}
    ...other template code in here
    {% if craft.request.lastSegment == 'thanks' %}  
        ...thank you message
    {% endif %}
{% endfor %}

This lets me now get the correct entry data loaded for this entry (or others in this section) and my thank you message still appears however it just feels like an awkward way to do it and I suspect in other scenarios it could become really messy.

Is there a better way to approach this in Craft?

Or can you actually have Craft ignore certain url segments?

4

One easy way to work around this is to use a query string instead of a segment. You'd redirect the user to for instance /contact?receipt=thanks instead of /contact/thanks, and you change the conditional in your template to something like this:

{% if craft.request. getParam('receipt') == 'thanks' %}  
    ...thank you message
{% endif %}

I've always felt that this kind of information doesn't belong as a segment-part of an url, and the only reason we did that in EE was the lack of easy access to the query string.

Another way would be to set up a route like you did. Maybe set it to (slug)/thanks, where (slug) is the slug token. Then, at the beginning of your template, before trying to access entry, you check if it exists, and if it doesn't you pull it manually. Like this:

{% if entry is not defined %}
    {% set entry = craft.entries({ slug: craft.request.segment(1) }).first() %}
{% endif %}

Hope one of those solutions helps.

|improve this answer|||||
  • Ah - thanks André. I think your solution re: the slug in the route and checking for the entry is a little cleaner - thanks. And I understand your point re: the querystring - and maybe my 'thanks' example wasn't the best one. I know there are certainly instances where we'd want to leverage 'extra' url segments for some template logic and not rely on a querystring - for pages where SEO considerations came into play or other factors like easy of remembering a URL, etc. – Mike Sep 11 '14 at 17:25
  • Ok, I see, I agree that in other cases it might make more sense to use a segment. – André Elvan Sep 11 '14 at 19:02

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.