1

Having issues trying to wrap logic inside of <script> tags, it seems my "if/else" statements get printed instead of parsed?

As an example, if I do something like:

{exp:query sql="SELECT a_field FROM some_table"}
<script>
{if "{a_field}" == ""}
var field = "some value";
{if:else}
var field = "{a_field}";
{/if}
</script>
{/exp:query}

You would think it would output var field = "some value"; if the field is empty, else output var field = "{a_field}";. In actuality, it will output the actual logic?

E.g., the above would actually output

<script>
{if "field value" == ""}
var field = "field value";
{if:else}
var field = "field value";
{/if}
</script>

<script>
{if "" == ""}
var field = "";
{if:else}
var field = "";
{/if}
</script>

...

If I move the logic outside of the <script> tags, or simply remove them, the logic is parsed as expected. Never ran across this before, so hoping someone has some insight...

P.S. using EE 2.8.1 and (if it matters) PHP 5.4.39 for testing currently.

3

OK, I feel like an idiot! Forgot about the Javascript template type.

Solution: I simply created a new template, designated it as type "JavaScript", inserted my code, minus the <script> tags, then did <script src="{path="paht_to_js_template"}"></script>.

Simple ;)

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.