2

I have a template the loops over the Channel entries and displays some basic information about each entry. I'd like to have each item link to more details about that particular entry. It seems that I should be linking to the one of the following: {url_title}, {entry_id}, {comment_entry_id_auto_path}, or {permalink}. The problem is that the resulting values of those variables do not seem like they would map to a template group + template. For example, {permalink} gives me something like this:

http://domain.com/2

(NOTE: I have removed index.php via .htaccess)

Shouldn't the URL be something like this:

http://domain.com/products/2/

Then maybe in my templates /products/index, I do a check on {segment_2} to decide if I should be displaying the details page or not? So where does the details template go?

1 Answer 1

5

The permalink variable will take into account template group and template if you specify them. So for example...

{permalink="products/detail"} 

would give you...

http://example.com/products/detail/1 

...where "1" is the entry id and "detail" is a template in the "products" template group.

But, as far as path variables go, my preference when building links that involve template paths is to use title_permalink (works just like permalink above but uses the url_title instead of the entry_id).

Finally, as you noticed url_title and entry_id don't take into account template group/template paths. They just reference a given entry (either by the url title or the entry id). But they are useful in manually building paths to single entries.

So if you prefer, just use a root-relative path in conjunction with url_title. For example...

/products/detail/{url_title}

As long as you are removing index.php the above works great. I have started to use this more than I do the path variables.


And to answer your other question:

Then maybe in my templates /products/index, I do a check on {segment_2} to decide if I should be displaying the details page or not? So where does the details template go?

Just put your "detail" template in the "products" template group and any time you load a path that matches /products/detail (or /products/detail/etc/etc) that template will be loaded as you'd expect. No extra work is required, ExpressionEngine will do that automatically.

If, instead (as you indicated in your comment), you are wanting to include all the logic and display of a "detail" template in your single index template and forego a second template, then doing a couple simple conditionals like...

{if segment_2 == "detail"}
    detail-template stuff here
{/if}

{if segment_2 != "detail"}
    non detail-template stuff here
{/if} 

...is often enough to accomplish what you described. If it is not, or if more complicated template logic is needed, definitely take a look at Switchee.

5
  • 1
    Solid post. I don't even mess with the path and permalink variables usually. It's just unnecessary parsing. However, there are plenty of good reasons to do it. I just wanted to note that hardcoding the path is such as acceptable IMO (just as you prefer). Commented Dec 29, 2012 at 5:39
  • @alex - if I wanted to forego the "detail" template (meaning, no more "products/detail/" template) and just wanted something like "products/123" where 123 is url_title, etc. -- then I'd just do the {segment_2} check, right? Commented Dec 29, 2012 at 6:11
  • Actually, looking at {segment_2} might not be so straightforward if you consider pagination. How to tell the difference between /products/P2 and /products/123? Commented Dec 29, 2012 at 7:00
  • Doing a simple check like {if segment_2 == "detail"}detail template stuff here{/if} and another like {if segment_2 != "detail"}non-detail template stuff here{/if} is often enough to accomplish what you described. You're absolutely right, though, that if you are doing a check for {segment_2 == ''} you will run into problems with pagination in your index template. In that case, or if more complicated template logic is needed, definitely take a look at Switchee
    – Alex Kendrick
    Commented Dec 29, 2012 at 15:04
  • {if segment_2 == "detail"} and {if segment_2 != "detail"} worked just fine. Thanks. Commented Dec 29, 2012 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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