murph-doc/docs/template.md

101 lines
3.8 KiB
Markdown
Raw Permalink Normal View History

2021-06-04 16:31:16 +02:00
# Templating
## Variables
By default, these variables are given to a CMS view:
2022-03-01 16:23:15 +01:00
* 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`
2021-06-04 16:31:16 +02:00
2022-03-01 16:23:15 +01:00
## Navigations and menus
2021-06-04 16:31:16 +02:00
2022-03-01 16:23:15 +01:00
* Retrieve all navigations: `_store.navigations`
* Retrieve a navigation by its code: `_store.navigation('the_code')`
2021-06-04 16:31:16 +02:00
2022-03-01 16:23:15 +01:00
* 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})`
2021-06-04 16:31:16 +02:00
2022-03-01 16:23:15 +01:00
* 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)`
2021-06-04 16:31:16 +02:00
2022-03-01 16:23:15 +01:00
## Page
2021-06-04 16:31:16 +02:00
2022-03-01 16:23:15 +01:00
You can access a page's blocks this way:
2021-06-04 16:31:16 +02:00
2023-02-09 21:50:06 +01:00
```twig
2022-03-01 16:23:15 +01:00
{% set myBlock = _page.myBlock.value %}
2021-06-04 16:31:16 +02:00
2022-03-01 16:23:15 +01:00
{{ myBlock }}
2021-06-04 16:31:16 +02:00
```
## URL and path
Murph has somes twig functions to manage URLs:
### Generic functions
2022-02-01 14:19:56 +01:00
* 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) }}`
2021-06-04 16:31:16 +02:00
### Node functions
2022-02-01 14:19:56 +01:00
* 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) }}`
2021-06-04 16:31:16 +02:00
2022-03-01 16:23:15 +01:00
A node may have a disabled URL:
2023-02-09 21:50:06 +01:00
```twig
2022-03-01 16:23:15 +01:00
{% 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:
2023-02-09 21:50:06 +01:00
```twig
2022-03-01 16:23:15 +01:00
{% set path = safe_node_path(node, {_domain: _domain}) %}
{% set url = safe_node_url(node, {_domain: _domain}) %}
```
2022-03-16 12:43:50 +01:00
## 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) }}`
2021-06-04 16:31:16 +02:00
### Filters
2022-04-19 11:38:55 +02:00
When a content could contains tags (eg: '{{url://my_route}}`), use `murph_url`. See the example below:
2021-06-04 16:31:16 +02:00
| 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.
2022-03-01 16:23:15 +01:00
## 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 |