setono/php-templates-bundle

一个集成了php模板库的Symfony扩展包

安装次数: 1,168

依赖者: 0

建议者: 0

安全性: 0

星星: 1

关注者: 2

分支: 0

公开问题: 1

类型:symfony-bundle

v1.0.0 2020-05-15 13:11 UTC

This package is auto-updated.

Last update: 2024-09-15 21:48:06 UTC


README

Latest Version Latest Unstable Version Software License Build Status Coverage Status Quality Score

此扩展包将PHP模板库集成到Symfony中。

安装

步骤 1: 下载

$ composer require setono/php-templates-bundle

步骤 2: 启用扩展包

如果你使用Symfony Flex,它将自动启用。否则,你需要将其添加到config/bundles.php

<?php
// config/bundles.php

return [
    // ...
    Setono\PhpTemplatesBundle\SetonoPhpTemplatesBundle::class => ['all' => true],
    // ...
];

使用

服务使用

该扩展包注册了服务setono_php_templates.engine.default,并将接口Setono\PhpTemplates\Engine\EngineInterface自动绑定到该默认引擎。这意味着你可以通过接口类型提示来注入引擎

<?php
use Setono\PhpTemplates\Engine\EngineInterface;

final class YourService
{
    /** @var EngineInterface */
    private $engine;

    public function __construct(EngineInterface $engine) {
        $this->engine = $engine;
    }

    public function __invoke(): string
    {
        return $this->engine->render('YourNamespace/template', [
            'parameter' => 'value'
        ]);
    }
}

模板

该扩展包自动添加了模板引擎的路径。对于扩展包,默认为src/Resources/views/php,对于应用程序,默认为templates/php

这意味着如果你放置了模板(按照正确结构),你可以无缝使用模板,如原始文档中所述。