承接 gloomy/twig-decorator-bundle 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

gloomy/twig-decorator-bundle

Composer 安装命令:

composer require gloomy/twig-decorator-bundle

包简介

Twig extension to make conditional layout and to inject variables in templates

README 文档

README

ABOUT

This bundle adds 2 tags in twig:

  • {% grab %} which injects variables into the template
  • {% decorate %} that you can use instead of the {% extends %} tag to determine which layout to use to extend your template, and inject custom variables to each layout.

Build Status SensioLabsInsight Total Downloads

USAGE

{% grab %}

{% grab 'my_grabber' %}
{# or #}
{% grab 'my_grabber' with {'var1': 'my_value'} %}

The bundle comes with 1 grabber:

  • ControllerGrabber

    This grabber will be used only to inject variables inside the template from a controller.

    {% grab 'controller' with {
        '_controller': 'MyBundle:MyController:MyMethod',
        'my_own_var': 'cool !'}
    %}
    <?php
    //...
    class MyController
    {
        public function MyMethodAction($variables)
        {
            $layout = array('my_var' => 'I use my layout in many templates, but I inject variables only here');
            return array_merge($variables, $layout);
        }

{% decorate %}

{% decorate 'my_decorator' %}
{# or #}
{% decorate 'my_decorator' with {'var1': 'my_value'} %}

The bundle comes with 2 decorators:

  • controller

    This decorator will be used only to inject variables inside the layout from a controller.

    {% decorate 'controller' with {
        '_controller': 'MyBundle:MyController:MyMethod',
        '_template': 'MyBundle::layout.html.twig',
        'my_own_var': 'cool !'}
    %}
    <?php
    //...
    class MyController
    {
        public function MyMethodAction($variables)
        {
            $layout = array('my_var' => 'I use my layout in many templates, but I inject variables only here');
            return array_merge($variables, $layout);
        }
  • xmlhttprequest

    This decorator lets you define 2 layouts depending if XmlHttpRequest has been used or not. Then you can choose which variables to inject into each.

    {% decorate 'xmlhttprequest' with {
        '_controller': 'MyBundle:MyController:MyMethod',
        '_template': 'MyBundle::layout.html.twig',
        '_xmlhttprequest': 'MyBundle::xmlhttprequest.html.twig',
        'my_own_var': 'cool !'} %}
    <?php
    //...
    class MyController
    {
        public function MyMethodAction($variables)
        {
            $layout = array();
            if (false === $this->getRequest()->isXmlHttpRequest()) {
                $layout = array('needed_only_for_layout' => 'This variable is NOT injected in XmlHttpRequest mode');
            }
            return array_merge($variables, $layout);
        }

    All parameters are optionals. Without the _xmlhttprequest parameter, all blocks are printed by default.

    {% decorate 'xmlhttprequest' with {
        '_template': 'MyBundle::layout.html.twig',
        'my_own_var': 'cool !'} %}
    
    {% block test %}
        This block is printed directly if request is xmlHttpRequest, but extends MyBundle::layout.html.twig otherwise
    {% endblock %}

    You can choose the blocks you want to display with the _blocks parameter

    {% decorate 'xmlhttprequest' with {
        '_template': 'MyBundle::layout.html.twig',
        '_blocks': ['test']
        'my_own_var': 'cool !'} %}
    
    {% block test %}
        This block is printed if request is xmlHttpRequest, but extends MyBundle::layout.html.twig otherwise
    {% endblock %}
    
    {% block test_not_printed %}
        This block is NOT printed if request is xmlHttpRequest, but extends MyBundle::layout.html.twig otherwise
    {% endblock %}

EXTEND

Create your own grabber or decorator.

Grabber

Your class must implement Gloomy\TwigDecoratorBundle\Grabber\GrabberInterface which has only 1 method:

  • public function getVariables(array $variables);

Decorator

Your class must implement Gloomy\TwigDecoratorBundle\Decorator\DecoratorInterface which has only 2 methods:

  • public function getTemplate(array $variables);
  • public function getVariables(array $variables);

Then define a tagged service and use it as the first argument of the {% grab %} or {% decorate %} tag. The tag can be :

    <tag name="gloomy.grabber" alias="my_alias"/>
    <tag name="gloomy.decorator" alias="my_alias"/>

LICENSE

MIT

INSTALLATION

1. Install with composer

composer.phar require "gloomy/twig-decorator-bundle" "*"

2. Modify your app/AppKernel.php

<?php
    //...
    $bundles = array(
        //...
        new Gloomy\TwigDecoratorBundle\GloomyTwigDecoratorBundle(),
    );

统计信息

  • 总下载量: 46
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-06-26

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固