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.

EE conditionals + variables question

August 15, 2012 9:04pm

Subscribe [2]
  • #1 / Aug 15, 2012 9:04pm

    digitalformula

    176 posts

    Hi all,

    In one of my templates I have the following:

    {if:elseif segment_2 == "investment"}
     {snippet-investment}
    {if:elseif segment_2 == "about"}
     {snippet-about}

    Assuming that segment_2 is always where I need to check and all snippets are named “snippet-<name>”, is there a better way of doing this?

    Can I get EE to load the snippet named “snippet-<segment_2>” so that I don’t have to modify the template each time I add a new snippet?

    Thanks

  • #2 / Aug 15, 2012 9:09pm

    digitalformula

    176 posts

    I thought this was a dumb question but I can’t make the template do what I want.  For example, this doesn’t work:

    {snippet-{segment_2}}

    I hoped that would load a snippet based on what ever is in {segment_2}, provided it is prefixed with “snippet”.

    Is there a way of making that work?

  • #3 / Aug 17, 2012 12:00pm

    Kevin Smith

    4784 posts

    You’re running into issues with parse order here. I’d recommend downloading this PDF as a great reference.

    Let’s take a step back a bit and get a bigger look at this. What are you looking to achieve here? What are these snippets accomplishing on the page?

  • #4 / Aug 17, 2012 8:22pm

    digitalformula

    176 posts

    You’re running into issues with parse order here. I’d recommend downloading this PDF as a great reference.

    Let’s take a step back a bit and get a bigger look at this. What are you looking to achieve here? What are these snippets accomplishing on the page?

    Thanks, Kevin - I’ll take a look at that PDF.

    Re bigger look at this - essentially, I’m trying to reduce my template and snippet collection from what I think is too many down to ... well ... less.  😊

    I used to have a snippet named “snippet-<section>” where <section> is the specific title of the page being displayed.  It meant that I needed to check {segment_2} and, based on what it is, show the appropriate snippet.  It made the templates messy and far too big, not to mention duplicating some markup, too.  This was all done when I was just starting this site and before I knew anything about the conditionals etc.

    I’ve managed to make one of the primary culprit templates much better by using something like this:

    {exp:channel:entries channel="whatever" url_title="{segment_2"} dynamic="no" disable="categories"}

    That does work a lot better as it means I don’t have to check the {segment_2} value, provided it is valid (if not, the 404 template is shown, of course).

    The original question above was me trying to set a variable that matched the name of the required snippet before I tried putting the url_title into the exp:channel:entries tag, something that does seem to work OK.

    All that said, I’m simply trying to make the site more efficient and also so that the one of the non-technical admins of the site can add content without me having to create a snippet then check that it exists inside the template (super messy).

    Apologies if that makes no sense.  😊

  • #5 / Aug 20, 2012 4:35pm

    Shane Eckert

    7174 posts

    Hey digitalformula,

    Sounds like you are diving deep into ExpressionEngine!

    What did you think of the PDF?

    Cheers,

  • #6 / Aug 21, 2012 4:22am

    digitalformula

    176 posts

    Hey digitalformula,

    Sounds like you are diving deep into ExpressionEngine!

    What did you think of the PDF?

    Cheers,

    Hi Shane,

    The PDF did clear up the parse order but since the initial problem was me being an idiot, it didn’t help the issue ... lol.

    The next steps are to go through the current site’s templates and see how much of their markup can be reduced to one-time conditionals and used in all the templates from there.

    It’s a lot of work but fun, I think.  😊

    Cheers,
    Chris

  • #7 / Aug 22, 2012 12:27pm

    Shane Eckert

    7174 posts

    Hey digitalformula,

    Fun indeed! That’s a really good idea!

    If you need anything else, please just let me know by opening a new thread.

    Cheers,

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

ExpressionEngine News!

#eecms, #events, #releases