Take the 2-minute tour ×
ExpressionEngine® Answers is a question and answer site for administrators, end users, developers and designers for ExpressionEngine® CMS. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

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.

share|improve this answer
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). –  Justin Kimbrell Dec 29 '12 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? –  ExpressionEngineNewbie Dec 29 '12 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? –  ExpressionEngineNewbie Dec 29 '12 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 Dec 29 '12 at 15:04
    
{if segment_2 == "detail"} and {if segment_2 != "detail"} worked just fine. Thanks. –  ExpressionEngineNewbie Dec 29 '12 at 17:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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