2017 © Pedro Peláez
 

library twig-stamp

Put a stamp placeholder in a base template, fill it from anywhere.

image

blablacar/twig-stamp

Put a stamp placeholder in a base template, fill it from anywhere.

  • Thursday, March 29, 2018
  • by odolbeau
  • Repository
  • 6 Watchers
  • 2 Stars
  • 16,585 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 10 % Grown

The README.md

Twig Stamp

Put a placeholder in a base template, fill it from anywhere., (*1)

Idea

One of our front mates needs to dump SVG sprites on our base layout, but don't want to dump all our icons: only the SVGs that are actually used in the whole page, on demand. The problem here is that our pages are complex, with lots of {% include %}, {% render %} and other features that doesn't let us easily track what are the required SVG icons and dump them at the top of the base layout., (*2)

This Twig extension adds the ability to put a placeholder somewhere in a base layout, and to put something inside from any page of the application whatever his scope and independence., (*3)

Example

To simplify, imagine that we want to create a page having a table of contents (toc) at the top, generated based on what's inside the page., (*4)

The main layout:, (*5)

{# demo.twig #}
{% stamp 'toc' %}

{# here is the placeholder where we want the table of contents dumped #}
{% stamp_dump 'toc' %}

{{ include('section1.twig') }}
{# ... #}

{% endstamp %}

One section:, (*6)

{# section1.twig #}
{# Here, we add a stamp that will be dumped in the placeholder later on #}


{{ stamp_use('toc', 'Section 1') }}

Lorem ipsum dolor sit amet, eu vel aliquam adversarium...

The table of contents:, (*7)

{# toc.twig #}


Table of contents

    {% for title in list %}
  • {{ title }}
  • {% endfor %}

Now we need to create the logic:, (*8)

<?php

namespace Demo\TableOfContents;

use Blablacar\Twig\Api\StampInterface;

class TableOfContentsStamp implements StampInterface
{
    protected $twig;
    protected $list = [];

    public function __construct(\Twig_Environment $twig)
    {
        $this->twig = $twig;
    }

    /**
     * Method called with {{ stamp_use('toc', 'Section 1') }}
     */
    public function useStamp()
    {
        list($title) = func_get_args();
        $this->list[] = $title;

        return $title;
    }

    /**
     * Method called when reaching {% endstamp %}, that will dump content in the {% stamp_dump %} placeholder
     */
    public function dumpStamp()
    {
        return $this->twig->render('toc.twig', [
            'list' => $this->list
        ]);
    }

    public function getName()
    {
        return 'toc';
    }
}

We need to register the Stamp in the extension:, (*9)

use Blablacar\Twig\Extension\StampExtension;
// ...

$extension = new StampExtension();
$twig->addExtension($extension);

$stamp = new TableOfContentsStamp($twig);
$extension->addStamp($stamp);

By running this sample, you'll get:, (*10)




Table of contents

  • Section 1

Section 1

Lorem ipsum dolor sit amet, eu vel aliquam adversarium...

Installation

composer require blablacar/twig-stamp

License

The MIT License (MIT), (*11)

Please read the LICENSE file for more details., (*12)

The Versions

29/03 2018

dev-master

9999999-dev

Put a stamp placeholder in a base template, fill it from anywhere.

  Sources   Download

MIT

The Requires

 

by Alain Tiemblo

29/03 2018

dev-twig-2.x

dev-twig-2.x

Put a stamp placeholder in a base template, fill it from anywhere.

  Sources   Download

MIT

The Requires

 

by Alain Tiemblo