smcgarrity / rhubarb-module-leaf-twigview
Rhubarb 的 Twig 集成视图
1.0.0
2018-03-12 09:35 UTC
Requires
- rhubarbphp/module-leaf: ^1.0.0
- rhubarbphp/rhubarb: ^1.5.9
- twig/twig: ^2.0
Requires (Dev)
- rhubarbphp/custard: ^1.0.8
This package is not auto-updated.
Last update: 2024-09-27 16:31:09 UTC
README
在 Rhubarb 视图中集成 Twig HTML 模板引擎。
创建 Twig 视图
要创建一个 twig 视图,只需创建一个继承自 TwigView 类的视图。
class ProductDetailsView extends TwigView
您需要实现两个方法
getTwigFileLocation() 应该返回您的 .twig 或 .html 文件的字符串位置。
function getTwigFileLocation(): string { return __DIR__ . '/File.twig'; }
getTwigVariables() 返回一个包含您想要在 Twig 模板中可用的值的数组。
function getTwigVariables() { return [ 'PageTitle' => 'Edit Page', 'Type' => $this->model->restModel->Type, 'NameInput' => $this->leaves['NameTextbox'], ]; }
您还需要一个实际的 Twig 文件进行编译。Twig 可以编译任何 HTML 文件,但还支持额外的 twig 功能,如控制和变量。使用 PHPStorm 的 .twig 文件格式化 twig 输入。
TwigView 使用 printViewContent 方法来渲染 HTML,因此如果您扩展了 printViewContent,请确保包含父调用。
使用 Twig
Twig 使用括号 { }
将变量和控制插入到 HTML 模板中。
可以使用双括号直接输出变量。
<p>{{ date }}</p>
可以使用点符号以相同的方式访问对象
<p>Dear {{ recipient.Name }},</p>
可以使用 {% %}
添加控制。
{% for purchase in purchases %} {% if purchase.cancelled %} <p class="red">You cancelled your purchase of a {{purchase.product}} on {{purchase.date}} {% else %} <p> you purchased a {{purchase.product}} on {{purchase.date}} for {{purchase.price}}</p> {% endif %} {% endfor %}
完整的 Twig 文档:https://twig.symfony.com.cn/doc/2.x/