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.

Show different content based on {segment}

August 06, 2009 9:31am

Subscribe [2]
  • #1 / Aug 06, 2009 9:31am

    IschaGast

    56 posts

    On the website I am building I have these three pages:
    - http://janhekmanschool.nl/nieuws/
    - http://janhekmanschool.nl/nieuws/huisvesting/
    - http://janhekmanschool.nl/nieuws/2009/

    When {segment_2} ends with a year it must show the archive of that year and when {segment_2} is not a year it must show that post.

    But how do I target it?

    In my template I have this:

    <div id="content_main">
        {embed="_includes/.article" location="{template_group}"}
    </div><!-- /content_main -->

    Which loads this:

    <div class="article">
        {exp:weblog:entries weblog="{embed:location}" limit="1" orderby="date" sort="desc" disable="categories|member_data|pagination|trackbacks"}
            <div class="article_header">
                {if segment_1 == "nieuws" && segment_2==year}
                <h1>Nieuwsarchief {segment_2}</h1>
                {if:else}
                <h1>{title} {if segment_1 == "nieuws|nieuwsbrief"} <span>{entry_date format="%d %F %Y"}</span>{/if}</h1>
                {/if}
            </div><!-- /article_header -->
            {if segment_1 == "nieuws" &&  segment_2 != ""}
                {body_nieuws}
            {if:else}
                {body}
            {/if}
        {/exp:weblog:entries}
    </div><!-- /article -->

    I tried something with this kind of code but all failed:

    {if segment_1 == "nieuws" && segment_2==year}

    It failed I think because when you want segment_2==year it has to be wrapped inside {exp:weblog:entries or am I wrong?

    I hope someone that what I want is possible…

  • #2 / Aug 06, 2009 10:34am

    Joel Bradbury

    18 posts

    Are you thinking something like :

    {if '{segment_2}'=='{entry_date format="%Y"}'}
     ...

    That should work if you’re within the entries tag.

    If you want to filter outside the entries tag a quick (and dirty way) would be to

    {if segment_2<='99' || (segment_2>='2000' && segment_2<='2099')}
    
    ...etc

    Which will match only when it’s numeric and within certain ranges.
    Though that’s not very pretty imho.

  • #3 / Aug 06, 2009 1:33pm

    Lisa Wess

    20502 posts

    Quoting and bracing your segment conditionals is a huge no-no.  See Troubleshooting Conditionals.

    You will have to quote and brace your entry_date conditional though.

  • #4 / Aug 07, 2009 4:54am

    IschaGast

    56 posts

    It’s fixed with the filter outside the entries tag that’s a quick and dirty way but I think it could not be fixed within the entires tag because of this code:

    <ul class="list_weblog_archive">
        {exp:weblog:entries weblog="{template_group}" year="{segment_2}" dynamic="off" disable="pagination|trackbacks|member_data"}
            <li><a href="http://{path={">{title} <span>{entry_date format="%d %F %Y"}</span></a></li>
        {/exp:weblog:entries}
        </ul>

    I think it’s not possible to wrap that also inside the entries tag?

    So after a good night sleep I got it working with this code:

    <div class="article">
    {if segment_2 <= "99" OR segment_2 >= "2000" && segment_2 <= "2009"}
        <div class="article_header">
            <h1>Nieuwsarchief {segment_2}</h1>
        </div><!-- /article_header -->
        <ul class="list_weblog_archive">
        {exp:weblog:entries weblog="{template_group}" year="{segment_2}" dynamic="off" disable="pagination|trackbacks|member_data"}
            <li><a href="http://{path={">{title} <span>{entry_date format="%d %F %Y"}</span></a></li>
        {/exp:weblog:entries}
        </ul>
    {if:else}
    {exp:weblog:entries weblog="{template_group}" limit="1" orderby="date" sort="desc" disable="categories|member_data|pagination|trackbacks"}
        <div class="article_header">
            <h1>{title} <span>{entry_date format="%d %F %Y"}</span></h1>
        </div><!-- /article_header -->
        {body_nieuws}
    {/exp:weblog:entries}
    {/if}
    </div><!-- /article -->

    The only thing is that this code below dowsn’t feel good, I think that could be smaller?
    If segment_2 is any number should be enough I think but how?

    {if segment_2 <= "99" OR segment_2 >= "2000" && segment_2 <= "2009"}
  • #5 / Aug 07, 2009 5:08am

    Joel Bradbury

    18 posts

    Stupid. Sorry about quote & brace on the conditionals, that’s what you get for not really thinking when you write.


    if segement_2 could be _any_ number you could try

    {if segment_2 >= "1"}

    At least in theory. (Assuming here that you’ll not have -ve numbers in the url. I guess if you’re using it for pagination or years/months/days thats a given.)

  • #6 / Aug 07, 2009 5:35am

    IschaGast

    56 posts

    if segement_2 could be _any_ number you could try

    {if segment_2 >= "1"}

    At least in theory. (Assuming here that you’ll not have -ve numbers in the url. I guess if you’re using it for pagination or years/months/days thats a given.)

    This is not working :(
    What workst is this:

    {if segment_2 >= "2007" && segment_2 <= "2070"}
  • #7 / Aug 07, 2009 5:47pm

    IschaGast

    56 posts

    This code is working correct, I removed the quotes…

    {if segment_2 >= 1}
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases