hashandsalt / kirby-carver
Kirby 3 Carver 插件,用于使用 PHP 和 Kirby 的强大功能创建自定义 HTML 标签。
1.0.1
2019-03-24 12:24 UTC
Requires
This package is auto-updated.
Last update: 2024-09-25 07:42:58 UTC
README
它做什么
此插件允许您使用 PHP 编写自定义 HTML 标签。
为什么?
Kirby 非常出色,但我确实怀念 Textpattern 模板语言的简洁性,它看起来像 HTML。经过对可用的替代模板引擎(如 Twig 和 Blade)的尝试,没有太多喜爱,我决定将这个插件整合起来。
例如,要从一个字段获取格式化的日期,通常你会这样做
<time class="mytime">
<?php echo $page->date()->toDate('d/m/Y') ?>
</time>
如果你可以这样操作呢?
<kb:date format="d/m/Y" field="date" wraptag="time" class="mytime" />
你可以在 site/plugins/carver/library
中找到一些示例标签。
安装
- 下载文件并将其放置在
plugins/carver
中。 - 在
site
文件夹下创建一个名为carver
的文件夹。这是你标签存储的位置。
用法
让我们创建一个简单的标签作为示例,展示它可以做什么,基于上面提到的日期标签。
首先创建以下文件:'site/carver/date/tag.php'。
将以下代码添加到文件中
<?php
function kb_date($tag)
{
// Get the attributes
$att = json_decode(json_encode($tag['attributes']), true);
// Deal with the attributes
$format = isset($att['format']) ? $att['format'] : 'd/m/Y';
$wraptag = isset($att['wraptag']) ? $att['wraptag'] : 'p';
$class = isset($att['class']) ? $att['class'] : '';
// Get the field date or use todays date if its not set or cant be found
$dateval = isset($att['field']) ? page()->{$att['field']}()->toDate($format) : date($format);
return Html::tag($wraptag, $dateval, ["class" => $class]);
}
让我们解释一下它是如何工作的。我们的标签看起来像这样
<kb:date format="d/m/Y" field="date" wraptag="time" class="mytime" />
它有三个自定义属性,format
、field
和 class
。
如果你现在在模板中使用它,你将得到以下渲染结果,假设你在你的蓝图中有设置日期字段并存储了值
<time class="mytime">02/02/2019</time>
相当直观,你可以设置要使用的字段、输出的类以及要使用的日期格式。但这还不是全部 - 你可以跳过一些属性,因为已经设置了回退。
在模板中这样做将使用今天的日期,将其格式化为 'd/m/Y',并且不添加类
<kb:date />
将得到以下结果
<p>02/02/2019</p>
更多标签示例
<!-- Render a gist -->
<kb:gist url="https://gist.github.com/bastianallgeier/b79615a9f7ca76c810b7" />
<!-- Render a youtube video -->
<kb:youtube url="https://www.youtube.com/watch?v=VcjzHMhBtf0" width="100%" height="450px" autoplay="1" loop="1" />
<!-- Render a vimeo video -->
<kb:vimeo url="https://vimeo.com/324963776" width="100%" height="450px" autoplay="1" loop="1" autopause="0" />
<!-- Render an iframe -->
<kb:iframe url="https://getkirby.com" width="100%" height="400px" wraptag="div" class="iframecontent" wrapclass="iframecontainer" />
<!-- Render a link -->
<kb:a href="https://hashandsalt.com" class="mylink" rel="nofollow" />
<!-- Title -->
<kb:title field="title" wraptag="h1" />
<!-- Kirbytext from a field -->
<kb:kt field="text" />
<!-- Obfusifcated Email Link -->
<kb:email field="email" text="Email us today" class="mail" />
<!-- Images from a field, wrapped in a container tag and items wrapped in a tag each -->
<kb:images class="images" file="gallery" wraptag="ul" breaktag="li" />
<!-- Single image by name -->
<kb:image class="myimage" file="lw31n7hd1i.jpg" />
<!-- Handle a missing image gracefully -->
<kb:image class="myimage" file="lw31n7hsdfsdfd1i.jpg" />
路线图
- 将标签解析器更新到最新
- 重构标签解析器,以允许通过配置设置选项
- 创建一个包含与 Kirby 内置函数一起工作的内置标签的大型库。
Composer 支持
贡献
当我不用 Kirby 时,我使用 Textpattern。遗憾的是,创建了 Textpattern 的 Dean Allen 大约一年前去世了。他在 CMS 中的简洁性哲学与 Kirby 相一致,这个插件是为了纪念他。