4

In my particular case I need to be able to switch the layout that the template extends. Is this possible to do using locales?

Ian Young
  • 584
  • 3
  • 9

1 Answers1

7

Untested, but should be fairly straightforward.

{% if craft.locale == 'de' %}
    {% set template = '_layout_de.html' %}
{% else %}
    {% set template = '_layout.html' %}
{% endif %}
{% extends template %}

or

{% extends craft.locale == 'de' ? '_layout_de.html' : '_layout.html' %}

or using a locale folder (thank josh baker)

{% extends craft.locale ~ '/_layout' %}
Douglas McDonald
  • 13,457
  • 24
  • 57
  • 2
    You might also use string concatenation, which makes adding new layouts easier in future. For example {% extends craft.locale ~ '/_layout' %} would point to /templates/en/_layout. – Joshua Baker Jul 22 '15 at 17:40
  • Great addition @JoshBaker – Douglas McDonald Jul 23 '15 at 16:03
  • When I try that I get a "Multiple extends tags are forbidden" message returned. Something to do with the processing order perhaps where it's evaluating extends before the conditional construct? – Ian Young Jul 27 '15 at 11:14
  • 1
    Most likely just doesn't like the multiple extends statements. I modified the example using a single extends and variables. Also, did you try one of the other methods? – Douglas McDonald Jul 27 '15 at 15:33
  • The error message gave it away. :) In my case your other suggestion wouldn't fit my particular use case as it could be one of several and not just one or the other. I didn't notice Josh's suggestion though but it was the kind of pseudo logic I had in my head without quite knowing how to apply it. I've given it a try though and successfully got it to work along (with a slight tweak for my particular template folder structure: {% extends "layouts/" ~ craft.locale ~ '/layout-name' %}. Many thanks for the help and suggestions. – Ian Young Jul 28 '15 at 15:37