wearejust / kirby-twig
为 Kirby CMS 提供 Twig 模板支持
5.0.2
2024-04-27 12:57 UTC
Requires
- getkirby/composer-installer: ^1.1
- twig/twig: ^3.4
This package is auto-updated.
Last update: 2024-08-27 13:52:33 UTC
README
它看起来像什么
之前
<?php /* site/templates/hello.php */ ?> <h1><?= $page->title() ?></h1> <ul> <?php foreach ($page->children() as $child): ?> <li><a href="<?= $child->url() ?>"><?= $child->title() ?></li> <?php endforeach; ?> </ul>
之后
{# site/templates/hello.twig #} <h1>{{ page.title }}</h1> <ul> {% for child in page.children() %} <li><a href="{{ child.url }}">{{ child.title }}</li> {% endfor %} </ul>
安装
下载
下载并复制此存储库到 /site/plugins/kirby-twig
。
Git 子模块
git submodule add https://github.com/wearejust/kirby-twig.git site/plugins/kirby-twig
Composer
composer require wearejust/kirby-twig
使用方法
页面模板
现在插件已安装并激活,您可以在 site/templates
目录中编写 Twig 模板。例如,如果您的文章文本文件的名称为 post.txt
,您可以有如下 post.twig
模板:
{% extends 'layout.twig' %} {% block content %} <article> <h1>{{ page.title }}</h1> {{ page.text.kirbytext | raw }} </article> {% endblock %}
看 {% extends '@templates/layout.twig' %}
和 {% block content %}
部分?它们是管理许多模板具有共同页面布局的强大方式,只需更改核心内容(以及/或其他特定部分)。阅读 我们的 Twig 模板指南 了解更多信息。
提示:访问页面方法而不是公共变量
Twig 对特定方法的调用,如 page.children
,有时返回 NULL
。如果也存在一个公共变量,该变量仅在调用相应方法后初始化,则可能会发生这种情况。
{{ page.children }}
返回 NULL
,因为公共变量被返回。请像这样调用方法:{{ page.children() }}
。
选项
您可以在 选项文档 中找到完整选项列表。
更多文档
许可协议
致谢
- 维护者: JUST
- (前) 维护者: Christian Zehetner
- Twig 库: Fabien Potencier 和贡献者 / 许可协议
- Kirby 2 的 Twig 插件: Florens Verschelde