2017 © Pedro Peláez
 

drupal-module twigshim

Shoehorns Twig template compiling into Drupal 7.

image

derekderaps/twigshim

Shoehorns Twig template compiling into Drupal 7.

  • Tuesday, April 3, 2018
  • by derekderaps
  • Repository
  • 8 Watchers
  • 4 Stars
  • 2,064 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 6 Open issues
  • 9 Versions
  • 0 % Grown

The README.md

Twigshim

Shoehorns Twig template compiling into Drupal 7. Especially useful when you already have twig templates rendering your components in another system (e.g., an external styleguide) and don't want to re-implement everything in PHPTemplate., (*1)

Installation

Download the Twig library

This project depends on the Twig PHP library, which can be installed one of three ways:, (*2)

  1. Composer Manager* (recommended)
  2. Composer
  3. Libraries API*

via Composer Manager

This is the recommended installation method. Install the Composer Manager module* and follow its installation instructions to generate your composer.json and download your vendor dependencies. No extra work is required beyond the steps outlined in the Composer Manager project., (*3)

or via Composer

Follow the installation instructions for the main Composer project, then cd into this module's directory (probably sites/all/modules/contrib/twigshim) and run composer install., (*4)

or via Libraries API

Install the Libraries API module* and download the Twig library into your libraries directory (probably sites/all/libraries)., (*5)

Install the Twig Shim module

Follow the normal Drupal module installation procedure.*, (*6)


*_For instructions on installing Drupal modules, see the Drupal.org documention._, (*7)

Usage

If your templates live anywhere other than in the templates subdirectory of your default theme, visit /admin/conig/development/twigshim and set the appropriate path. All your template references resolve relative to this path. Do not include leading or trailing slashes., (*8)

The main Twig Shim function is twigshim_render(). Call this with your template path (relative to the templates directory you just set) and an optional array of variables to be used in the template. Example:, (*9)

/**
 * Implements hook_block_view().
 */
function mymodule_block_view($delta = '') {
  switch ($delta) {

    // Render the header block.
    case 'myblock':
      return twigshim_render('chrome/header.twig', array(
        'campaign'  => variable_get('mysite_current_campaign', 'Donate now!'),
        'hidePopup' => !user_is_anonymous(),
      ));
      break;
  }
}

When rendering an entity, use the helper function twigshim_render_entity() to automatically pull each of the entity's fields and properties into the variables array. It strips field_ from the front of each key, facilitating the re-use of variables across both your style guide and implementation., (*10)

/**
 * Implements theme_HOOK() for paragraphs_item__button().
 */
function mytheme_paragraphs_item__button(&$vars) {

  // Get a wrapper for the button entity.
  $button = entity_metadata_wrapper('paragraphs_item', $vars['paragraphs_item']);

  // Set values for any fields that don't follow the standard format.
  $template_vars['href'] = $button->field_href->url->value();

  return twigshim_render_entity('paragraphs_item', $button, 'button/button.twig', $template_vars);
}

Note that it is the caller's responsibility to properly sanitize all variables before passing into Twig Shim. See the Drupal.org documentation on writing secure code and the sanitisation functions., (*11)

The Versions

03/04 2018

dev-7.x-1.x

dev-7.x-1.x

Shoehorns Twig template compiling into Drupal 7.

  Sources   Download

GPL-2.0+

The Requires

 

13/03 2018

dev-soniktrooth-patch-1

dev-soniktrooth-patch-1

Shoehorns Twig template compiling into Drupal 7.

  Sources   Download

GPL-2.0+

The Requires

 

08/03 2018

dev-update-twig

dev-update-twig

Shoehorns Twig template compiling into Drupal 7.

  Sources   Download

GPL-2.0+

The Requires

 

27/09 2017

dev-master

9999999-dev

Shoehorns Twig template compiling into Drupal 7.

  Sources   Download

MIT GPL-2.0+

The Requires

 

31/01 2017

dev-feature/robloach/progmi-1--site_name

dev-feature/robloach/progmi-1--site_name

Shoehorns Twig template compiling into Drupal 7.

  Sources   Download

MIT

The Requires

 

06/01 2017

dev-twig2

dev-twig2

Shoehorns Twig template compiling into Drupal 7.

  Sources   Download

MIT

The Requires

 

03/01 2017

1.0.0-alpha1

1.0.0.0-alpha1

Shoehorns Twig template compiling into Drupal 7.

  Sources   Download

MIT

The Requires

 

16/12 2016

dev-fix/robloach/filter-slug

dev-fix/robloach/filter-slug

Shoehorns Twig template compiling into Drupal 7.

  Sources   Download

MIT

The Requires

 

07/12 2016

dev-unittests

dev-unittests

Shoehorns Twig template compiling into Drupal 7.

  Sources   Download

MIT

The Requires