codemade-xyz/php-liquid-bundle

PHP 的 Symfony LiquidBundle 模板引擎

1.0.4 2020-10-03 18:14 UTC

This package is auto-updated.

Last update: 2024-09-29 05:48:42 UTC


README

Liquid 是 Ruby 的 Liquid 模板引擎(由 Tobias Lutke 编写)的 PHP 版本,虽然 PHP 中有许多其他模板引擎,包括 Liquid 部分受启发的 Smarty

为什么选择 Liquid?

为什么还需要另一个模板库?

Liquid 编写以满足三个模板库要求:良好的性能、易于扩展和简单易用。

安装

您可以通过 composer 安装此库

composer require codemade-xyz/php-liquid-bundle

示例模板

{% if products %}
	<ul id="products">
	{% for product in products %}
	  <li>
		<h2>{{ product.name }}</h2>
		Only {{ product.price | price }}

		{{ product.description | prettyprint | paragraph }}

		{{ 'it rocks!' | paragraph }}

	  </li>
	{% endfor %}
	</ul>
{% endif %}

如何在 Symfony 中使用 Liquid

1. 在文件 bundle.php 或 kernel.php 中连接包

示例添加到 kernel.php

public function registerBundles()
{
    $bundles = array(
        ...
        new \CodeMade\LiquidBundle\LiquidBundle()
    );
    
    return $bundles;
}

2. 这里是一个简单的示例配置

在配置文件中添加设置,如果没有指定值,则将使用默认设置。

liquid:
  cache: '%kernel.cache_dir%/liquid'
  default_path: '%kernel.project_dir%/templates'
  filter: App\Kernel\LiquidTemplateFilter
  include_suffix: 'tpl'
  include_prefix: ''
  tags:
    section: App\Kernel\LiquidTagSection
  paths:
    'App': '%kernel.project_dir%/templates/App'

3. 在配置文件 framework.yaml 中

在 framework.yaml 中添加设置

framework:
  ...
  templating:
    engines: ['liquid']

4. 在控制器中使用标准函数处理模板。

<?php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

class Index extends AbstractController
{

    /**
     * @Route("/")
     */
    public function index()
    {
        return $this->render('@App/home', [
            'document' => [
                'title' => 'Home page'
            ]
        ]);

    }

}

要求

  • PHP 7.1+

分支说明和贡献者

此包为 Symfony 创建并使用
harrydeluxe 库 Liquid!