https://github.com/afbora/kirby-latte
Enable Latte Template Engine for Kirby 3
https://github.com/afbora/kirby-latte
Last synced: 9 months ago
JSON representation
Enable Latte Template Engine for Kirby 3
- Host: GitHub
- URL: https://github.com/afbora/kirby-latte
- Owner: afbora
- License: mit
- Created: 2020-12-07T12:37:06.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2020-12-07T12:39:05.000Z (over 5 years ago)
- Last Synced: 2025-05-17T08:11:40.228Z (about 1 year ago)
- Language: PHP
- Size: 98.6 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Kirby Latte ☕
[](https://github.com/afbora/kirby-latte)
[](https://github.com/afbora/kirby-latte)
[](https://github.com/afbora/kirby-latte)
[](https://github.com/afbora/kirby-latte)
[](https://github.com/afbora/kirby-latte)
[](https://github.com/afbora/kirby-latte)
This plugin use Latte `latte/latte` 2.x package and compatible with Kirby 3. For detailed information, you can browse the [Latte documents](https://latte.nette.org/en/guide).
## Installation
### Installation with composer
```ssh
composer require afbora/kirby-latte
```
### Add as git submodule
```ssh
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](https://latte.nette.org/en/guide).
## 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:
```php
'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:
```php
'afbora.kirby-latte.tempDirectory' => '/site/storage/temp',
```
### Filters
[Filters Documentation](https://latte.nette.org/en/develop#toc-custom-filters)
Filters are functions that change or format the data to a form we want. [The built-in filters which are available](https://latte.nette.org/en/filters).
#### Usage filters
Latte allows calling filters by using the pipe sign notation (preceding space is allowed):
````php
{$heading|upper}
````
Custom filters can be registered this way:
```php
'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:
```php
{$text|shortify}
{$text|shortify:100}
```
### Functions
[Functions Documentation](https://latte.nette.org/en/develop#toc-functions)
In Latte you can use all PHP functions and at the same time define your own:
```php
'afbora.kirby-latte.functions' => [
'random' => function (...$args) {
return array_rand($args);
}
],
```
The usage is then the same as when calling the PHP function:
```php
{random('apple', 'orange', 'lemon')} // prints for example: apple
```
### Macros (User-defined Tags)
[Macros Documentation](https://latte.nette.org/en/develop#toc-user-defined-tags)
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:
```php
'afbora.kirby-latte.macros' => [
[
'try', // tag name
'try {', // PHP code replacing the opening brace
'} catch (\Exception $e) {}' // code replacing the closing brace
]
]
```