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.

Displaying a "default" single entry in a template

November 05, 2007 1:59pm

Subscribe [3]
  • #1 / Nov 05, 2007 1:59pm

    quena

    66 posts

    I’m trying to get a get a template to display a specific entry if none is specified in the URL. The template_group/template is “site/about”; the default entry, “About the Organization” (about-the-organization), is entry #17 in the “about” weblog.

    In other words, I want http://www.example.com/site/about/ to display the same thing as http://www.example.com/site/about/about-the-organization/, instead of a list of entries.

    I’ve been trying to do this by testing for {segment_3}; if it’s missing, the template would display the default entry.

    <!-- Use variables to allow the code to be used for other weblogs simply by changing the values -->
    {assign_variable:this_weblog="about"}
    {assign_variable:default_entry="17"}
    {assign_variable:this_template_group="site"}
    
    {if segment_3}                                         <!-- Test for segment_3; -->
        {exp:weblog:entries weblog="{this_weblog}"}
            {assign_variable:this_entry="{entry_id}"}      <!-- if it exists (indicating the presence of a sing
                                                                single entry), assign {this_entry} the
                                                                {entry_id}'s value; -->
        {/exp:weblog:entries}
    {if:else}
        {assign_variable:this_entry="17"}                  <!-- otherwise, assign {this_entry} the
                                                                {default_entry}'s value  -->
    {/if}
    
    <-- Test output -->
    {assign_variable:this_entry="17"}Third segment = {segment_3}; {this_entry} in {this_weblog}

    The resulting HTML source at http://www.example.com/site/about/:

    <!-- Use variables to allow the code to be used for other weblogs simply by changing the values -->
    
    
    
    
                          <!-- otherwise, assign {entry_id} the
                                                                17's value  -->
    
    <--! Test output -->
    Third segment = ; {entry_id} in about

    The resulting HTML source at http://www.example.com/site/about/about-the-organization/:

    <!-- Use variables to allow the code to be used for other weblogs simply by changing the values -->
    
    
    
    
                                             <!-- Test for segment_3; -->
        
                  <!-- if it exists (indicating the presence of a sing
                                                                single entry), assign 17 the
                                                                17's value; -->
        
    
    <--! Test output -->
    Third segment = about-the-organization; {entry_id} in about

    Am I over-thinking this? I can’t figure out why {entry_id} isn’t getting the assigned value. (Or maybe it’s not able to retain the value outside of {exp:weblog:entries) tag pairs?)

  • #2 / Nov 05, 2007 2:01pm

    quena

    66 posts

    Also, I don’t want to use {sticky} to make an entry the default; I’m using sticky to pull entries for the (Suckerfish) menu, which allows me to control the menus to some degree from within the CP > Edit entries page.

  • #3 / Nov 05, 2007 2:03pm

    Lisa Wess

    20502 posts

    Yes, this has to do with parse_order.

    What I would do for this would actually just be to use require_entry, and if no_results; rather than use a different entry, just put it in the if no_results.

    I’m going to pop this up to the how-to forum, so you can get some brainstorming done. =)

    Alternatively, you could simply use two weblog entries tag, and use a conditional to show the right one based on the existence of segment 3.

  • #4 / Nov 05, 2007 2:06pm

    quena

    66 posts

    Thanks, Lisa. Any thoughts on why {entry_id} seems unable to accept/retain an assigned value?

  • #5 / Nov 05, 2007 2:08pm

    Lisa Wess

    20502 posts

    Thanks, Lisa. Any thoughts on why {entry_id} seems unable to accept/retain an assigned value?

    I wasn’t clear; that issue is why I mentioned parse order.  =)

  • #6 / Nov 05, 2007 2:19pm

    e-man

    1816 posts

    I’ve been trying to do this by testing for {segment_3}; if it’s missing, the template would display the default entry.

    How’s this?:

    {assign_variable:this_weblog="about"}
    {assign_variable:default_entry="17"}
    {assign_variable:this_template_group="site"}
    
    {if !segment_3}                                
    {exp:weblog:entries weblog="{this_weblog}" entry_id="{default_entry}"}    
    content here                                              
    {/exp:weblog:entries}                          
    {if:else}                                     
    {exp:weblog:entries weblog="{this_weblog}" limit="5"}    
    content here                                              
    {/exp:weblog:entries}               
    {/if}
  • #7 / Nov 05, 2007 3:35pm

    quena

    66 posts

    Assuming I go with the {if no_results} option, do I have hard-code the default entry’s contents into the template? How can I pull the default entry (entry_id=17)?

  • #8 / Nov 05, 2007 3:36pm

    Lisa Wess

    20502 posts

    Yes, that would involve hard-coding. You could do some embedding and passing the variable; but the end result would be some pretty heavy nesting that I’d strongly recommend against.

    You could also use a direct query, of course.

  • #9 / Nov 06, 2007 11:44am

    quena

    66 posts

    Lisa: Alternatively, you could simply use two weblog entries tag, and use a conditional to show the right one based on the existence of segment 3.

    Erwin: How’s this?

    Aside from moving this from a template (site/about) into its own template group (about/index) and using a shorter URL to access it (http://example.com/about/, etc.), I have implemented the test for segment_2 (instead of segment_3) based on your suggestions:

    {if !segment_2}
      {exp:weblog:entries weblog="about" entry_id="{default_entry}"}
        {exp:strip_p}{title}{/exp:strip_p}
        {exp:strip_p}{exp:allow_eecode}{body}{/exp:allow_eecode}{/exp:strip_p}
      {/exp:weblog:entries}
    {if:elseif segment_2}
      {exp:weblog:entries weblog="about" require_entry="yes"}
        {exp:strip_p}{title}{/exp:strip_p}
        {exp:strip_p}{exp:allow_eecode}{body}{/exp:allow_eecode}{/exp:strip_p}
      {/exp:weblog:entries}
    {/if}

    http://example.com/about/ pulls up a blank page, with nothing in the exp:weblog:entries tag pair under the first conditional. The conditional works, which I can verify by putting plain text between the conditional and the opening exp:weblog:entries tag.

    http://example.com/about/about-the-organization/ works just fine, where “about-the-organization” is an entry URL rather than a template.

  • #10 / Nov 06, 2007 12:10pm

    Lisa Wess

    20502 posts

    {if !segment_2}

    That is not valid.  Check the docs on conditionals for valid syntaxes. =)

  • #11 / Nov 06, 2007 12:13pm

    e-man

    1816 posts

    {if !segment_2}

    That is not valid.  Check the docs on conditionals for valid syntaxes. =)

    D’oh! Try this instead then:

    {if segment_2==""}
  • #12 / Nov 06, 2007 12:28pm

    quena

    66 posts

    Found the problem: {default_entry} is not carrying/passing the value, probably because of the parse order. Hard-coding it to “17” fixed the problem.

    Thanks to you both for offering free trouble-shooting. I will probably end up setting global variables for the “About” weblog and any other “static” weblogs; this would be helpful for Suckerfish menus which make use of the suggestion to give every “parent” menu link a destination to improve accessibility.

  • #13 / Nov 07, 2007 7:54am

    van der Spuy

    36 posts

    Hi, I had a similar problem and managed to solve it using the techniques you suggested.  This is much simplified from the code above, but works fine for my purposes so far:

    {if segment_2=="index"}                     
          {exp:weblog:entries weblog="default" disable="catgories|member_data|pagination|trackbacks" sort="asc" dynamic="off"} 

    {title}

          {body}                           
          {/exp:weblog:entries}                 
          {if:else}                       
          {exp:weblog:entries weblog="{segment_2}” disable=“catgories|member_data|pagination|trackbacks” sort=“asc”  dynamic=“off”}

    {title}

          {body}
          {/exp:weblog:entries}       
          {/if}

    I’m not sure if testing for “index” is the best solution, however.

    - Rex

  • #14 / Nov 07, 2007 12:29pm

    quena

    66 posts

    Thanks, Rex. This code has some good pointers for me. Did you come up with this recently or have you been running it for a while? Could I look at your (public) site?

    I added a test for segment_2==”“, in case the template group’s index page was assumed by the URL (e.g. http://example.com/site/ instead of http://example.com/site/index/). I’m also not keen on losing functionality for categories, member data, etc. so I’m going to keep pushing on this solution.

    I’d welcome further thoughts on this.

  • #15 / Nov 07, 2007 1:06pm

    van der Spuy

    36 posts

    Hi Quena,

    I just worked out this code this morning based on lots of bits of pieces from various tutorials and forum threads. Expression Engine is very new to me, so I’m learning too.  I don’t have a public site up yet, unfortunately.

    Actually, right now I’m trying to refine this code to make it more flexible, but am running into a problem with what I believe is the “parsing order”

    First, at the top of the template, I’m using this code to try and find out what the current URL is:

    {assign_variable:my_weblog="{segment_2}”}
    {if segment_2 == "index"}
    {assign_variable:my_weblog="home"}
    {/if}

    This checks to see if “segement_2” is “index”.  If it is, is knows to assign the “home” weblog to the the “my_weblog” variable.  If segment_2 isn’t “index” then any other weblog assigned as part of the URL (such as “about” or “contact”) is loaded into the “my_weblog” variable.  That’s basically the same logic as the code I posted earlier.

    Later in the code, I use this:

    {exp:weblog:entries weblog="{my_weblog}” disable=“catgories|member_data|pagination|trackbacks” sort=“asc”  dynamic=“off”}

    {title}

    {body}
    {/exp:weblog:entries}

    This works the same as the code in my post above, but uses the “my_weblog” variable to load the weblog content.  This should be more efficient as I can use the weblog stored in “my_weblog” to do other clever things later like display title information. 

    However, I’ve run into a problem: the conditional statement doesn’t appear to work.  I’m not sure if it’s a logic/syntax error on my part, or a “parse order” problem.  From reading the docs I know that you can’t use “assign_variable” in advanced conditional statments, however I’m using a simple conditional, so I’m not sure if this is the problem.  When I view the template, the content where the weblog should be displayed is blank.  If I click on a link, however, such as “about” the correct “about” weblog content displays, which shows that the rest of the code is running as expected.  The part that doesn’t work is the “assign_variable” inside the conditional statement for the index page.

    If anyone with more experience could point out what the problem might be, I’d be very grateful!

    Thanks,
    Rex

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

ExpressionEngine News!

#eecms, #events, #releases