devtronic/twig-compose

从多个子模板组合Twig模板

v1.2.2 2018-06-16 10:11 UTC

This package is auto-updated.

Last update: 2024-09-21 20:23:09 UTC


README

A twig extension to compose templates from multiple child templates.

❓ 这是什么?

例如,你有一个基本模板和N个子模板可以修改基本模板。如果模板是固定的,你可以相互扩展,没有问题。如果模板数量是动态的,你不能使用扩展。在这种情况下,你可以使用这个包。只需调用组合方法,传递一个基本模板和子模板数组...然后,你会得到一个包含每个子模板内容的模板 🌠

📦 安装

$ composer require devtronic/twig-compose

❤ 赞助商

目前没有赞助商。 成为赞助商

🛠 使用

详细信息请参阅测试目录

➡ 使用你的现有Twig_Environment

<?php

use Devtronic\TwigCompose\ComposeEnvironment;

/** @var $twig Twig_Environment */
$twig->addExtension(new Devtronic\TwigCompose\ComposeExtension());
$template = ComposeEnvironment::composeStatic($twig, 'base.html.twig', ['pluginA.html.twig', 'pluginB.html.twig']);

echo $template->render([]);

➡ 使用ComposeEnvironment

<?php

use Devtronic\TwigCompose\ComposeEnvironment;

$loader = new \Twig_Loader_Filesystem(''); // or whatever
$twig = new ComposeEnvironment($loader); // instead of Twig_Environment

$template = $twig->compose('base.html.twig', ['pluginA.html.twig', 'pluginB.html.twig']);

echo $template->render([]);

➡ 自动组合模板(实验性,仅适用于ComposeEnvironment)

<?php

use Devtronic\TwigCompose\ComposeEnvironment;

$loader = new \Twig_Loader_Filesystem('/res/main-theme');
$twig = new ComposeEnvironment($loader);
$loader->addPath('/res/theme1', 'Theme1');
$loader->addPath('/res/theme2', 'Theme2');

// If autocompose is enabled, all registered paths will be checked.
// If a file with the the same name (e.g. base.html.twig) exists,
// it will be loaded & composed with the `base.html.twig` inside the main-theme folder
$twig->setAutoCompose(true); 
$template = $twig->load('base.html.twig');

echo $template->render([]);