gloomy/twig-decorator-bundle

Twig 扩展,用于条件布局和向模板注入变量

0.1 2013-12-11 13:19 UTC

This package is auto-updated.

Last update: 2024-09-25 06:54:04 UTC


README

关于

此包在 twig 中添加了 2 个标签

  • {% grab %},该标签可以将变量注入到模板中
  • {% decorate %},您可以使用它代替 {% extends %} 标签来决定要扩展的布局,并为每个布局注入自定义变量。

Build Status SensioLabsInsight Total Downloads

用法

{% grab %}

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

该包包含 1 个抓取器

  • ControllerGrabber

    此抓取器仅用于从控制器中向模板注入变量。

    {% 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'} %}

该包包含 2 个装饰器

  • 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

    此装饰器允许您根据是否使用了 XmlHttpRequest 定义 2 个布局。然后您可以选择将哪些变量注入到每个布局中。

    {% 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);
        }

    所有参数都是可选的。如果没有 _xmlhttprequest 参数,则默认打印所有块。

    {% 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 %}

    您可以使用 _blocks 参数选择要显示的块

    {% 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 %}

扩展

创建自己的抓取器或装饰器。

抓取器

您的类必须实现 Gloomy\TwigDecoratorBundle\Grabber\GrabberInterface,该接口只有一个方法

  • public function getVariables(array $variables);

装饰器

您的类必须实现 Gloomy\TwigDecoratorBundle\Decorator\DecoratorInterface,该接口有两个方法

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

然后定义一个带标签的服务,并将其用作 {% grab %} 或 {% decorate %} 标签的第一个参数。标签可以是

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

许可证

MIT

安装

1. 使用 composer 安装

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

2. 修改您的 app/AppKernel.php

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