Usage
Bamboo Twig has a lot of advantages and brings a lot of new features to the Twig landscape of Drupal 8.
It boosts performance by using lazy loading, improves the code quality with automated workflow.
It's expected that Bamboo Twig will normalize the way you use twig in your Drupal project reducing the need to install a bunch of extra modules.
Internationalization
Use the bamboo_i18n_format_date filter to return a date string in the correct locality.
datestring - date, timestamp, DrupalDateTimePlus, DateTimePlus or DateTime.typestring (optional) - The format to use, one of the built-in formats: 'short', 'medium', 'long'. Use 'custom' to use $format.formatstring (optional) - PHP date format string suitable for input to date().timezonestring (optional) - Time zone identifier, as described at http://php.net/manual/timezones.php Defaults to the time zone used to display the page.langcodestring (optional) - Language code to translate to. NULL (default) means to use the user interface language for the page.
{# Print the formatted date using Drupal i18n. #}
{{ node.changed.value|bamboo_i18n_format_date('medium') }}
{{ node.changed.value|bamboo_i18n_format_date('custom', 'Y-m-d') }}
Use the bamboo_i18n_current_lang function to return the current lang iso code.
langcodestring (optional) - defaults to current interface language.
{# Print the current language ID. #}
{{ bamboo_i18n_current_lang() }}
The bamboo_i18n_get_translation filter returns the translated entity
{# Get the French translation of an entity #}
{{ entity|bamboo_i18n_get_translation('fr').title.value }}
{# Get the current language translation of an entity #}
{{ entity|bamboo_i18n_get_translation.title.value }}Files
The bamboo_file_extension_guesser filter returns the extension of a file based on its
mimeType.
mimeTypestring
{# Print the extension of the 'application/pdf' mimeType #}
{{ 'application/pdf'|bamboo_file_extension_guesser }}
The bamboo_file_url_absolute function returns absolute url of a given URI or path to a file.
uristring - URI or string path to a file.
{# Print the absolute url to access 'image.jpg' #}
{{ bamboo_file_url_absolute('public://image.jpg') }}
Paths
The bamboo_path_system function returns the relative URL of a system entity.
typestring - one of 'core', 'profile', 'module', 'theme' or 'theme_engine'.itemstring
{# Print the relative URL to the system entity 'theme' named 'stable' #}
{{ bamboo_path_system('theme', 'stable') }}
Loaders
The bamboo_load_currentuser function returns a User object of the current logged user.
{# Load the current user #}
{% set user = bamboo_load_currentuser() %}
The bamboo_load_entity function returns a EntityInterface object of the requested entity.
entity_typestring.idint (optional).-
langcodestring (optional) - defaults to current context language
{# Load the entity node with nid 1 #}
{% set node = bamboo_load_entity('node', 1) %}
Keep in mind, when loading an entity it will fetch it in the current context language.
When you access it directly through a EntityReferenceField or a Paragraph (e.g. node.field_referenced_tags.entity), the entity is always loaded in its original language (it won't be loaded in the current context language or in the entity loaded language).
You should then use the bamboo_i18n_get_translation filter to make sure you have the entity displayed in another language.
{# Load the entity node with nid 1 #}
{% set node = bamboo_load_entity('node', 1) %}
{# Display the entity title in the current context lang (page lang) #}
{{ node.title.value }}
{# Display the referenced entity name in its original lang #}
{{ node.field_referenced_tags.entity.name.value }}
{# Display the ref. entity name in the current context lang (page lang) #}
{{ node.field_referenced_tags.entity|bamboo_i18n_get_translation.name.value }}
The bamboo_load_entity_revision returns a EntityInterface object
of the requested entity revision.
entity_typestring.revision_idint (optional).-
langcodestring (optional) - defaults to current context language
{# Load the entity revision node with revision ID 1 #}
{% set node = bamboo_load_entity_revision('node', 1) %}Keep in mind, when loading an entity it will fetch it in the current context language.
When you access it directly through a EntityReferenceField or a Paragraph (e.g. node.field_referenced_tags.entity), the entity is always loaded in its original language (it won't be loaded in the current context language or in the entity loaded language).
You should then use the bamboo_i18n_get_translation filter to make sure you have the entity displayed in another language.
{# Load the entity revision node with revision ID 1 #}
{% set node = bamboo_load_entity_revision('node', 1) %}
{# Display the entity title in the current context lang (page language) #}
{{ node.title.value }}
{# Display the referenced entity name in its original lang #}
{{ node.field_referenced_tags.entity.name.value }}
{# Display the referenced entity name in the current context lang (page language) #}
{{ node.field_referenced_tags.entity|bamboo_i18n_get_translation.name.value }}
The bamboo_load_fieldfunction returns a render array of an entity field.
field_namestring.entity_typestring.idint (optional).langcodestring (optional).formatterstring (optional).
{# Load the title of node 1 with nid 1 #}
{% set title = bamboo_load_field('title', 'node', 1) %}
The bamboo_load_image function returns a ImageInterface object of the requested image.
pathstring - The path or URI to the original image.
{# Load image with uri 'public://antistatique.jpg' #}
{% set image = bamboo_load_image('public://antistatique.jpg') %}
Render
The bamboo_render_block function returns a render array of the specified block (works only for Block Plugin).
block_namestring.paramsarray (optional).
{# Render the 'system_powered_by_block' block #}
{{ bamboo_render_block('system_powered_by_block') }}
In the case you want to render a Block Entity, you have to use the bamboo_render_entity.
The bamboo_render_entity function returns a render array of the specified entity type. Can render a specific view.
entity_typestring.idint (optional).view_modestring (optional) - machine name of the view mode.langcodestring (optional) - defaults to current language.
{# Render node with nid 1 #}
{{ bamboo_render_entity('node', 1) }}
{# Render the teaser of node with nid 2 #}
{{ bamboo_render_entity('node', 2, 'teaser') }}
{# Render the `stark_messages` Block entity #}
{{ bamboo_render_entity('block', 'stark_messages') }}
The bamboo_render_entity_revision returns a render array of the specified
entity revision type. Can be rendered a specific view_mode.
entity_typestring.revision_idint (optional).view_modestring (optional) - machine name of the view mode.langcodestring (optional) - defaults to current language.
{# Render node with revision id 1 #}
{{ bamboo_render_entity_revision('node', 1) }}
{# Render the teaser of node with revision id 2 #}
{{ bamboo_render_entity_revision('node', 2, 'teaser') }}
The bamboo_render_form function returns a render array of the specified Form.
modulestring.formNamestring.paramsarray (optional).
{# Render a the CronForm #}
{{ bamboo_render_form('system', 'CronForm') }}
The bamboo_render_menu function returns a render array of the specified menu.
menu_namestring.levelint (optional) - defaults to 1.depthint (optional) - defaults to 0.
{# Render a part of the admin menu #}
{{ bamboo_render_menu('admin', 1, 2) }}
The bamboo_render_field function returns a render array of an entity field.
field_namestring.entity_typestring.idint (optional).langcodestring (optional) - defaults to current language.formatterstring (optional) - The formatter that should be used to render the field. Eg. 'text' for textfield or 'url' for linkfield.
{# Render the title of node 1 #}
{{ bamboo_render_field('title', 'node', 1) }}
The bamboo_render_region function returns a render array of the specified region.
regionstring.theme_namestring (optional) - defaults to default theme.
{# Render the sidebar_first region for current theme. #}
{{ bamboo_render_region('sidebar_first') }}
Image Styles
The bamboo_render_image function returns a render array of the specified image file.
fidint.stylesstring.
{# Get thumbnail from image with fid 12. #}
{{ bamboo_render_image(12, 'thumbnail') }}
The bamboo_render_image_style function returns URL string of the specified image path or URI.
pathstring.stylesstring.preprocessboolean - preprocess the image style before first HTTP call.
{# Get thumbnail from image 'public://antistatique.jpg'. #}
{{ bamboo_render_image_style('public://antistatique.jpg', 'thumbnail') }}
Views
The bamboo_render_views function renders the requested view.
viewstring.itemstring.
{# Render the View `who_s_new` block `block_1` #}
{{ bamboo_render_views('who_s_new', 'block_1') }}
Cacheable
The bamboo_attach_cacheable_metadata(cacheable_metadata) returns an array containing cacheable metadata.
cacheable_metadataarray
{# Bubble the User Cache Context. #}
{{ bamboo_attach_cacheable_metadata({'contexts': ['user']}) }}
{# Bubble the Cache Tag. #}
{{ bamboo_attach_cacheable_metadata({'tags': ['node:12']}) }}
{# Bubble the Max-Age. #}
{{ bamboo_attach_cacheable_metadata({'max-age': 12}) }}
Configurations
The bamboo_config_get function returns the specified config.
config_keystring.namestring.
{# Get system mail setting #}
{{ bamboo_config_get('system.site', 'mail') }}
The bamboo_state_get function returns the specified state.
state_keystring.
{# Get system.cron_last from state #}
{{ bamboo_state_get('system.cron_last') }}
The bamboo_settings_get function returns the specified setting.
state_keystring.
{# Get hash_salt from settings #}
{{ bamboo_settings_get('hash_salt') }}
Security
The bamboo_has_role function returns a boolean if the current|given user has the requested role.
rolestring.userint - User id instead of the current logged user..
{# Does the current|given user have the given role ? #}
{{ bamboo_has_role('authenticated') ? 'TRUE' : 'FALSE' }}
The bamboo_has_roles function returns a boolean of the current|given user has the requested roles.
rolesstring[]conjunction(optional) string - The conjunction to use on the set of roles. Only the two values 'AND' or 'OR' are allowed.user(optional) int - User id instead of the current logged user.
{# Does the current user have all the given roles ? #}
{{ bamboo_has_roles(['authenticated', 'administrator']) ? 'TRUE' : 'FALSE' }}
{# Does the current user have at least one of the given roles ? #}
{{ bamboo_has_roles(['authenticated', 'administrator'], 'OR') ? 'TRUE' : 'FALSE' }}
The bamboo_has_permission function returns a boolean if the current|given user has the requested permission.
permissionstringuserint - User id instead of the current logged user.
{# Does the current|given user have the given permission ? #}
{{ bamboo_has_permission('administer site configuration') ? 'TRUE' : 'FALSE' }}
The bamboo_has_permissions function returns a boolean if the current|given user has the requested permissions.
permissionsstring[]conjunction(optional) string - The conjunction to use on the set of permissions. Only the two values 'AND' or 'OR' are allowed.user(optional) int - User id instead of the current logged user.
{# Does the current user have all the given permissions ? #}
{{ bamboo_has_permissions(['administer site configuration', 'bypass node access']) ? 'TRUE' : 'FALSE' }}
{# Does the current user have at least one of the given permissions ? #}
{{ bamboo_has_permissions(['administer site configuration', 'bypass node access'], 'OR') ? 'TRUE' : 'FALSE' }}
Extensions
The bamboo_extensions_truncate filter from Twig-extensions Text.
sentencestring.wordboolean - Truncat at the end of words.ellipsisstring.
{# Truncate a sentence #}
{{ "This is a very long sentence."|bamboo_extensions_truncate(2, false, '...') }}
The *coming soon* bamboo_extensions_truncate_html filter to truncates sentences html and preserves tags.
sentencestring.wordboolean - Truncate at the end of words.ellipsisstring.
{# Truncate a HTML sentence #}
{{ "<p>This <b>is a very</b> long sentence.</p>"|bamboo_extensions_truncate_html(2, false, '...') }}
The bamboo_extensions_shuffle filter from Twig-extensions Array.
arrayarray.
{# Shuffle the given array #}
[1, 2, 3]|bamboo_extensions_shuffle
The bamboo_extensions_time_diff filter from Twig-extensions Date.
datestring - date, timestamp, DrupalDateTimePlus, DateTimePlus or DateTime.
{# Difference between two dates #}
{{ '24-07-2014 17:28:01'|bamboo_extensions_time_diff('24-07-2014 17:28:06') }}
Token
The bamboo_token function substitute a given tokens with appropriate value.
tokenstring.
{# Substitute token #}
{{ bamboo_token('site:name') }}
Core Twig
Drupal core already adds a handful of custom functions that are Drupal specific.
i18n
The trans tag to translate string. See the following link for more details.
Urls
The path functions returns a relative URL given a route name and parameters.
routestring.params(optional) array.options(optional) array.
{# Generates URL from route 'entity.user.canonical'. #}
<a href="{{ path('entity.user.canonical', {'user': user.id}) }}">View user profile</a>
The link functions return <a> tag for the URL.
textstring.urlstring.params(optional) array.
{# Create a link with markup. #}
{{ link('Homepage', item.url, { 'class':['foo', 'bar', 'baz']} ) }}
The file_url function returns a relative url of a given URI or path to a file.
uristring - URI or string path to a file.
{# Generate a relative URI path to the file. #}
{{ file_url('public://antistatique.jpg') }}
Attachments
The attach_library function Attaches an asset library to the template,
and hence to the response.
librarystring.
{# Attaches an asset library to the template. #}
{{ attach_library('classy/node') }}
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion