jays-de/handlebars-bundle

此 Symfony 扩展包为 handlebars 模板引擎提供集成。

安装量: 90,055

依赖者: 1

建议者: 0

安全: 0

星标: 3

关注者: 3

分支: 3

开放问题: 0

类型:symfony-bundle

1.2.0 2018-06-13 16:27 UTC

README

Symfony 扩展包通过使用 Handlebars 模板引擎和 LightnCandy 作为渲染器进行集成。

Build Status Scrutinizer Scrutinizer Packagist SensioLabsInsight

安装

先决条件

  • Symfony 2.8+
  • composer

安装

composer require jays-de/handlebars-bundle

Composer will install the bundle to your project's `vendor/` directory.

### 2. Enable the bundle

Enable the bundle in the kernel:

``` php
<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new \JaySDe\HandlebarsBundle\HandlebarsBundle();
    );
}

3. 在配置中启用 Handlebars 模板引擎

    # app/config/config.yml
    framework:
        templating:      { engines: ['twig', 'handlebars'] }

文档

用法

以 .hbs 或 .handlebars 结尾的 Resources/view 中的文件受支持。

public function indexAction(Request $request)
    {
        ...
        return $this->render('index.hbs', [...]);
    }

这将渲染 Resources/views 文件夹中的 index.hbs 文件。

配置标志

可以设置或取消 LightnCandy 提供的不同标志。因此,在您的 config.yml 中设置 fields 和 excludedFlags 字段。该包将确保设置默认标志,以防止模板引擎无法工作。完整的标志列表可以在 LnC 文档 中找到。

 # app/config/config.yml
handlebars:
  flags:
    - FLAG_BESTPERFORMANCE
  excludedFlags:
    - FLAG_STANDALONE

辅助函数

内置辅助函数

该包附带一些内置辅助函数。

链接到页面

使用路径辅助函数并引用路由

<a href="{{route_path '_welcome' }}">Home</a>

也可以向引用的路由添加参数

<a href="{{route_path 'article_show' slug=article.slug}}">{{ article.title }}</a>

您还可以使用 url 辅助函数生成绝对 URL

<a href="{{route_url '_welcome' }}">Home</a>
链接到资源

为了避免对图像、javascript 或样式表等资源的路径进行硬编码,请使用 asset 辅助函数。

<img src="{{asset 'images/logo.png' }}" />

<link href="{{asset 'css/blog.css' }}" rel="stylesheet" />

添加新辅助函数

要将新的辅助函数添加到 handlebars 引擎,您只需创建一个实现 JaySDe\HandlebarsBundle\Helper\HelperInterface 的类,并创建一个带有 handlebars.helper 标签的服务定义。标签的 ID 是 handlebars 模板中辅助函数块名称。

示例

<service id="handlebars.helper.trans" class="JaySDe\HandlebarsBundle\Helper\TranslationHelper">
	<tag name="handlebars.helper" id="i18n" />
	<argument type="service" id="translator" />
</service>

辅助函数注册也支持注册任何可调用对象。因此,可以创建一个具有 magic __invoke() 方法的类,并为它定义一个服务

class MyHelper{
    public function __invoke($context, $options) {}
}
<service id="handlebars.helper.my" class="MyHelper">
	<tag name="handlebars.helper" id="my" />
</service>

或使用工厂方法返回匿名函数,例如

class HelperFactory{
    public function getMyHelper() {
        return function($context, $options) {}
    }
}
<services>
    <service id="handlebar.helper_factory" class="HelperFactory" />
    <service id="handlebars.helper.my" class="callable">
        <factory service="handlebar.helper_factory" method="getMyHelper" />
        <tag name="handlebars.helper" id="my" />
    </service>
</services>

作者

Jens Schulze - jens.schulze@commercetools.de

还可以查看参与此项目的 贡献者列表

提交错误和功能请求

错误和功能请求在 GitHub 上跟踪。