1

I'm making first steps with Twig in Craft. Ok, it's a Twig question. But maybe you can give me the best practice for Craft?

I'm used to create templates Top-Down, like having a master.html template that includes footer.html, header.html, content.html etc.

Now Twig and with this Craft suggests I'd rather extend / inherit parent templates.

It looks like the easy way to start if I simply use {% include "pages/partials/_head.html" %} – but will this later on, when I start working on the content, somehow prohibit accessing the unique features of extend?

So: Should I grasp the logic of extend first or doesn't it matter?

2

Hmmm I think I've got it already:

_layout.html

<!DOCTYPE html>
<html lang="de">

{% include "pages/partials/_head.html" %}

<body class="body">

{% include "pages/partials/_header.html" %}

    <main class="main-wrapper">
         {% block content %} 
         MAIN CONTENT PLACEHOLDER
         {% endblock %}
    </main>

{% include "pages/partials/_footer.html" %}
{% include "pages/partials/_jsfooter.html" %}

</body>
</html>

and then in a page template, say front.html.

{% extends "_layout" %} 

{% block content %} 
FRONT PAGE CONTENT
{% endblock %}
| improve this answer | |
  • This is correct. However, I tend to put my <head> stuff straight in my base layout file (and the JS tags at the bottom). Because with an include it's just another bit of work for Craft to do that doesn't offer much benefit in development. Header and Footer files get worked on regularly during development that can be very large files, so it makes sense to pop them out. – Jay Aug 24 '18 at 9:15
2

I use extend for different entry types. So i have a master.html with the main html construct and includes page parts like header.html and footer.html

Then i have a standard.html that extends master.html and include the content templates.

Entry Types with different page structure like projects.html or gallery.html also extend master.html, but can have different content templates included.

| improve this answer | |
2

My setup regarding includes for templates is as follows:

I'll have a 'site' structure that looks at /pages/_entry.twig for it's template. But I'd have a range of entry types within this structure, for example:

  • Homepage
  • Contact
  • News
  • Standard content

Then my /pages/_entry.twig template looks like this:

{% extends "_layout/base.twig" %}

{% block content %}
    {% include 'pages/' ~ entry.type %}
{% endblock %}

So the extending of my base template is sorted for all of them in one place (less repeated code 👍🏼) and it just includes a file based on the entry type like so:

/pages
  -- _entry.twig
  -- contact.twig
  -- homepage.twig
  -- news.twig
  -- standardContent.twig

And the entry type matches the file name so each entry type has it's own template automagically.

| improve this answer | |
  • Is the value for entry.type passed in automatically? Where do you set it? And does it matter if the ending is .twig or .html? – Urs Aug 24 '18 at 10:16
  • 1
    entry.type is passed automatically, yeah. I use .twig so I have Twig syntax highlighting in my IDE. – Jay Aug 24 '18 at 11:19
  • 1
    oho, that's why it doesn't highlight with me! – Urs Aug 24 '18 at 13:06
  • What IDE are you using? – Jay Aug 29 '18 at 10:49
  • 1
    Im using vscode – Urs Aug 29 '18 at 14:13

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.