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.

Showing protected content without duplicating code blocks

October 19, 2008 4:49pm

Subscribe [4]
  • #1 / Oct 19, 2008 4:49pm

    George Ornbo

    272 posts

    I need to set up a live review for new content. I’m hoping someone might be able to advise me on a better method as the current method I’m using is ugly from a maintenance point of view as I will be duplicating hundreds of lines of code. I can’t seem to find a way in EE that avoids duplicating code.

    Currently I’m using member group ids and status to control access to pages:

    {if member_group == "1" OR member_group == "7" OR member_group == "8"}
        {exp:weblog:entries  weblog="myweblog" status="Draft|Approved Internally|Approved Externally|Open"}
        ..Massive amount of code here
        {exp:weblog:entries}
    {if:else}
        {exp:weblog:entries  weblog="myweblog" status="Open"}
        ..Massive amount of the same code I'm duplicating again here. Meet maintenance hell!
        {exp:weblog:entries}
    {/if}

    I tried

    {if member_group == "1" OR member_group == "7" OR member_group == "8"}
        {exp:weblog:entries  weblog="myweblog" status="Draft|Approved Internally|Approved Externally|Open"}
    {if:else}
        {exp:weblog:entries  weblog="myweblog" status="Open"}
    {/if}
        ..Massive amount of code here
        {exp:weblog:entries}

    it seems that the member variables are parsed after the weblog tag so this fails and shows protected content to everyone


    I also hoped this might work

    {if member_group == "1" OR member_group == "7" OR member_group == "8"}
    {assign_variable:protected_status="Draft|Approved Internally|Approved Externally|"}
    {/if}
    
    {exp:weblog:entries  weblog="myweblog" status="{protected_status}Open"}
    ..Massive amount of code here
    {exp:weblog:entries}

    this also fails and shows protected content to member groups outside of the ones specified

    The main thing I want to avoid is duplicating big blocks of code. It would seem pretty simple - just a variable that needs to be set but I’m tearing my hair out!

    Any ideas about how to do this without using two sets of weblog tags as per the first example?

  • #2 / Oct 19, 2008 9:19pm

    Boyink!

    5011 posts

    Did you try making the weblog:entries loop an embedded template, and send the status as an embed variable from the conditional?

  • #3 / Oct 20, 2008 6:00am

    George Ornbo

    272 posts

    Thanks for you insight! You’ve helped me solve it!

    For anyone else who needs it it works like this. I’m on a single entry page so I also need to send the url title to the embeded template. In my case it is segment_3 but just change that to whichever segment yours is.

    {if member_group == "1" OR member_group == "7" OR member_group == "8"}
    {embed="includes/my_single_entry_template" url_title="{segment_3}" protected_status="Draft|Approved Internally|Approved Externally"}
    {if:else}
        {embed="includes/single_entry_template" url_title="{segment_3}" protected_status="Open"}
    {/if}

    The embedded template is then

    {exp:weblog:entries  weblog="your_weblog" status="{embed:protected_status}" require_entry="yes" url_title="{embed:url_title}"}
    {if no_results}{redirect="404"}{/if}
    .. your template stuff here…
    {/exp:weblog:entries}

    This saves on duplicating the template code with workflow - exactly what I was looking to do.

    Thanks Michael!

  • #4 / Oct 20, 2008 7:24am

    Mark Bowen

    12637 posts

    George just as a quick note so that you don’t try it again next time 😉, in your second piece of code :

    {if member_group == "1" OR member_group == "7" OR member_group == "8"}
        {exp:weblog:entries  weblog="myweblog" status="Draft|Approved Internally|Approved Externally|Open"}
    {if:else}
        {exp:weblog:entries  weblog="myweblog" status="Open"}
    {/if}
        ..Massive amount of code here
        {exp:weblog:entries}

    That would probably never work as you have to have a complete weblog tag together each time such as :

    {if member_group == "1" OR member_group == "7" OR member_group == "8"}
    
    {exp:weblog:entries  weblog="myweblog" status="Draft|Approved Internally|Approved Externally|Open"}
    Code goes here…
    {/exp:weblog:entries}
    
    {if:else}
    
    {exp:weblog:entries  weblog="myweblog" status="Open"}
    Code goes here…
    {/exp:weblog:entries}
    
    {/if}

    You have to have a complete weblog tag together in each part of the {if} conditional for it to work like that. Using embeds as Michael pointed out will stop those problems from happening though.

    Glad you have it figured out now. Just thought I would show why the second piece of code won’t work though.

    There’s a bit more information in the knowledge base about this too

    http://expressionengine.com/knowledge_base/article/tags_within_advanced_conditionals/


    Best wishes,

    Mark

  • #5 / Oct 20, 2008 7:32am

    George Ornbo

    272 posts

    Thanks Mark - sorry I thought it was clear that in the first post none of the examples other than duplicating the code as per the knowledge base article work.

    From a maintenance view I wanted to avoid this as I have many lines of code and I know these will need to be updated in the future.

    Using embeds is definitely the way to do it in my opinion - the KB article isn’t very practical when you need to maintain a code block with 100s of lines of code.

    I’ve now got a nice, clean workflow system and just one set of code to maintain 😊

  • #6 / Oct 20, 2008 7:39am

    Mark Bowen

    12637 posts

    Thanks Mark - sorry I thought it was clear that in the first post none of the examples other than duplicating the code as per the knowledge base article work.

    Sorry I didn’t mean to butt in on the post. I could see that you knew what didn’t work but wasn’t sure if you had seen the KB article or not.

    From a maintenance view I wanted to avoid this as I have many lines of code and I know these will need to be updated in the future.

    Using embeds is definitely the way to do it in my opinion - the KB article isn’t very practical when you need to maintain a code block with 100s of lines of code.

    Yep embeds is the way I would have done it too. I think the reason it is shown like that in the KB article though is because then new users can see what is meant by not breaking up the weblog tags. If it showed embeds instead then they would have to be explained too. You’re right though the KB could perhaps explain it a little more in depth though.

    Perhaps a quick PM to Lisa or one of the moderators to perhaps update the KB article a little might be helpful for others.

    I’ve now got a nice, clean workflow system and just one set of code to maintain 😊

    Glad you have it all working now. Hope it all goes great for you.

    Again sorry for popping in on the thread, I just wasn’t sure if you knew why what you were doing wasn’t working so thought the link might help a bit for the future.

    Best wishes,

    Mark

  • #7 / Oct 20, 2008 8:28am

    Boyink!

    5011 posts

    Cool deal…glad it worked…;)

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

ExpressionEngine News!

#eecms, #events, #releases