Skip to content

Instantly share code, notes, and snippets.

@WengerK
Last active December 31, 2019 22:37
Show Gist options
  • Save WengerK/065c0f741a8affee1f58eb8baf811461 to your computer and use it in GitHub Desktop.
Save WengerK/065c0f741a8affee1f58eb8baf811461 to your computer and use it in GitHub Desktop.
Article Ressources - Drupal 8 - Bamboo Twig, Beyond Twig

Article Ressources - Drupal 8 - Bamboo Twig, Beyond Twig

This is the Gist repository for my article Drupal 8 - Bamboo Twig, Beyond Twig.

Be aware that this article has been wrote for the Blog of Antistatique — Web Agency in Lausanne, Switzerland. A place where I work as Full Stack Web Developer.

Feel free to read it the full article on Medium or check it out on Antistatique.

Content of this Gist :

  • bamboo_twig_renderer.html.twig : Renderer
  • bamboo_twig_loader.html.twig : Loader
  • bamboo_twig_i18n.html.twig : Internationalisation
  • bamboo_twig_file.html.twig : Files
  • bamboo_twig_extensions.html.twig : Twig Extensions
  • bamboo_twig_config.html.twig : Configurations
  • bamboo_twig_token.html.twig : Token
  • bamboo_twig_security.html.twig : Security
{# Get Settings #}
{{ bamboo_settings_get('hash_salt') }}
{# Get Config #}
{{ bamboo_config_get('system.site', 'mail') }}
{# Get State #}
{{ bamboo_state_get('system.cron_last') }}
{# String manipulations #}
{{ "This is a very long sentence."|bamboo_extensions_truncate(23, true, '...') }}
{# Array manipulations #}
{% set ar = {'a': 'apple', 'b': 'orange', 'c': 'citrus'}|bamboo_extensions_shuffle %}
{# Time diff #}
{{ '24-07-2014 17:28:01'|bamboo_extensions_time_diff('24-07-2014 17:28:02') }}
{# Pad a string #}
{{ "Alien"|bamboo_extensions_strpad(10, ' ') }}
{{ bamboo_file_url_absolute('public://drupal.jpg') }}
<time datetime="{{ node.field_published_at.date|date('c') }}">
{{ node.field_published_at.date|bamboo_i18n_format_date('long_date_only') }}
</time>
{# Get the entity translations in the current context lang (page language) #}
{{ entity|bamboo_i18n_get_translation.title.value }}
{# Get the entity translations in french #}
{{ entity|bamboo_i18n_get_translation('fr').title.value }}
{# Load the current user #}
{% set user = bamboo_load_currentuser() %}
{# Load the entity node with nid 1 #}
{% set node = bamboo_load_entity('node', 1) %}
{# show the entity title in the current context lang (page language) #}
{{ node.title.value }}
{# Keep in mind, when loading an entity it will fetch it in the current context lang. #}
{# But When you will access a *EntityReferenceField* or *Paragraph*, #}
{# the given entity is always in his own original language #}
{# (not in the current context lang neither in the entity lang). #}
{# You should then use the `bamboo_i18n_get_translation` filter. #}
{# show the entity name in his original lang #}
{{ node.field_referenced_tags.entity.name.value }}
{# show the entity name in the current context lang (page language) #}
{{ node.field_referenced_tags.entity|bamboo_i18n_get_translation.name.value }}
{# Render node with teaser viewmode #}
{% for article in articles %}
{{ bamboo_render_entity('node', article.nid.value, 'teaser') }}
{% endfor %}
{# Render the `system_powered_by_block` block #}
{{ bamboo_render_block('system_powered_by_block') }}
{# Render a the CronForm #}
{{ bamboo_render_form('system', 'CronForm') }}
{# Render the title of node 1 #}
{{ bamboo_render_field('title', 'node', 1) }}
{# Get thumbnail HTML markup from image with fid 12. #}
{{ bamboo_render_image(12, 'thumbnail') }}
{# Get thumbnail URL from image `public://antistatique.jpg`. #}
{{ bamboo_render_image_style('public://antistatique.jpg', 'thumbnail') }}
{# Does the current user has the given role/permission ? #}
{{ bamboo_has_role('anonymous') }}
{{ bamboo_has_permission('administre site configuration') }}
{# Does the current user has the given roles ? #}
{{ bamboo_has_roles(['authenticated', 'administrator']) }}
{{ bamboo_has_roles(['authenticated', 'administrator'], 'OR') }}
{# Does the current user has the given roles ? #}
{{ bamboo_has_permissions(['administre site configuration']) }}
{{ bamboo_has_permissions(['administre site configuration', 'bypass node access'], 'OR') }}
{{ bamboo_token('site:name') }}
{% set article = bamboo_load_entity('node', 1) %}
{{ bamboo_token('node:title', {'node': article}) }}
{# Afficher un Block Entity #}
{# Render the `stark_messages` Block entity #}
{{ bamboo_render_entity('block', 'stark_messages') }}
{# Afficher un Block Plugin #}
{# Render the 'system_powered_by_block' block #}
{{ bamboo_render_block('system_powered_by_block') }}
{# Afficher le node N°1 #}
{{ bamboo_render_entity('node', 1) }}
{{ bamboo_render_entity('node', 1, 'teaser') }}
{# Afficher un Paragraph #}
{{ bamboo_render_entity('paragraph', paragraph.target_id) }}
{# Afficher tout les paragraphs, attachés dans le champ `field_paragraphs` de mon node #}
{% for paragraph in node.field_paragraphs %}
{{ bamboo_render_entity('paragraph', paragraph.target_id) }}
{% endfor %}
{# Afficher un Contact Form, attachés dans le champ `field_contact_form` de mon node #}
{{ bamboo_render_entity('contact_form', node.field_contact_form.target_id) }}
{# Afficher un formulaire #}
{{ bamboo_render_form('system', 'CronForm') }}
{# Afficher un champ spécifique #}
{{ bamboo_render_field('title', 'node', 1) }}
{# Afficher une image (fid 1) selon un ImageStyle #}
{{ bamboo_render_image(1, 'thumbnail') }}
{# Afficher l'URL d'une une image selon un ImageStyle #}
{# Get thumbnail from image 'public://antistatique.jpg'. #}
{{ bamboo_render_image_style('public://antistatique.jpg', 'thumbnail') }}
{# Afficher une views #}
{{ bamboo_render_views('who_s_new', 'block_1') }}
{# Couper une chaîne de caractères trop longue #}
{{ "This is a very long sentence."|bamboo_extensions_truncate(2, false, '...') }}
{# Utiliser Token #}
{{ bamboo_token('site:name') }}
{# Afficher la différence de jours entre deux dates #}
{{ '24-07-2014 17:28:01'|bamboo_extensions_time_diff('24-07-2014 17:28:06') }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment