texnixe / kirby-latte

Enable Latte Template Engine for Kirby 3

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Kirby Latte ☕

Source Download Open Issues Last Commit Release License

This plugin use Latte latte/latte 2.x package and compatible with Kirby 3. For detailed information, you can browse the Latte documents.

Installation

Installation with composer

composer require afbora/kirby-latte

Add as git submodule

git submodule add https://github.com/afbora/kirby-latte.git site/plugins/kirby-latte

What is Latte?

According to Latte documentation is:

Latte is a template engine for PHP which eases your work and ensures the output is protected against vulnerabilities, such as XSS.

  • Latte is fast: it compiles templates to plain optimized PHP code.
  • Latte is secure: it is the first PHP engine introducing context-aware escaping and link checking.
  • Latte speaks your language: it has intuitive syntax and helps you to build better websites easily.

Usage

It will be sufficient to upload the latte file to the templates folder according to the template name. For ex: /site/templates/default.latte for default template.

All the documentation about Latte is in the official documentation.

Options

The default values of the package are:

Option Default Values Description
afbora.kirby-latte.templatesDirectory site/templates (string) Location of the templates
afbora.kirby-latte.tempDirectory site/cache/temp (string) Location of the cached templates
afbora.kirby-latte.filters [] (array) Array with the custom filters
afbora.kirby-latte.functions [] (array) Array with the custom functions
afbora.kirby-latte.macros [] (array) Array with the custom macros
afbora.kirby-latte.autoRefresh true (boolean) Automatically regenerates the caches

All the values can be updated in the config.php file.

Templates

Default templates folder is site/templates directory or wherever you define your templates directory, but you can change this easily:

'afbora.kirby-latte.templatesDirectory' => '/theme/default/templates',

You can find latte templates for Kirby Starterkit in repository /templates folder.

├── layouts
│   └── default.latte
├── blocks
│   └── intro.latte
├── about.latte
├── album.latte
├── default.latte
├── home.latte
├── note.latte
├── notes.latte
└── photography.latte

All the views generated are stored in site/cache/temp directory or wherever you define your cache directory, but you can change this easily:

'afbora.kirby-latte.tempDirectory' => '/site/storage/temp',

Filters

Filters Documentation

Filters are functions that change or format the data to a form we want. The built-in filters which are available.

Usage filters

Latte allows calling filters by using the pipe sign notation (preceding space is allowed):

<h1>{$heading|upper}</h1>

Custom filters can be registered this way:

'afbora.kirby-latte.filters' => [
    'shortify' => function (string $s): string {
        return mb_substr($s, 0, 10); // shortens the text to 10 characters
    }
]

We use it in a template like this:

<p>{$text|shortify}</p>
<p>{$text|shortify:100}</p>

Functions

Functions Documentation

In Latte you can use all PHP functions and at the same time define your own:

'afbora.kirby-latte.functions' => [
    'random' => function (...$args) {
        return array_rand($args);
    }
],

The usage is then the same as when calling the PHP function:

{random('apple', 'orange', 'lemon')} // prints for example: apple

Macros (User-defined Tags)

Macros Documentation

Latte provides API for making your own tags. It isn't difficult at all. Tags are added in sets (a set can consist of a single tag).

In Latte you can use all PHP functions and at the same time define your own:

'afbora.kirby-latte.macros' => [
    [
        'try', // tag name
	    'try {',  // PHP code replacing the opening brace
	    '} catch (\Exception $e) {}' // code replacing the closing brace
    ]
]

About

Enable Latte Template Engine for Kirby 3

License:MIT License


Languages

Language:PHP 55.3%Language:Latte 44.7%