Giter VIP home page Giter VIP logo

twig-injection-bundle's Introduction

TwigInjectionBundle

The TwigInjectionBundle allows to inject twig templates through the event behavior.

Installation

TwigInjectionBundle requires Symfony 3.4 or higher.

Require the bundle in your composer.json file:

{
    "require": {
        "intaro/twig-injection-bundle": "^2.0"
    }
}

Register the bundle in AppKernel:

// app/AppKernel.php

public function registerBundles()
{
    $bundles = [
        //...

        new Intaro\TwigInjectionBundle\IntaroTwigInjectionBundle(),
    ];

    //...
}

Install the bundle:

$ composer update intaro/twig-injection-bundle

Usage

  1. Add {{ inject() }} calling in template:
{{ inject('twig.injection.event.name', { parameter1: 'some-value', parameter2: some_object }) }}
  1. Prepare controller action which you want to render or template which you want to include.

  2. Define Listener which will inject include or render calling:

<?php

namespace Acme\DemoBundle\EventListener;

use Intaro\TwigInjectionBundle\Event\TwigInjectEvent;
use Intaro\TwigInjectionBundle\Event\TwigInjectRender;

class TwigInjectionListener
{
    public function onSomeEvent(TwigInjectEvent $event)
    {
        $parameters = $event->getParameters();

        if (!isset($parameters['parameter1']) || 'some-value' !== $parameters['parameters1']) {
            return;
        }

        $render = new TwigInjectRender(
            'AcmeDemoBundle:DefaultController:index',
            [ 'object' => $parameters['parameters2'] ]
        );
        $event->addInjection($render);

        $include = new TwigInjectInclude('AcmeDemoBundle:Default:someTemplate.html.twig');
        $event->addInjection($include);
    }
}
  1. Register the listener:
services:
    acme_demo.twig_injection.listener:
        class: Acme\DemoBundle\EventListener\TwigInjectionListener
        tags:
            - { name: kernel.event_listener, event: twig.injection.event.name, method: onSomeEvent }

twig-injection-bundle's People

Contributors

1on avatar addfs avatar muxx avatar tourze avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

twig-injection-bundle's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.