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.

Conditionals in Snippets

March 30, 2011 5:50am

Subscribe [4]
  • #1 / Mar 30, 2011 5:50am

    R Gruyters

    14 posts

    Is it possible to set conditionals in Snippets?

    I have a Global Variable set named “google analytics” and have the following conditional in “snp_footer” Snippet:

    {if google_analytics}
    [removed]
    var _gaq = _gaq || [];
    ...
    [removed]
    {/if}

    When try to load a page it doesn’t add the Google Analytics script.

  • #2 / Mar 30, 2011 5:20pm

    Brandon Jones

    5500 posts

    Hi R Gruyters,

    What does {google_analytics} output on its own inside the snippet? Also, if you’re setting the global variable via index.php, that won’t currently work and you’ll need to define it via the Control Panel instead.

  • #3 / Mar 30, 2011 5:41pm

    R Gruyters

    14 posts

    {google_analytics} displays the Google code when I call it within a snippet.

    {google_analytics} is set in the Global Variable in the Control Panel.

  • #4 / Mar 30, 2011 6:41pm

    Mark Bowen

    12637 posts

    I might be wrong but as global variables are one of the last thing parsed via ExpressionEngine and snippets are quite early on is this perhaps a parse order problem here?

    Best wishes,

    Mark

  • #5 / Mar 30, 2011 7:14pm

    Mark Bowen

    12637 posts

    EDIT - Actually a daft question here but can you even perform a conditional on a global variable anyway?

    Also unless I’m doing something really silly I can’t even get Global Variables to work at all in 2.1.4 :-(

    I tried this :

    {if global_test == "yes"}
    Output this
    {/if}

    global_test is a global variable set via the templates manager and contains the word yes.

  • #6 / Mar 31, 2011 6:04am

    R Gruyters

    14 posts

    Same here. But you can display it within a snippet and template right? ({global_test})

  • #7 / Mar 31, 2011 6:32am

    Mark Bowen

    12637 posts

    Yep a global variable can go both in a snippet and a template and shows up fine. I could have sworn you could do conditionals on a user defined global variable but perhaps I’m mistaken there?

  • #8 / Mar 31, 2011 6:34am

    R Gruyters

    14 posts

    Apparently.. 😉

    It doesn’t seem to be working.

  • #9 / Apr 03, 2011 4:25pm

    Greg Salt

    3988 posts

    Hi R Gruyters,

    Unless I’m misunderstanding you completely you don’t need a global variable. Just put your GA scripts in a snippet and you can test that snippet in a conditional:

    {if my_snippet != ''}
    {my_snippet}
    {/if}

    @Mark, you are correct, a global variable is parsed very late so that’s why the conditional won’t work. A snippet will though.

    Does that help?

    Cheers

    Greg

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

ExpressionEngine News!

#eecms, #events, #releases