Bamboo Twig


This project is not covered by Drupal’s security advisory policy.

Bamboo Twig. Drupal 8 powered module.

The Bamboo Twig module provides some Twig extensions with some useful functions
and filters aimed to improve the development experience.

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 also includes automated unit and kernel tests
to ensure stability.

Use Bamboo Twig if

  • You need to "Format dates using Drupal I118n".
  • You need to "Render a Block".
  • You need to "Render a Region".
  • You need to "Render an Entity with view mode".
  • You need to "Load an Entity".
  • You need to "Render a Field".
  • You need to "Retrieve the current user".
  • You need to "Check permissions or roles".
  • You need to "Render a Form".
  • You need to "Deal with image styles".
  • You need to "Use Token".
  • You need to "Create an absolute URL from a theme".
  • You need to "Retrieve the Extension file from given mimeType".
  • You want to use a twig module that follows all the best practices.
  • You want to use a twig module that ensure stability with tests.
  • You want to use a twig module that will be compatible Drupal 9.

Bamboo Twig can do a lot more than that, but perhaps these are some of the
obvious uses of Bamboo Twig.

Performances

For performances reasons, Bamboo Twig has been split into multiple sub-modules
for each topic he provides Twigs.

Bamboo Twig versions

Bamboo Twig is only available for Drupal 8!
The module is ready to be used in Drupal 8, there are no known issues.

If you need some help to upgrade from the version 8.x-1.x to 8.x-2.x check the guide about it.

In the case you are looking for the documentation of version 8.x-1.x, check the previous README.md or the previous project page.

Dependencies

The Drupal 8 version of Bamboo Twig requires nothing !
Feel free to use it.

Similar modules

From the first sight, Bamboo Twig offers similar functionality to the following modules.
It's excepted that Bamboo Twig will normalize the way you use twig
in your Drupal project reducing the need to install a bunch of extra modules.

Plus, it adds a lots of functionality, ensures stability with tests, includes automated quality control and open minded to contribute throught Github PR.
Finally, Bamboo Twig use all the best practices of Drupal 8 to ensure
compatibility with Drupal 9.

Supporting organizations

This project is sponsored by Antistatique. We are a Swiss Web Agency,
Visit us at www.antistatique.net or
Contact us.

Getting Started

We highly recommend you to install the module using composer.

$ composer require drupal/bamboo_twig

You can also install it using the drush or drupal console cli.

$ drush dl bamboo_twig
$ drupal module:install bamboo_twig

Don't forget to enable the modules you need from Bamboo Twig.

Bamboo Twig Extensions

Internationalization

Use the bamboo_i18n_format_date filter to return a date string in the correct locality.

  • datecode> string - date, timestamp, DrupalDateTimePlus, DateTimePlus or DateTime.
  • type string (optional) - The format to use, one of the built-in formats: 'short', 'medium', 'long'. Use 'custom' to use $format.
  • format string (optional) - PHP date format string suitable for input to date().
  • timezone string (optional) - Time zone identifier, as described at https://php.net/manual/timezones.php Defaults to the time zone used to display the page.
  • langcode string (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. #}
<dt>Format date:</dt>
<dd>{{ node.changed.value|bamboo_i18n_format_date('medium') }}</dd>
<dd>{{ node.changed.value|bamboo_i18n_format_date('custom', 'Y-m-d') }}</dd>



Use the bamboo_i18n_current_lang function to return the current lang iso code.

{# Print the current language ID. #}
<dt>Current lang:</dt>
<dd>{{ bamboo_i18n_current_lang() }}</dd>



Files

The bamboo_file_extension_guesser filter returns the extension of a file based on its
mimeType.

  • mimeType string
{# 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.

  • uri string - 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.

  • type string - one of 'core', 'profile', 'module', 'theme' or 'theme_engine'.
  • item string
{# 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_type string.
  • id int (optional).
{# Load the entity node with nid 1 #}
{% set node = bamboo_load_entity('node', 1) %}



The bamboo_load_field function returns a FieldItemListInterface object
of the requested field.

  • field string.
  • entity_type string.
  • id int.
{# 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.

  • path string - 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.

  • block_name string.
  • params array (optional).
{# Render the 'system_powered_by_block' block #}
{{ bamboo_render_block('system_powered_by_block') }}



The bamboo_render_entity function returns a render array of the specified
entity type. Can render a specific view.

  • entity_type string.
  • id int (optional).
  • view_mode string (optional) - machine name of the view mode.
  • langcode string (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') }}



The bamboo_render_form function returns a render array of the specified Form.

  • module string.
  • formName string.
  • params array (optional).
{# Render a the CronForm #}
{{ bamboo_render_form('system', 'CronForm') }}



The bamboo_render_menu function returns a render array of the specified menu.

  • menu_name string.
  • level int (optional) - defaults to 1.
  • depth int (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_name string.
  • entity_type string.
  • id int (optional).
  • view_mode string - defaults to "default".
  • langcode string - defaults to current language.
{# 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.

  • region string.
  • theme_name string (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.

  • fid int.
  • styles string.
{# Get thumbnail from image with fid 12. #}
{{ bamboo_render_image(1, 'thumbnail') }}



The bamboo_render_image_style function returns URL string of the
specified image path or URI.

  • path string.
  • styles string.
  • preprocess boolean - preprocess the image style before first HTTP call.
{# Get thumbnail from image 'public://antistatique.jpg'. #}
{{ bamboo_render_image_style('public://antistatique.jpg', 'thumbnail') }}



Configurations

The bamboo_config_get function returns the specified config.

  • config_key string.
  • name string.
{# Get system mail setting #}
{{ bamboo_config_get('system.site', 'mail') }}



The bamboo_state_get function returns the specified state.

  • state_key string.
{# Get system.cron_last from state #}
{{ bamboo_state_get('system.cron_last') }}



The bamboo_settings_get function returns the specified setting.

  • state_key string.
{# Get hash_salt from settings #}
{{ bamboo_settings_get('hash_salt') }}



Security

The bamboo_has_role function returns a boolean of the current|given user
has the requested role.

  • role string.
  • user int - User id instead of the current logged user..
{# Does the current|given user has the given role ? #}
{{ bamboo_has_role('authenticated') ? 'TRUE' : 'FALSE' }}



The bamboo_has_permission function returns a boolean of the current|given user
has the requested permission.

  • permission string
  • user int - User id instead of the current logged user.
{# Does the current|given user has the given permission ? #}
{{ bamboo_has_permission('administer site configuration') ? 'TRUE' : 'FALSE' }}



Extensions

The bamboo_extensions_truncate filter from Twig-extensions Text.

  • sentence string.
  • word boolean - Truncat at the end of words.
  • ellipsis string.
{# 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.

  • sentence string.
  • word boolean - Truncate at the end of words.
  • ellipsis string.
{# Truncate a HTML sentence #}
{{ "<p>This <b>is a very</b> long sentence.</p>"|bamboo_truncate_html(2, false, '...') }}



The bamboo_extensions_shuffle filter from Twig-extensions Array.

  • array array.
{# Shuffle the given array #}
[1, 2, 3]|bamboo_extensions_shuffle



The bamboo_extensions_time_diff filter from Twig-extensions Date.

  • date string - 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.

  • token string.
{# Substitute token #}
{{ bamboo_token('site:name') }}

Core Twig

Drupal core already adds a handful of custom functions that are Drupal specific.



Views

The views_embed_view function renders the requested view.

  • view string.
  • item string.
{# Render the View 'who_s_new' block 'block_1' #}
{{ views_embed_view('who_s_new', 'block_1') }}



i18n

The trans tag to translate string.

See the following link for more details.



Urls

The path functions returns an absolute URL given a route name and parameters.

  • route string.
  • 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.

  • text string.
  • url string.
  • 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.

  • uri string - 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.

  • library string.
{# Attaches an asset library to the template. #}
{{ attach_library('classy/node') }}


Supporting organizations: 

Bamboo Twig项目的Drush安装命令:复制到剪贴板

注:个别模块仍需开启相关子模块。
周安装量: 
6
维护状态: 
最低限度维护
开发状态: 
积极开发中

其他版本下载:

版本下载地址发布日期发布说明翻译下载
8.x-2.0tar.gz (45.1 KB) | zip (76.11 KB)2017年6月6日发布说明简 | 繁 | 更多
8.x-1.0tar.gz (12.73 KB) | zip (23.34 KB)2017年3月19日发布说明简 | 繁 | 更多

开发版本下载:

版本下载地址发布日期发布说明翻译下载
8.x-2.x-devtar.gz (45.1 KB) | zip (76.12 KB)2017年6月6日发布说明简 | 繁 | 更多
8.x-1.x-devtar.gz (19.44 KB) | zip (38.08 KB)2017年3月30日发布说明简 | 繁 | 更多