fvsch / kirby-twig

Twig templating support for Kirby CMS 2. For Kirby 3, use https://github.com/amteich/kirby-twig
MIT License
70 stars 8 forks source link

Layouting #9

Closed mmintel closed 7 years ago

mmintel commented 8 years ago

Hi there, nice plugin.

I have two questions:

  1. how do i use the snippet helper with twig?
  2. is it possible to use twig layouts (blocks) with this plugin?

BR Marc

fvsch commented 8 years ago

Hi Marc.

How do i use the snippet helper with twig?

This plugin doesn’t change the snippet() function, so the function will still look for site/snippets/[snippet name].php and evaluate it as PHP. This is actually useful if you’re migrating from PHP templates and don’t want to rewrite everything. :) And if you want the same feature as snippets but with Twig, you can do includes:

{% include 'partials/something.twig' with {var1: 'value', var2: 'something else'} %}

I tend to put snippet-like Twig code in a subfolder in site/templates, for instance site/templates/partials or site/templates/blocks, to keep things clean and so that they’re never used as the template for a page.

In a future release I might allow plugins users to define a different root for inclusions, currently it’s hardcoded to the templates directory but it could be changed to the site directory, which would allow you to do:

{% include 'snippets/something.twig' with {var1: 'value', var2: 'something else'} %}

But maybe that’s too much options/complexity for little gain.

Is it possible to use twig layouts (blocks) with this plugin?

Definitely. For instance you could have:

site/templates/article.twig
site/templates/default.twig
site/templates/home.twig
site/templates/layout.twig

And in the article template:

{% extends 'layout.twig' %}

{% block content %}
  …
{% endblock %}

{% block sidebar %}
  …
{% endblock %}

(provided your layout.twig, or however you called it, already defines those blocks)