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.

How can I check if template should display list view or single-entry view?

June 05, 2012 10:32am

Subscribe [2]
  • #1 / Jun 05, 2012 10:32am

    Matthew81

    81 posts

    Apologies for what promises to be a long post.

    To summarize as concisely as possible, the basic problem is to create a site-wide header template/snippet that dynamically sets the <title> and <meta> tags, while creating the minimum number of templates (e.g., using only index templates for as many template groups as possible, thus sharing a template for list-view and single-entry view).  I need to conditionally get the information either from {exp:channel:entries} or {exp:channel:info}.  The only method I’ve found so far to determine where the information must come from is to put the HTML tags inside an {exp:channel:entries} loop and count the results, which has unwanted effect of inserting the HTML tags multiple times. Preload replace variables would solve the entire problem except that they apparently can’t store EE data.

    So that’s the summary. Here’s a bit more detail:

    I’ve been using absolute_results as the criteria of {if} conditional statements to see if the template is loading a listing of entries or a single entry.  The problem with this is at least twofold:

          1.) It seems to require the HTML tags to be inside the {exp:channel:entries} loop, which inserts the tags
              multiple times on pages showing multiple entries, and,

          2.) It wouldn’t work in situations where {exp:channel:entries} only returns one item, yet context requires
              displaying a listing-style page anyway (this may be only a hypothetical problem).

    I could easily accomplish what I want if I were willing to create some additional templates. However, I have some channels which, with good use of {if} conditionals, could easily use only the index template to display both list view and single-entry view. It’s hard to justify cluttering my URLs with otherwise-meaningless segments just to load a different template. Besides which, extra templates are unnecessary complexity for development and maintenance, so I’d rather handle as much as possible through {if} conditionals within index templates.

    So that’s the situation with as much clarity as I can describe it with.  Any ideas appreciated, especially with regards to what other criteria I could use in my {if} conditionals that would more accurately determine if the template should be loading a list view or single-entry view.

    Thanks,

    Matthew

  • #2 / Jun 05, 2012 12:00pm

    Rob Allen

    3114 posts

    It might be worth looking into Snippets here to hold code per page view type, then use conditionals in your index template to pull out the right on according to whatever parameters you set, eg:

    {gv_html_start}
    {if segment_1 == ""}
    
      {sn_home}
    
    {/if}
    
    {if segment_1 != ""}
    
      {if segment_1 == "category"}
    
      {sn_category}
    
      {if:else}
    
      {sn_article}
    
      {/if}
    
    {/if}
    {gv_html_end}

     

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

ExpressionEngine News!

#eecms, #events, #releases