2017 © Pedro Peláez
 

library kirby-twig

Twig templating support for Kirby CMS

image

fvsch/kirby-twig

Twig templating support for Kirby CMS

  • Friday, September 29, 2017
  • by fvsch
  • Repository
  • 4 Watchers
  • 71 Stars
  • 211 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 9 Forks
  • 3 Open issues
  • 15 Versions
  • 6 % Grown

The README.md

Twig Plugin for Kirby CMS

, (*1)

  • Adds support for Twig templates to Kirby CMS 2.x.
  • PHP templates still work, you don’t have to rewrite them if you don’t want to.

What it looks like

Before:, (*2)

<?php /* site/templates/hello.php */ ?>
<h1><?= $page->title() ?></h1>
<ul>
<?php foreach ($page->children() as $child): ?>
  <li><a href="<?= $child->url() ?>"><?= $child->title() ?></li>
<?php endforeach; ?>
</ul>

After:, (*3)

{# site/templates/hello.twig #}
<h1>{{ page.title }}</h1>
<ul>
{% for child in page.children %}
  <li><a href="{{ child.url }}">{{ child.title }}</li>
{% endfor %}
</ul>

Installation

Standard installation

  1. Download the latest release.
  2. Unzip, rename the kirby-twig-main folder to just twig and put it in your project’s site/plugins folder.

You should end up with a folder structure like this:, (*4)

site
 └─ plugins
     └─ twig
         ├─ lib
         ├─ src
         └─ twig.php

Using Composer

Require fvsch/kirby-twig in your Composer dependencies:, (*5)

composer require fvsch/kirby-twig:^3.0

Then, make sure your Kirby installation is autoloading Composer dependencies on both frontend and panel. For this, the safest way is to create your own custom plugin., (*6)

site
 └─ plugins
     └─ composer
         └─ composer.php
<?php
// site/plugins/composer/composer.php

// Composer autoload
require_once kirby()->roots()->index() . '/vendor/autoload.php';

Finally, register the plugin by adding this line to your newly created site/plugins/composer/composer.php, after having required the autoloader., (*7)

// Register the Twig plugin's template component
Kirby\Twig\Plugin::register();

Usage

Page templates

Now that the plugin is installed and active, you can write Twig templates in the site/templates directory. For example, if the text file for your articles is named post.txt, you could have a post.twig template like this:, (*8)

{% extends '@templates/layout.twig' %}
{% block content %}
  <article>
    <h1>{{ page.title }}</h1>
    {{ page.text.kirbytext | raw }}
  </article>
{% endblock %}

See the {% extends '@templates/layout.twig' %} and {% block content %} parts? They’re a powerful way to manage having a common page layout for many templates, and only changing the core content (and/or other specific parts). Read our Twig templating guide for more information., (*9)

Rendering a template in PHP: the twig helper

This plugin also enables a twig PHP function for rendering template files and strings, like this:, (*10)

<?php

// Render a simple template from the site/snippets directory
echo twig('@snippets/header.twig');

// Same, but passing some additionnal variables
echo twig('@snippets/header.twig', ['sticky'=>false]);

// Render a string
echo twig('Hello {{ who }}', ['who'=>'World!']);

If you work with Twig templates for pages, you might not need the twig() helper at all. But it can be useful when working with the Modules and Patterns plugins., (*11)

More documentation

Recommended reads:, (*12)

Other topics:, (*13)

Credits

The Versions

29/09 2017

dev-composer

dev-composer https://github.com/fvsch/kirby-twig/

Twig templating support for Kirby CMS

  Sources   Download

MIT

The Requires

 

by Florens Verschelde

29/09 2017

v3.0.1

3.0.1.0 https://github.com/fvsch/kirby-twig/

Twig templating support for Kirby CMS

  Sources   Download

MIT

The Requires

 

by Florens Verschelde

01/04 2017

v1.3.0

1.3.0.0

  Sources   Download

01/04 2017

v2.0.0

2.0.0.0

  Sources   Download

01/04 2017

v2.0.1

2.0.1.0

  Sources   Download

01/04 2017

dev-develop

dev-develop

  Sources   Download

19/01 2017

dev-master

9999999-dev https://github.com/fvsch/kirby-twig/

Twig templating support for Kirby CMS

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Florens Verschelde

19/01 2017

v3.0.0

3.0.0.0 https://github.com/fvsch/kirby-twig/

Twig templating support for Kirby CMS

  Sources   Download

MIT

The Requires

 

by Florens Verschelde

17/01 2017

v3.0.0-beta1

3.0.0.0-beta1 https://github.com/fvsch/kirby-twig/

Twig templating support for Kirby CMS

  Sources   Download

MIT

The Requires

 

by Florens Verschelde

06/12 2016

2.1.2

2.1.2.0 https://github.com/fvsch/kirby-twig/

Twig templating support for Kirby CMS

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Florens Verschelde

04/10 2016

v2.1.1

2.1.1.0 https://github.com/fvsch/kirby-twig/

Twig templating support for Kirby CMS

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Florens Verschelde

03/10 2016

v2.1.0

2.1.0.0 https://github.com/fvsch/kirby-twig/

Twig templating support for Kirby CMS

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Florens Verschelde

02/09 2016

v2.0.2

2.0.2.0 https://github.com/fvsch/kirby-twig/

Twig templating support for Kirby CMS

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Florens Verschelde

06/05 2016

v1.2.0

1.2.0.0

Twig Plugin for Kirby CMS

  Sources   Download

MIT

The Requires

 

12/04 2016

v1.0.0

1.0.0.0

Twig Plugin for Kirby CMS

  Sources   Download

MIT

The Requires