2

I'd like to set some default values for several layout variables, for when a particular template doesn't have a need to pass a specific variable to the parent layout. For example, I set my page's "description" meta tag via a layout variable, but some pages don't need a unique description - they should just use a "global" description. Setting defaults would save a lot of extraneous code in each template.

I tried doing a conditional check in my layout template:

{if layout:description == ''}
    {layout:set name="description" value="My default site description."}
{/if}

But that doesn't work, because layout:set tags are not parsed inside layout templates.

How can I set defaults?

1 Answer 1

3

The key here is in looking at the template engine order of operations. You can see that snippets are processed very early on. It also so happens that if you set a layout variable multiple times, it's the last value which remains.

So you can create a snippet which sets default for all of your metadata, then include that snippet alongside your layout tag in all your templates. Then, call {layout:set} for only the variables you need to change per-template.

Template

{layout="_global/_layout"}
{sn_default_metadata}

<p>Hello world.</p>

Layout file (_global/_layout)

<!DOCTYPE html>
<html lang="{lang}">
<head>
    <meta charset="{charset}" />
    <meta name="description" content="{layout:description}" />
    <meta name="thumbnail" content="{layout:thumbnail}" />
    {if layout:robots}<meta name="robots" content="{layout:robots}" />{/if}

    <meta property="og:title" content="{if layout:title}{layout:title}{if:else}{layout:site_name}{/if}" />
    <meta property="og:description" content="{layout:description}" />
    <meta property="og:image" content="{layout:thumbnail}" />
    <meta property="og:url" content="{layout:url}" />
    <meta property="og:type" content="{layout:open_graph_type}" />

    <title>{if layout:title}{layout:title} &ndash; {/if}{layout:site_name}</title>
</head>
<body>

    {layout:contents}

</body>
</html>

Snippet ({sn_default_metadata})

{layout:set name="description" value="My default site description."}
{layout:set name="open_graph_type" value="website"}
{layout:set name="site_name" value="My Great Website"}
{layout:set name="thumbnail" value="{site_url}/assets/img/thumbnail.png"}
{layout:set name="url" value="{current_url}"}

Template with overrides

{layout="_global/_layout"}
{sn_default_metadata}

{exp:channel:entries channel="news" limit="1"}
    <h1>{title}</h1>
    <img src="{news_image}" alt="" />
    {news_body}

    {layout:set name="title" value="{title}"}
    {layout:set name="description" value="{news_summary}"}
    {layout:set name="url" value="{title_permalink="news/entry"}"}
    {layout:set name="thumbnail" value="{news_image}"}
    {layout:set name="open_graph_type" value="article"}
{/exp:channel:entries}
6
  • Since layout variables are parsed before preload_replace ones, you can use it to set the defaults.
    – Sobral
    Commented Nov 7, 2014 at 16:23
  • Can you elaborate Robson? Interested in seeing other examples of how to achieve this. Commented Nov 7, 2014 at 17:19
  • Something like {preload_replace:layout:robots="index,follow,noarchive"} <meta name="robots" content="{layout:robots}">
    – Sobral
    Commented Nov 7, 2014 at 19:06
  • Hmm, this doesn't work at all in my testing. I don't think you can preload_replace layout vars. Commented Nov 7, 2014 at 19:18
  • I tested this and it worked. I even talked about to EllisLab. Just drop this on the layout file and it should work.
    – Sobral
    Commented Nov 7, 2014 at 19:28

Your Answer

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

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