3

I am trying to use EE conditional statement within Javascript. The Javascript is called through a EE snippet. Part of the problem is I am using js conditionals right below my EE conditional. Is this even possible with a EE if statement? Do I need to escape my if statement some how? Any suggestions would be greatly appreciated. Here is a simplification of my snippet:

<script type="text/javascript"> 
...

{if add_option_on=="On"}
if (thisForm.os2.value == "") {
 alert('Please choose a {exp:channel:entries channel="products" disable="member_data|pagination" limit="1"}{customize_option_label}{/exp:channel:entries}');
 return false;
}
{/if}

...

</script>
  • What is add_option_on? Is that a global variable? – Siebird Dec 1 '12 at 3:42
  • No it's a channel field {add_option_on} – Shan Ricciardi Dec 1 '12 at 3:45
  • Of the products channel? – Siebird Dec 1 '12 at 3:46
  • yes {add_option_on} is a checkbox type channel field of the products channel. Thanks @Siebird for helping me clarify my question. – Shan Ricciardi Dec 1 '12 at 3:51
9

Try turning off javascript protection to allow the conditionals to parse: $config['protect_javascript'] = 'n';

Also, I would wrap the channel entries tag around the conditional like so:

<script type="text/javascript"> 
...
{exp:channel:entries channel="products" disable="member_data|pagination" limit="1"}
{if add_option_on=="On"}
if (thisForm.os2.value == "") {
 alert('Please choose a {customize_option_label}');
 return false;
}
{/if}
{/exp:channel:entries}
...

</script>
| improve this answer | |
  • Thanks @Siebird that did the trick! Looks like I overlooked having my EE conditional nested within a channel entries tag. Thanks again. – Shan Ricciardi Dec 1 '12 at 4:02
  • Yep no problem! – Siebird Dec 1 '12 at 4:04
  • So what is $config['protect_javascript'] = 'n'; doing? – Shan Ricciardi Dec 1 '12 at 4:06
  • You may even be able to get rid of that as it's not a advanced conditional. The protect_javascript config variable prevents the advanced conditionals (ie. if:else) parser from processing anything inbetween the tags. I believe the issue was where you had your channel tags ;) – Siebird Dec 1 '12 at 4:11
  • I tried protect_javascript to 'y' and it did not work. I looked this hidden configuration variable up on the EE docs for more explanation. If anyone else is wondering how this is used, you can read more about it here -->. – Shan Ricciardi Dec 1 '12 at 4:23

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.