smartive/handlebars-bundle

此包已被废弃,不再维护。未建议替代包。

此包将 Handlebars 模板渲染集成到 Symfony2

1.3.1 2017-01-09 13:26 UTC

This package is not auto-updated.

Last update: 2020-08-21 19:04:48 UTC


README

Build Status Coverage Status Latest Stable Version Total Downloads License

SmartiveHandlebarsBundle

将 Handlebars 模板集成到您的 Symfony2 / Symfony3 应用程序的包。

此包通过使用 xamin/handlebars.php 来渲染 Handlebars。

安装

在您的 composer.json 中要求 smartive/handlebars-bundle 包并更新您的依赖项。

{
    …
    "require": {
        "smartive/handlebars-bundle": "dev-master"
    }
    …
}

app/AppKernel.php 中注册该包

// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new Smartive\HandlebarsBundle\SmartiveHandlebarsBundle(),
    );
}

配置

一些功能可以在 app/config/config.yml 中的 smartive_handlebars 部分中进行配置。

Handlebars 文件扩展名

Handlebars 文件的默认文件扩展名设置为 .hbs。可以使用以下设置覆盖(示例文件扩展名设置为 .handlebars

smartive_handlebars:
    templating:
        file_extension: .handlebars

模板目录

通过 template_directories 设置,您可以定义搜索 Handlebars 模板的目录。您可以使用 Symfony 资源表示法以及绝对文件路径来配置目录。

smartive_handlebars:
    templating:
        template_directories:
            - '@AcmeDemo/Resources/views/Templates'
            - /var/www/templates

默认情况下,模板将在模板目录中递归搜索。您可以通过以下方式禁用此行为

smartive_handlebars:
    templating:
        template_directories_recursive: false

Twig 扩展

默认启用 Handlebars Twig 扩展。要禁用它,请将以下内容添加到您的配置中

smartive_handlebars:
    twig:
        enabled: false

使用方法

渲染服务

smartive_handlebars.templating.renderer 服务提供了一个 render($templateName, $data) 方法,可用于渲染 Handlebars 模板。

Twig

要在 Twig 中渲染 Handlebars 模板,您可以使用 Twig 函数 handlebars(templateName, data)

自定义 Handlebars 辅助函数

您可以通过实现 Handlebars\Helper 接口将您自己的 Handlebars 辅助函数作为标记服务添加。有关如何编写自定义辅助函数的更多信息,请参阅 xamin/handlebars.php 的内置辅助函数

实现您自己的辅助函数后,您必须使用 smartive_handlebars.helper 标签和一个适当的别名将其注册为服务

# app/config/services.yml
services:
    demo_bundle.my_demo_helper:
        class: DemoBundle\Helpers\MyDemoHelper
        tags:
            - { name: smartive_handlebars.helper, alias: myDemo }

现在,您可以在模板中如下使用您自己的自定义 Handlebars 辅助函数

{{#myDemo parameter}}
    {{!-- do stuff --}}
{{/myDemo}}

缓存

渲染服务提供了在请求之间缓存解析模板的能力,以便更快地进行渲染。

您可以通过将 smartive_handlebars.cache 设置为您的 app/config/config.yml 中存在的缓存服务 ID 来启用缓存。

smartive_handlebars:
    cache:
        enabled: true
        service: <service-id>

默认情况下,有几种缓存服务和策略可供选择。

磁盘

服务 ID: smartive_handlebars.cache.disk

使用 Handlebars\Cache\Disk 在 Symfony 的缓存目录中读取/写入文件缓存。

APC

服务 ID: smartive_handlebars.cache.apc

使用 Handlebars\Cache\APC 通过 APC 读取/写入缓存对象。

Redis

服务 ID: smartive_handlebars.cache.redis

使用 PhpRedisPredis 通过一个 Redis 服务器 读取/写入缓存对象。此包通过 RedisBundle 集成,以便更容易地配置您的 Redis 实现。默认使用的 Redis 客户端是 snc_redis.default(请参阅 RedisBundle 文档)。

默认配置可以按如下方式覆盖

smartive_handlebars:
    cache:
        redis:
            client_service: snc_redis.default
            key_prefix: 'smartive-handlebars:'

自定义

您也可以通过添加一个实现 Handlebars\Cache 的类来自定义缓存服务。要使用您自定义的缓存服务,您必须在服务配置中注册它。

# app/config/services.yml
services:
    demo_bundle.my_demo_cache_service:
        class: DemoBundle\Cache\CustomCache
# app/config/config.yml
smartive_handlebars:
    cache: demo_bundle.my_demo_cache_service

完整的配置示例

# app/config/config.yml
smartive_handlebars:
    templating:
        enabled: true
        file_extension: .hbs
        template_directories:
            - '@AcmeDemo/Resources/views/Templates'
            - /var/www/templates
        template_directories_recursive: true
    twig:
        enabled: true
    cache:
        enabled: false
        service: smartive_handlebars.cache.redis
        redis:
            client_service: snc_redis.default
            key_prefix: 'smartive-handlebars:'