1

I'm trying to make sure I understand the scope of {layout:set} variables, compared to how embeds work.

Traditionally, variables set with an {embed="template_group/template variable="foo"} are only accessible one level deep, directly within the embedded template.

Now let's talk layouts.

We have at minimum 2 layout templates: layouts/global & layouts/master.

For the sake of this questions, let's say I have a blog/index template that extends layouts/master and layouts/master extends layouts/global

blog/index:

 {layout="layouts/master"}

 {layout:set name="title"}Blog Title | {site_name}{/layout:set}

    {exp:channel:entries channel="blog"}

        All the blog content here...

    {/exp:channel:entries}

layouts/master:

{layout="layouts/global"}

{embed="embeds/masthead"}

{layout:contents}

{embed="embeds/footer"}

layouts/global:

<html>
    <head>
        <title>{layout:title}</title>
    </head>

     <body>   

         {layout:contents}

    </body>
</html>

The question I'd like clarity on (that my tests confirm) is this: Will the the {layout:title} variable be available within the layouts/global template regardless of where it's set, or do I have to redefine it within the layouts/master template, the way that you have to do if you were nesting embeds and passing variables?

1 Answer 1

2

From my own testing, the value set for the {layout:title} variable will cascade back through the layout templates to the outermost layout. So if you set it in a nested template or a template that ultimately uses the global layout, then the value of the variable will be available to the global layout.

If you set the {layout:title} variable in a nested template (e.g. blog/index in your example) then redefine it in the layouts/master template, the value set in layouts/master template will be the one that is presented to the global layout.

Note that you can use this to extend the value of the variable like this:

blog/index :

{layout="layouts/master"}

{layout:set name="title"}Blog Title{/layout:set}

{exp:channel:entries channel="blog"}

    All the blog content here...

{/exp:channel:entries}

layouts/master :

{layout="layouts/global"}

{layout:set name="title"}{layout:title} | {site_name}{/layout:set}

{embed="embeds/masthead"}

{layout:contents}

{embed="embeds/footer"}

layouts/global :

<html>
  <head>
    <title>{layout:title}</title>
  </head>

   <body>   

     {layout:contents}

  </body>
</html>

would result in the following:

    <title>Blog Title | Site Name</title>

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.