aleskafka/component-macro

扩展Latte的简单组件模型

0.1 2015-04-19 09:51 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:20:20 UTC


README

扩展Latte的简单组件模型,灵感来自React的JSX

安装

使用composer

$ composer require aleskafka/component-macro

在配置文件中安装到Latte引擎

$latte = new Latte\Engine;
ComponentMacro::install($latte, __DIR__ . '/components');

基本示例

__DIR__ . '/components/header.latte中创建Header组件作为latte模板

{default title=>'', subtitle=>'', follow=>NULL}
<h1>{$title}<small n:if="$subtitle">{$subtitle}</small>
  {if $follow}
  <a href="?follow=0"><span class="fa fa-ok"></span> followed</a>
  {else if $follow!==NULL}
  <a href="?follow=1"><span class="fa fa-add"></span> follow</a>  
  {/if}
</h1>

在任意页面模板中使用特定参数渲染Header组件。

<div class="page">
  <Header n:component="title=>$page->title"></Header>
  
  <div class="page-body"></div>
</div>

功能

更多示例请参见测试。简而言之,您可以使用以下方式:

  • 以React风格渲染组件
  • 您可以在组件中使用变量 $_html。它包含在组件标签内渲染的父模板的body
  • 您可以将变量扩展到组件中 <Header ..$page n:component />
  • 您可以将定义的变量扩展到组件中 <Header ..$this n:component />
  • 您可以使用宏n:key="string"(键可以是动态的)命名组件内的元素/组件,并在父组件标签内覆盖它
<Header n:component="...">
  <span n:child="string">Override span[string] subcomponent</span>
</Header>

许可协议

MIT。请参阅完整的许可协议