ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Is this correct way to use conditionals and segment variables together?

December 20, 2011 7:23am

Subscribe [2]
  • #1 / Dec 20, 2011 7:23am

    bgarrant

    356 posts

    I have a template that I want to use as a parent template (personal-finance/index template with no segment_2 in URL and fixed entry_id=“11”) and I also want to use it if there is an value in URL (segment_2).  I need to prevent the use of segment_3 as it should not ever be used by the client as I am building a 2 level navigation menu.  Is this correct way to code the conditionals?  It seems to work, but looking for advise if this is a preferred method.

    {if segment_3 != ""}{redirect="404"}{/if}
    
    {if segment_1 == "personal-finance" && segment_2 == ""}
    {exp:channel:entries channel="pages" entry_id="11" disable="categories|member_data|pagination" limit="1" dynamic="no"}
    {if no_results}{redirect="404"}{/if}
    {simple_content_body}
    {/exp:channel:entries}
    {/if}
    
    {if segment_1 == "personal-finance" && segment_2 != ""}
    {exp:channel:entries channel="pages" disable="categories|member_data|pagination" require_entry="yes" limit="1"}
    {if no_results}{redirect="404"}{/if}
    {simple_content_body}
    {/exp:channel:entries}
    {/if}
  • #2 / Dec 20, 2011 7:55am

    Boyink!

    5011 posts

    Looks fine to me.

  • #3 / Dec 20, 2011 8:48am

    bgarrant

    356 posts

    Looks fine to me.

    Thanks Michael.  Your book was what got me into EE for the first time.  Now after about 12 projects I am feeling pretty confident, but I am trying to build sites without using to many add-ons.  My biggest issue is allowing clients to create static pages.  I used to setup a single template for client created pages, but the whole Pages URI thing just caused a mess.  Then I tried using the single template with standard template_group/template/entry EE formatting but the URLs started getting very long. I read your entire Pages Module article, but clients really have a hard time creating their own URLs (formatting issues, levels, etc).

    What method do you use for this?  Do you give clients Pages Module access or do you just allow them to create entries and use use standard template_group/template/entry EE formatting for URLs?  I noticed even the EE site removed the template_group from the URL on most pages, but on your site it looks like you leave it in place.  Any recommendations on the whole client turnover issues and creating pages? 

    I use NavEE for navigation now as I like how content and navigation are not combined and it does not take over the site like Structure.  I had to give up on Structure after having 3 sites have the Structure entries table go corrupt and all pages set to CLOSED status (about 50 pages each site).  After rebuilding the entire navigation in Structure again on those sites I gave up on it (hours of lost time). Many people have reported this issue and from what I have seen the devs have not been able to replicate the issue.

    Your advice is greatly appreciated.

    Bryan

  • #4 / Dec 20, 2011 8:59am

    Boyink!

    5011 posts

    I actually prefer content and nav be tightly coupled so clients don’t have to think about it separately.  I try to stick with the standard approach as a result.  Long URLs tend to be a developer concern more than a client concern.

  • #5 / Dec 20, 2011 9:05am

    bgarrant

    356 posts

    I actually prefer content and nav be tightly coupled so clients don’t have to think about it separately.  I try to stick with the standard approach as a result.  Long URLs tend to be a developer concern more than a client concern.

    I think I understand.  So you basically setup channels for each top level nav item and let them add entries to each one?  Just curious your recommendation on the whole template and channels relationship on this.  I agree the long URLs are typically not a concern in most cases.

  • #6 / Dec 20, 2011 9:07am

    Boyink!

    5011 posts

    Yes, so long as the content isn’t too deep.  I like having the publish & edit lists match the main nav as much as possible so it’s easier to (mentally) connect the front and back ends.

  • #7 / Dec 20, 2011 9:11am

    bgarrant

    356 posts

    Yes, so long as the content isn’t too deep.  I like having the publish & edit lists match the main nav as much as possible so it’s easier to (mentally) connect the front and back ends.

    That is what I am thinking as well.  I am going to try that approach on a site I am working on now.  I still like using NavEE to control the navigation as it enables the clients to be able to create more levels if needed, and it also allows them to reorder the navigation items.  Thanks for the advice.

  • #8 / Dec 20, 2011 12:18pm

    bgarrant

    356 posts

    Yes, so long as the content isn’t too deep.  I like having the publish & edit lists match the main nav as much as possible so it’s easier to (mentally) connect the front and back ends.

    In case anyone if watching this thread…

    Since I did not want to create a bunch of single index template, template groups with identical template code (all sub pages have same layout format), I choose this method which seems to work well and uses a single template approach.  It does show the template group in the URL, but I used a descriptive name “bank” so it should help SEO.  URLs look like “http://domain.com/bank/personal-loans”.  No Pages Module was used and all entries are in a single “pages” channel.  NavEE is used to select the single template and clients can add/remove pages and navigation items when needed.  I use Strict URLs and 404 redirects.

    {if segment_3 != ""}{redirect="404"}{/if}
    
    {if segment_1 == "bank" && segment_2 != ""}
    {exp:channel:entries channel="pages" url_title="{segment_2}" disable="categories|member_data|pagination" require_entry="yes" limit="1"}
    {if no_results}{redirect="404"}{/if}
    {content}
    {/exp:channel:entries}
    {/if}
  • #9 / Dec 20, 2011 12:24pm

    Boyink!

    5011 posts


    Since I did not want to create a bunch of single index template, template groups with identical template code (all sub pages have same layout format)

    Situations like that can often be handled with snippets or embedded templates.  I still like to keep my code DRY as well..😉

  • #10 / Dec 20, 2011 12:31pm

    bgarrant

    356 posts


    Since I did not want to create a bunch of single index template, template groups with identical template code (all sub pages have same layout format)

    Situations like that can often be handled with snippets or embedded templates.  I still like to keep my code DRY as well..😉

    I figured out how to make this work several different ways.  That is the beauty of EE.  In this case the word “bank” will actually help SEO so I can use a single index template and it is a good thing. 

    If a client does not want the template_group to show in the URL, my previous approach with single index templates would work well.  I did get it working pulling in channel entries from an embed template.  I still did not like having all those template groups for some reason.  More os a visual thing I guess.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases