3

I use regular file names for template structure from documentation for clearer understanding.

I have the template structure:

  • _layouts
    • base.twig
  • _includes
    • header.twig
    • body.twig
    • foot.twig
  • index.twig
  • de
    • _includes
      • header.twig
      • body.twig
      • foot.twig
    • index.twig

So for top level domain, 'index.twig' extends 'base.twig' and 'base.twig' include 'header.twig','body.twig' and 'foot.twig'.

For sub level domain (example.com/de), It's the same scenario as for top level domain.

All 'index.twig' files in all sub level domains extends the same file located at '_layouts/base.twig'.

The include filters look like this in my 'base.twig' template.

{% block header %}{% include '_includes/header' %}{% endblock %} {% block body %}{% include '_includes/body' %}{% endblock %} {% block foot %}{% include '_includes/foot' %}{% endblock %}

All work great, but did I use it correctly?

The thing that confuses me, is that I didn't have to copy '_layouts' folder with 'base.twig' file to 'de' folder, and modify paths in 'base.twig' for all include filters. Why is it so? Twig renders files of an active locale?

3

From the localization guide - When Craft is looking for a template, it first sees if a template with that path exists inside a directory named after the current locale, then for the actual path provided.

So in your case, when you're in the de locale, it would look for the following templates:

  1. templates/de/_layouts/base.twig
  2. templates/_layouts/base.twig

and

  1. templates/de/_includes/header.twig
  2. templates/_includes/header.twig

etc, using the first one that exists.

If there's little or no difference between how the site behaves in different locales (other than the translated strings,) then it might not be necessary to override whole templates like that, but it's great when you need it.

|improve this answer|||||
  • I know, we should avoid in comments here of using words like "Thanks", but Thanks for clarification. Thanks to Twig, that Craft is so great to play with. Love it so far :) – Dominik Krulak Mar 1 '15 at 20:00

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.