jaem3l/template-bundle

为在控制器中使用twig模板提供了一种简单的方式。

安装: 7

依赖: 0

建议者: 0

安全: 0

星标: 4

关注者: 4

分支: 0

开放问题: 0

类型:symfony-bundle

v1.1.0 2018-02-04 15:46 UTC

This package is auto-updated.

Last update: 2024-09-19 01:53:28 UTC


README

SensioLabsInsight Build Status Maintainability Test Coverage

此包提供了一个单一的@Template注解,可以用来替代SensioFrameworkExtraBundle中同名注解。

工作原理

您可以在tests/Annotation/TemplateTest.php中的DummyController中看到作为参考。完全支持Twig,这将使编写模板更容易。

要求

  • PHP 7.1
  • SensioFrameworkExtraBundle 5.1
  • Twig 2.4

安装

该包可以通过Composer安装

composer require jaem3l/template-bundle

当在用Flex引导的Symfony 4应用程序中使用此包时,请确保已安装twig-bundle并设置其配方,例如使用composer req twig

路由注解的使用示例

<?php

namespace App\Controller;

use jæm3l\TemplateBundle\Annotation\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class HelloController
{
    /**
     * @Route("/hello1")
     * @Template("Hello World")
     */
    public function simpleHelloAction()
    {
    }

    /**
     * @Route("/hello2 ")
     * @Template("Hello {{ name }}")
     */
    public function advancedHelloAction()
    {
        return [
            'name' => 'Santa Claus',
        ];
    }

    /**
     * @Route("/twig")
     * @Template("{{ 1 + 2 }}")
     */
    public function simpleExpressionAction()
    {
    }

    /**
     * @Route("/advanced_hello ")
     * @Template("
     * {% extends 'base.html.twig' %}
     *
     * {% block body %}
     * Hello {{ name }}!
     * {% endblock %}
     * ")
     */
    public function advancedTemplateAction()
    {
        return [
            'name' => 'Santa Claus',
        ];
    }
}

模板控制器使用示例

在您的路由中,现在可以依赖此控制器直接渲染提供的模板。

# SF3: app/config/routing.yml
# SF4: config/routes.yaml

hello_world:
    path: /hello-world
    controller: jæm3l\TemplateBundle\Controller\TemplateController::template
    defaults:
        template: 'Hello World!'
        
twig_expression:
    path: /twig-expression
    controller: jæm3l\TemplateBundle\Controller\TemplateController::template
    defaults:
        template: '{{ 1 + 2 }}'
        
advanced_template:
    path: /advanced-template
    controller: jæm3l\TemplateBundle\Controller\TemplateController
    defaults:
        template: |
            {% extends 'base.html.twig' %}

            {% block body %}
            Hello World!
            {% endblock %}