0

I have created a .header template that goes in all of the content templates on a site. However, there are only two templates that require additional jQuery code inside the .header template. I don't want to include the secondary code in all template. I have reviewed the documentation here and know that it is possible to conditionally embed the extra code. I just can't find where that information is.

Here is the normal embed code for including the .header template:

{embed="inc/.header"}

Here is what I am trying to accomplish in English:

embed .header template, if the template is page_visit embed .map_code in .header template.

My attempt - which doesn't work:

{embed="inc/.header" {if template="page_visit"} embed="inc/.map-code"{/if}}

What is the correct way to do this?

0

Ingmar's answer will work, but my preferred way to do this (if you're not using Template Layouts that is - which you really should be!) would be to actually declare what you want to do via your embed parameters. e.g.,

{embed="inc/.header" include_map="y"}

Then in your embedded template you can use:

{if embed:include_map == "y"}
     // Your map code.
{/if}

This code will make much more sense down the road when you or someone else revisits it, and it's easier to enable that same extra code in your header in other templates as well.

Lastly, I'd also suggest you not place an embed inside of another embed, as it's not very performant. (Just put your actual javascript there in this example.)

1
  • Your answer is switch-based while mine relies on the name of the calling template. Definitely more than one way to skin a cat, as always ... – Ingmar Dec 3 '15 at 6:29
1

You will need to determine which template you are on. You should be able to use {template_name} for this. Then a simple conditional will do it:

{embed="inc/.header"}

{if "{template_name}" == "page_visit"}
    {embed="inc/.map-code"}
{/if}

You could also pass the template name on to .header, like so:

{embed="inc/.header" my_template_name = "{template_name}"}

And then in .header use a conditional like this:

{if "{embed:my_template_name}" == "page_visit"}

    {!-- calling template is page_visit, embed code for it --}

{/if}

{!-- here goes the rest of your code common to all templates --}
4
  • To answer the question better, the conditional code would be placed into inc/.header instead of the parent template. – Peter Lewis Dec 2 '15 at 9:18
  • That makes sense to me since the above solution appears to append rather than include the secondary template. Correct? – forrest Dec 2 '15 at 12:34
  • Obviously it's not a turn-key solution, but it should get you started. There are certainly other ways as well. If you pass the template_id on to the embed, you can do the conditional check there. – Ingmar Dec 2 '15 at 13:11
  • @PeterLewis: I have amended my example code. It has to take the scope of {template_name} into account, since we are interested in the name of the calling template and not .header. – Ingmar Dec 2 '15 at 13: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.