stuartwakefield / templating
无装饰的模板
v0.1.1-alpha
2014-08-23 16:59 UTC
Requires
- php: >=5.3.0
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-24 07:06:53 UTC
README
需要PHP 5.3+
一个超级轻量级的模板系统,让PHP模板变得非常棒。
基本用法
从一个非常简单的应用开始,该应用具有以下布局
├── templates
│ └── greeting.phtml
├── composer.json
└── index.php
composer.json文件在require映射中包含stuartwakefield/templating
{
"require": {
"stuartwakefield/templating": "0.1.0a"
}
}
模板文件templates/greeting.phtml的内容如下
<p>Hey there, <?= $this->name ?>!</p>
index.php脚本
<?php require_once 'vendor/autoload.php'; use Templating\Template; $template = new Template('templates/greeting.phtml'); $result = $template->fill(array( 'name' => 'Bob' )); echo $result; // <p>Hey there, Bob!</p>
就这么简单!
演示者
这个模板库真正酷的地方在于,你可以轻松地将演示者对象与模板一起使用。
让我们设置一个基本的演示者src/Presenter.php,并假设你已经设置了composer来自动加载类
<?php class Presenter { private $name; function __construct($name) { $this->name = $name; } function greet() { return 'Hello, ' . $name . '! You are logged in.'; } }
然后在新的模板templates/account.phtml
<p><?= $this->greet() ?></p>
我们的更新后的index.php看起来像这样
<?php require_once 'vendor/autoload.php'; use Templating\Template; $template = new Template('templates/account.phtml'); $presenter = new Presenter('Khan'); $result = $template->fill($presenter); echo $result; // <p>Hello, Khan! You are logged in.</p>