murph-doc/docs/template.md
Simon Vieille 385af624a6
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
update theme
2023-02-09 21:50:06 +01:00

101 lines
3.8 KiB
Markdown

# Templating
## Variables
By default, these variables are given to a CMS view:
* The current node is `_node` and its menu is `_menu`
* The current navigation is `_navigation`
* The current locale is `_locale`
* The CMS store is `_store`
## Navigations and menus
* Retrieve all navigations: `_store.navigations`
* Retrieve a navigation by its code: `_store.navigation('the_code')`
* Retrieve all navigation menus: `_navigation.menus`
* Retrieve a menu by its code: `_navigation.menu('the_code')`
* Retrieve all nodes of a menu: `menu.rootNode.children`
* Retrieve visible nodes of a menu: `menu.rootNode.children({visible: true})`
* Test if a node is the current one: `_store.isActiveNode(node)`
* Test if a node is or contains the current one: `_store.isActiveNode(node, true)`
## Page
You can access a page's blocks this way:
```twig
{% set myBlock = _page.myBlock.value %}
{{ myBlock }}
```
## URL and path
Murph has somes twig functions to manage URLs:
### Generic functions
* Same as [twig url](https://symfony.com/doc/current/reference/twig_reference.html#url) but catches all exceptions: `{{ safe_url(routeName, options, relative) }}`
* Same as [twig path](https://symfony.com/doc/current/reference/twig_reference.html#path) but catches all exceptions: `{{ safe_path(routeName, options, relative) }}`
### Node functions
* Generates a URL using a node: `{{ node_url(node, options, relative) }}`
* Generates a path using a node: `{{ node_path(node, options, relative) }}`
* Generates a URL using a node and catches all exceptions: `{{ safe_node_url(node, options, relative) }}`
* Generates a path using a node and catches all exceptions: `{{ safe_node_path(node, options, relative) }}`
A node may have a disabled URL:
```twig
{% if not node.disableUrl %}
{% set path = safe_node_path(node) %}
{% set url = safe_node_url(node) %}
{% endif %}
```
When the navigation has several domains, you can specify the domain:
```twig
{% set path = safe_node_path(node, {_domain: _domain}) %}
{% set url = safe_node_url(node, {_domain: _domain}) %}
```
## Code functions
* Generates a URL using codes: `{{ code_url(menuCode, nodeCode, options, relative) }}`
* Generates a path using codes: `{{ code_path(menuCode, nodeCode, options, relative) }}`
* Generates a URL using codes and catches all exceptions: `{{ safe_code_url(menuCode, nodeCode, options, relative) }}`
* Generates a path using codes and catches all exceptions: `{{ safe_code_path(menuCode, nodeCode, options, relative) }}`
### Filters
When a content could contains tags (eg: '{{url://my_route}}`), use `murph_url`. See the example below:
| Code | Output |
| ---- | ------ |
| `{{ content }}` | `A link to the <a href="{{url://contact}}">contact page</a>` |
| `{{ content|murph_url }}` | `A link to the <a href="https://example.com/contact">contact page</a>` |
## String builder
The string builder builds a string using a format and an object or an array.
Examples:
* `{{ 'Entity ID is {id}'|build_string(myEntity) }}` will output: `Entity ID is 42`
* `{{ 'Hello, {user.displayName}!'|build_string(myEntity) }}` will output `Hello, John doe!`
In case of an not accessible property, no exception will be thrown.
## File attributes
Attributes are managed from the file manager. They are accessibles with these filters:
| Code | Result |
| ------ | ------ |
| `{{ '<img ... alt="{{fattr://hash/alt}}">'|file_attributes }}` | `<img ... alt="Attribute 'alt' of the file with the given hash">` |
| `{{ 'path/to/file'|file_attribute('alt') }}` | Attribute `alt` of the given file |