2

I wish to load some javascript in the footer, but only if a certain condition is met. I was wondering what the best option might be for this?

For example, if an entry has some related entries further up the page, then add the javascript in the footer.

The reason being that i don't want to include the javascript on every page.

3 Answers 3

2

Something like SWITCHEE would be useful to sort out conditionals without the information being loaded unless the condition is met. It is a little bit better than a straight conditional because the native if..then still looks for the item, just doesn't load it in.

{exp:switchee variable="{condition}" parse="inward"}
  {case value="x"}
     load js file
  {/case}
  {case value="y"}
     another js file
  {/case}
{/exp:switchee}
2

Take a look at the String or Variables plugins. Should do the trick.

The String plugin allows you to create and manipulate variables in your templates. This plugin is designed to take advantage of ExpressionEngine's unique way of parsing templates. Because the main template is executed to completion before any embedded templates are processed this plugin can be used to create a variable in the main template and output the value of the string in any embedded template... even an embedded page header template that comes before the point where you built the string!

or

The Variables plugin gives the possibility to create variables (either static or dynamic) and use them later on. This means that for example you can set certain data and use it in the head (if it's embedded) without having to re-create a tag pair for the specific channel.

0

Inside your entries tag perform a conditional looking for those related entries. Wrap it in a stash tag.

Then in your footer call the stash.

This should work. I'm a little unfamiliar with stash so can't give you a code sample guaranteed to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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