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.

Check if Global Template Variable exists

April 03, 2009 7:28am

Subscribe [3]
  • #1 / Apr 03, 2009 7:28am

    WDBookassist

    26 posts

    Hi all,

    Pretty simple question here I just can’t seem to find an answer to in the archives.

    Is it possible to check whether a Global Template Variable exists? Rather than just check that it’s equal or not equal to something…

    Scenario: I’m writing out some tracking and Google analytics JavaScript within a website, site-wide, so I put the text in global template variables. So I have {analytics} in every template page writing my analytics JS.
    Thing is we might want to turn this off sometimes. And I don’t want to delete the contents of the global variables (which would admittedly do the job) as I might want to turn it on again later.

    So my idea was to just rename the global variable and thus stop it being written. (e.g. “analytics_off”). But to do this I would need to check if the variable exists before trying to write it. Something like:

    {if analytics}
    {analytics}
    {/if}

    ...which obviously isn’t working.

    Or maybe this is just a really silly way to be going about things and someone can suggest an alternative?

  • #2 / Apr 03, 2009 8:10am

    BC Team

    244 posts

    Why not just have a template that contains all the Google JS and have it embed in a ‘Footer template’

    {embed="global/analytics"}

    If you want to turn analytics off just comment out or delete the code in the analytics template?

    Not sure if I am understanding your question though.

  • #3 / Apr 03, 2009 8:16am

    WDBookassist

    26 posts

    Oh. I had considered just making a template page and including that but forgotten about the possibility later on. That actually works. If I alter the name of “analytics” it doesn’t cause any issues, just ignores the embed.

    Thanks!

  • #4 / Apr 03, 2009 10:00am

    Ingmar

    29245 posts

    Is it possible to check whether a Global Template Variable exists? Rather than just check that it’s equal or not equal to something…

    Well, couldn’t you check for it being empty?

    {if foobar != ""}
     {foobar}
    {/if}
  • #5 / Apr 03, 2009 10:19am

    WDBookassist

    26 posts

    I tried this originally but it doesn’t appear to work. If foobar doesn’t exist (i.e. I’ve changed its name to “foobar_off”) this if statement will output {foobar} for me, and since foobar no longer exists I get the actual text “{foobar}”.

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

ExpressionEngine News!

#eecms, #events, #releases