0

I want to have some fallback text for when a snippet isn't available. I tried this:

{if '{snip-start-to-end-dates}' != ""}
  {snip-start-to-end-dates}
{if:else}
  Long-term Installation
{/if}

...but I get this error:

You have an invalid conditional in your template. Please review your conditionals for an unclosed string, invalid operators, a missing }, or a missing {/if}.

Parser State: Unexpected ‘}” == “{exhibition_e…’ (STRING); expected RD in Template “XXXXXX/exhibitions” on line 25.

I can't find anything in EE's documentation indicating a special way to check for snippets. Any ideas?

  • Is there any eecms code inside the snippet? – Robson Sobral Jun 8 '15 at 22:29
  • Yes, there's this code for formatting. I tried copying and pasting in the comment, but it's removing the line breaks so it isn't very readable. Basically the snippet contains a bunch of conditionals which determine how the date and time are formatted. – huertanix Jun 8 '15 at 23:09
  • Please, add the code to your question. – Robson Sobral Jun 9 '15 at 1:32
0

Start by creating a simple conditional with no EE tags in it, so you can identify if the erronious } is in the snippet or in the way the advanced confditional is bein parsed. For me I'd be inclined to lose the advanced conditional and make it simple too, for testing.

{if '{snip-start-to-end-dates}' == ""}Long-term Installation{/if}
{if '{snip-start-to-end-dates}' != ""}{snip-start-to-end-dates}{/if}

Ugly but simplifies the conditional. I would wager the issue is in the tags in your snippet though! Your code snippet in the OP looks ok.

Of course if you look to the EE Parse Order you'll notice that snippets are replaced on the template first, whereas what you seem to be doing is trying to measure the result of the snippet. For example assume I had this snippet :

{if '{segement_1}' == 'someseg' && '{segemnt_2}' != ''}
  {if group_id == 6}
    <p>Welcome User!</p>
  {/if}
{/if}

If I re-inact what happens on your conditional I get this :

{if '{if '{segement_1}' == 'someseg' && '{segemnt_2}' != ''}{if group_id == 6}<p>Welcome User!</p>{/if}{/if}' != ""}
  {if '{segement_1}' == 'someseg' && '{segemnt_2}' != ''}
    {if group_id == 6}
      <p>Welcome User!</p>
    {/if}
  {/if}
{if:else}
  Long-term Installation
{/if}

Woah! that got messy, note how the snippet within the quotes in the conditional is replaced as text? You're asking for trouble parsing this! Just look at the first part of the conditional now, in short you've got quote and curly brace madness happening within the if conditional.

You're best off not using a snippet and making sure your snippet returns "Long-term Installation" instead of nothing so you can just include it.

{snip-start-to-end-dates}

But to answer the question specifically, to check if a snippet exists and has characters within it you're right :

{if '{snip-start-to-end-dates}' != ""} ... {/if}

Edit 1 : Of course, there is another (way)

Here's a little trick that might just help you evaluate the result of the snippet as opposed to the content, you'll need to use PHP and 'borrow' EE's Template parser...

The usual wanring, danger, thar be pirates! PHp and messing iwth the Template parser can quickly ge you into a 'WAT' Situation (this is well worth a watch btw, good for a chuckle), though I have tested this code with my own snippet, with a conditional in it!

<?php 
  //you need to fool EE here, by making the snippet string with a 
  //PHP concat it's not picked up as a parsabel too early'
  $snipTest = '{' . 'test_snip}'; 

  //this will parse the $test string as if it where a template, and parse the snippet globals created within
  ee()->TMPL->parse($snipTest );
  $snipTest = ee()->TMPL->parse_globals($snipTest);

  //test the string, the string is the result of the evaluated template, not the snippet contents!!
  if ( strlen($snipTest) > 0 ) { echo $snipTest; } else { echo "Long-term Installation"; }
?>
| improve this answer | |
  • Thanks! Yeah, after realizing how the parse order can add unnecessary complication, I decided to just check for an exhibition end date, which Long-term exhibitions don't have. Much cleaner/simpler. – huertanix Jun 9 '15 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.