定制 arnoson/kirby-template-sugar 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

arnoson/kirby-template-sugar

最新稳定版本:1.0.0

Composer 安装命令:

composer require arnoson/kirby-template-sugar

包简介

README 文档

README

Kirby Template Sugar

A lightweight template compiler that adds some syntactic sugar to Kirby's php templates.

Kirby's new snippets with slots allow you to adapt a component-based workflow, similar to Laravel blade templates or javascript frameworks like Vue. However, the plain php syntax can be verbose. So with some template sugar you can write this:

<k:card $rounded="<? true ?>" class="bg-yellow" id="my-card">
  <k:slot name="icon">🍬</k:slot>
  <k:slot name="title">
    <h2>Kirby Template Sugar</h2>
  </k:slot>
  <k:slot>
    <k:link $url="github.com/arnoson/kirby-template-sugar">
      <i>Read more ...</i>
    </k:link>
  </k:slot>
</k:card>

instead of this:

<?php snippet('card', [
  'rounded' => true,
  'attr' => ['class' => 'bg-yellow', 'id' => 'my-card']
], slots: true); ?>
  <?php slot('icon'); ?>🍬<?php endslot(); ?>
  <?php slot('title'); ?>
    <h2>Kirby Template Sugar</h2>
  <?php endslot(); ?>
  <?php slot(); ?>
    <?php snippet('link', ['url' => 'github.com/arnoson/kirby-template-sugar'], slots: true); ?>
      <i>Read more ...</i>
    <?php endsnippet(); ?>
  <?php endslot(); ?>
<?php endsnippet(); ?>

How Does It Work

Your template files need to be compiled into regular php, similar to how other template languages work. But the goal of this project is not to create a new full-fledged template language for Kirby. Instead it embraces the existing php templates and just adds a little sugar where they tend to get too messy. You still write php/html (with syntax highlighting, intellisense, ...) and add a special <k:my-snippet> or <k:slot> tag here and there to keep things tidy.

Getting Started

The easiest way to get started is to check out one of the examples:

  • Use the CLI example if you want a minimal starter kit that doesn't rely on any other build tools.
  • Use the Vite example to compile your templates alongside your other frontend assets.

To start manually (or convert an existing project), have a look at the CLI or the Vite plugin. And make sure you also install the Kirby plugin.

Syntax

Snippets

Snippets can have slots or be self-closing:

With Sugar Compiled
<k:my-snippet>
  <k:slot name="title">
    Hello
  </k:slot>
</k:my-snippet>

<k:my-snippet />
<?php snippet('my-snippet', slots: true); ?>
  <?php slot('title'); ?>
    Hello
  <?php endslot(/* title */); ?>
<?php endsnippet(/* my-snippet */); ?>

<?php snippet('my-snippet'); ?>

Props and attributes

Snippets can have props, which are passed directly to the snippet, and attributes, which are grouped into an $attr variable passed to the snippet along with the props. Props start with $ (like $open and $items) and attributes are just specified like regular html attributes (class, aria-label).

If you want to pass a php expression to a snippet, e.g.: items => $site->children()->listed(), you just have to wrap it in php tags (see the code below):

With Sugar Compiled
<k:menu
  $open="<? true ?>"
  $items="<? $site->children()->listed() ?>"
  class="bg-red"
  aria-label="Main Menu"
/>
<?php snippet('menu', [
  'open' => true,
  'items' => $site->children()->listed(),
  'attr' => [
    'class' => 'bg-red',
    'aria-label' => 'Main Menu'
  ]
]); ?>

Well... actually the compiled code looks like this. To make debugging easier, the line numbers will stay the same:

With Sugar Compiled
<k:menu
  $open="<? true ?>"
  $items="<? $site->children() ?>"
  class="bg-red"
  aria-label="Main Menu"
/>
<?php snippet('menu', __snippetData([
  '$open' => true,
  '$items' => $site->children(),
  'class' => 'bg-red',
  'aria-label' => 'Main Menu'
])); ?>

This makes it super easy to implement a snippet like this:

// snippets/menu.php
<nav <?= attr($attr) ?>>
  <?php foreach ($items as $item) { /* ... */ } ?>
</nav>

Or even better with @fabianmichael's fantastic kirby-template-attributes

// snippets/menu.php
<nav <?= classes('menu', ['menu--open' => $open])->merge($attr) ?>>
  <?php foreach ($items as $item) { /* ... */ } ?>
</nav>

Prop shorthand syntax

If your prop names and php variable names are the same you can use the shorthand syntax:

<?php foreach ($projects as $project): ?>
  <k:project $project />
<?php endforeach ?>

Would be the same as:

<?php foreach ($projects as $project): ?>
  <k:project $project="<? $project ?>" />
<?php endforeach ?>

CSS Variables

You can assign CSS variables with an attribute-like syntax. This works on any tag, not just <k:my-snippet>.

Note: you can omit the var() if you are referencing another variable name (like --some-variable).

With Sugar Compiled
<k:point
  --x="10px"
  --y="--some-variable"
/>

<img --padding="2rem" />
<?php snippet('point', __snippetData([
  'style' => '--x: 10px;
  --y: var(--some-variable)'
])); ?>

<img style="--padding: 2rem" />

Debugging

If you are using xdebug, you won't be able to set breakpoints through your IDE, but you can use xdebug_break() in your source file. To remove the breakpoint, simply remove xdebug_break() and save the source file again.

Credits

统计信息

  • 总下载量: 273
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 41
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 41
  • Watchers: 2
  • Forks: 0
  • 开发语言: TypeScript

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-04-06

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固