2017 © Pedro Peláez
 

library twig_override

Allows twig templates and arguments to be overridden at run-time.

image

christophersmith262/twig_override

Allows twig templates and arguments to be overridden at run-time.

  • Thursday, February 8, 2018
  • by christophersmith262
  • Repository
  • 0 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Build Status Scrutinizer Code Quality Code Coverage, (*1)

TwigOverride

TwigOverride provides a twig extension that can help with:, (*2)

  • Preprocessing the arguments passed to a twig template anywhere that template is referenced.
  • Dynamically rewriting include / embed / extends to reference a different template based on run-time context.

Installation

TwigOverride supports twig 1.x and twig 2.x., (*3)

composer require christophersmith262/twig_override

Performing Simple Template Overrides

A basic override provider TwigOverride\Providers\SimpleRewriteProvider is included to perform template file swapping. For example, if I wanted to replace every occurrence of:, (*4)

{% include "template1.twig" %}

with, (*5)

{% include "template2.twig %}

By Manually Adding the Extension

If you are manully creating the twig environment, you can call the addExtension method to directly add the plugin:, (*6)


use TwigOverride\TwigOverrideExtension; use TwigOverride\Providers\SimpleRewriteProvider; $twig = new \Twig_Environment(new \Twig_Loader_Array([ 'template1.twig' => 'test1', 'template2.twig' => 'test2', 'template3.twig' => '{% include "template1.twig" %} ])); $twig->addExtension(new TwigOverrideExtension([ new SimpleRewriteProvider([ 'template1.twig' => 'template2.twig', ]), ]); // Outputs 'test2'. $twig->render('template3.twig');

Using Twig as a Symfony Service

If you are using the standard symfony services.yaml, then you can simply add the extension as a service:, (*7)

services:
    twig_override.extension:
        class: TwigOverride\TwigOverrideExtension
        arguments: [['@twig_override.provider.simple']]
        tags: ['twig.extension']

    twig_override.providers.simple:
        class: TwigOverride\Providers\SimpleRewriteProvider
        arguments: ['%twig_override.simple_mappings%']

parameters:
    twig_override.simple_mappings:
        'template1.twig': 'template2.twig'

Advanced Rewrites with a Custom Provider

In the example below we'll create a provider that dynamically resolves a custom 'profile' template for a user based on the user id, and decorates the arguments passed to any template that includes a 'user_id' with a 'user_name' argument., (*8)

To do this we'll first creat a provider class that will need access to the twig loader:, (*9)

use TwigOverride\Providers\ProviderInterface;

class AdvancedRewriteProvider implements ProviderInterface {

  private $users = [1 => 'Admin', 2 => 'Guest'];
  private $loader;

  public function __construct(\Twig_LoaderInterface $loader) {
    $this->loader = $loader;
  }

  /**
   * Overrides 'profile.twig' with 'profile--<uid>.twig' if the template exists.
   */
  public function rewriteTemplateName($template_name, array $with, array $_context, $only) {
    if ($template_name == 'profile.twig') {
      $uid = $this->getUid($with, $_context);
      $override = 'profile--' . $uid . '.twig';

      if ($this->loader->exists($override)) {
        return $override;
      }
    }

    return $template_name;
  }

  /**
   * Sets a 'user_name' parameter when 'user_id' is passed to a twig template.
   */
  public function preprocessTemplateArgs($template_name, array $with, array $_context, $only) {
    $uid = $this->getUid($with, $_context);

    if (!empty($this->users[$uid])) {
      $with['user_name'] = $this->users[$uid];
    }

    return $with;
  }

  protected getUid(array $with, array $_context) {
    if (!empty($with['user_id'])) {
      return $with['user_id'];
    elseif (!empty($_context['user_id'] && !$only) {
      return $_context['user_id'];
    }
    else {
      return NULL;
    }
  }

}

Assuming we are using a twig in a symfony container environment, we can expose the provider in the services.yaml. Note that in this example the provider needs the twig loader service as an argument., (*10)

services:
    twig_override.extension:
        class: TwigOverride\TwigOverrideExtension
        arguments: [['@twig_override.provider.advanced']]
        tags: ['twig.extension']

    twig_override.providers.advanced:
        class: TwigOverride\Providers\AdvancedRewriteProvider
        arguments: ['@twig.loader']

The Versions

08/02 2018

1.x-dev

1.9999999.9999999.9999999-dev

Allows twig templates and arguments to be overridden at run-time.

  Sources   Download

MIT

The Requires

 

The Development Requires

08/02 2018

1.0.0

1.0.0.0

Allows twig templates and arguments to be overridden at run-time.

  Sources   Download

MIT

The Requires

 

The Development Requires

08/02 2018

2.x-dev

2.9999999.9999999.9999999-dev

Allows twig templates and arguments to be overridden at run-time.

  Sources   Download

MIT

The Requires

 

The Development Requires

08/02 2018

2.0.0

2.0.0.0

Allows twig templates and arguments to be overridden at run-time.

  Sources   Download

MIT

The Requires

 

The Development Requires