Twig Templating

Syntax

Basic examples:

Templates

If you want to use twig templates not accessible from the public folder, you must use a separate template folder in the site config via the path_templates parameter.
This folder has the twig namespace @templates.
If you don't use a twig namespace, twig will look for files in the directory path_pages

Example of template inheritance : {% extends "@templates/base.twig" %}

Provided with this project

Variable app

The app variable is a readonly service container which exposes only a few services.

description example result
app.config website config {{ app.config.path_public }} /app/demo/public
{{ app.config.log_max_files }} 7
app.db db object that allows access to all yaml tables {{ app.db.table('persons').get(10).name }} Lindon
{{ app.db.table('persons')|length }} 100
app.logger for logging {{ app.logger.error('Hello World !') }}
app.request object ServerRequestInterface of PSR-7 HTTP message interfaces {{ app.request.uri.path }} /menu/templating
{{ app.request.uri }} https://simplesite.arnapou.net/menu/templating
app.version Simplesite version {{ app.version }} v8.3@dev

Variable view

The view variable is an immutable object which represents the current view.

description example result
view.scope string $scope {{ view.scope }} @pages
view.path string $path {{ view.path }} /menu/templating.twig
view.name string $name {{ view.name }} @pages/menu/templating.twig
view.isDir bool $isDir {{ view.isDir|json_encode }} false
view.isFile bool $isFile {{ view.isFile|json_encode }} true
view.exists bool $exists {{ view.exists|json_encode }} true
view.root root(): self {{ view.root }} @pages
view.dirname dirname(int $levels = 1): self {{ view.dirname }} @pages/menu
{{ view.dirname(1).basename }} menu
{{ view.dirname(2) }} @pages
view.basename basename(): string {{ view.basename }} templating.twig
view.extension extension(): string {{ view.extension }} twig
view.relative relative(string $relative): self {{ view.relative('../foo.ext') }} @pages/foo.ext
view.list

list(): array<self>
  ╰─ dirs, then files

list('d'): array<self>
  ╰─ only dirs

list('f'): array<self>
  ╰─ only files

{{ view.dirname.list
   | map(x => x.basename)
   | join("\n") | nl2br }}
database.twig
errors.html
images.twig
pages.twig
pages.yaml
php.html
templating.twig
{{ view.relative('..').list('d')
   | join("\n") | nl2br }}
@pages/menu
{{ view.relative('..').list('f')
   | join("\n") | nl2br }}
@pages/index.twig
@pages/php.png
@pages/test.json

Functions

description example result
asset calculates the path of an asset {{ asset('../assets/twig.png') }} /../assets/twig.png
data parse a file to get the data, works only for yaml and json {{ data('@pages/menu/pages.yaml').address.street }} Santa Claus Lane
{{ data('@pages/test.json').1.info.email }} xbahringer@hotmail.com
path calculates the path of a link {{ path('static_dir', { path: 'menu/pages' }) }} /menu/pages/
{{ path('static_page', { path: 'menu/templating' }) }} /menu/templating
path_dir shortcut of path('static_dir', ...) {{ path_dir('pages') }} /pages/
path_page shortcut of path('static_page', ...) {{ path_page('menu/templating') }} /menu/templating
thumbnail convert the image path to a thumbnail path {{ thumbnail('../assets/twig.png', 50) }} /../assets/twig.50.png

Filters

description example result
basename strip directory and suffix from filenames {{ '@pages/menu/php.html'|basename }} php.html
camel convert a text into camelCase {{ 'Hello World!'|camel }} helloWorld
debug_type display the type of the object {{ 'Hello'|debug_type }} string
dirname strip last component from file name {{ '@pages/menu/php.html'|dirname }} @pages/menu
getenv get the env variable {{ 'PHP_VERSION'|getenv }} 8.4.2
markdown_to_html convert markdown to html
{% apply markdown_to_html %}
Title
=====

Hello
World!
{% endapply %}
<h1>Title</h1>
<p>Hello!
World!</p>
minify_html removes unnecessary html spaces
{% apply minify_html %}
    <p>
        Hello <b>World !</b>
    </p>
{% endapply %}
<p>Hello <b>World !</b></p>
path_dir url of a folder path {{ 'pages'|path_dir }} /pages/
path_page url of a page path {{ 'menu/templating'|path_page }} /menu/templating
slug simplifies a text by converting the characters (useful for example for anchors) {{ 'Wôrķšƥáçè ~~sèťtïñğš~~ 5€'|slug }} workspace-settings-5eur
snake convert a text into snake_case {{ 'Hello World!'|snake }} hello_world
thumbnail convert the path of an image to that of its thumbnail {{ '../assets/twig.png'|thumbnail(50) }} /../assets/twig.50.png
view get a view object from its string representation {{ '@public/assets'|view }} @public/assets
yaml basically a yaml encode {{ {foo: 'bar', baz: 123}|yaml }}
foo: bar
baz: 123