aura.view



简介

Aura View包是对TemplateView样式的实现,支持视图辅助和路径栈。它的遵从“使用PHP做显示逻辑”的理念,并且它优于SavantZend_ViewSolar_View

此包依赖于Aura DI包。


基本使用


实例化

实例化Template(所有相关的视图辅助都可用)最简单的方法是包含Aura DI包源码,然后调用instance.php脚本。

<?php
// business logic
require_once '/path/to/aura.di/src.php';
$template = require '/path/to/aura.view/scripts/instance.php';

然后调用Template对象的fetch()方法去获取模板脚本的输出。

<?php
// business logic
echo $template->fetch('/path/to/tpl.php');

当然,我们也可以把aura.di/srcaura.view/src目录添加到自动装载器中,然后手动实例化:

<?php
use aura\di\Container;
use aura\di\Forge;
use aura\di\Config;
use aura\view\Template;
use aura\view\Finder;
$template = new Template(
    // a helper container
    new Container(new Forge(new Config)),
    // a template finder
    new Finder
);

(注意,如果我们手动实例化视图对象,就不得不配置Container以添加视图辅助服务。更多信息请在本页末尾查看“视图辅助”部分。)


分配数据

我们可以通过设置Template对象属性的方式把变量分配给模板脚本,如:

<?php
// business logic
$template->var = 'World';

然后我们可以在模板脚本中通过$this引用它们。

<?php
// template script
$e = $this->getHelper('escape');
echo $e($this->var);

我们也可以一次添加多个数据属性,使用addData()方法把现有数据和新数据合并。

<?php
// business logic
$template->addData(array(
    'foo' => 'Value of foo',
    'bar' => 'Value of bar',
));

(注意,数组键名将会被映射到对象的属性,所以请确保它们是合法的属性名。)

最后,我们可以使用setData()方法替换所有Template中的变量值。

<?php
// business logic
// this will remove $var, $foo, and $bar from the template
$template->setData(array(
    'baz' => 'Value of baz',
    'dib' => 'Value of dib',
));


编写模板脚本

Aura View使用普通的PHP书写模板脚本,不需要一门新的标记语言。模板脚本在Template对象的作用域中执行,所以可以使用$this引用Template对象。下面给出一个示例:

<?php $e = $this->getHelper('escape'); ?>
<html>
<head>
    <title><?php echo $e($this->title) ?></title>
</head>
<body>
    <p><?php
        echo "Hello " . $e($this->var) . '!';
    ?></p>
</body>
</html>

我们可以在模板中正常使用任何PHP代码。(这里要求作者严格磨练自己,在模板脚本中只处理显示逻辑。)我们也希望使用另一种语法来书写PHP条件语句和循环语句:

<?php if ($this->message): ?>
    <p>The message is <?php echo $e($this->message); ?></p>
<?php endif; ?>

<ul>
<?php foreach ($this->list as $item): ?>
    <li><?php echo $e($item); ?></li>
<?php endforeach; ?>
</ul>


使用视图辅助

Aura View有很多Helper类,用于封装常用的显示逻辑。这些辅助类通过辅助容器Container映射到Template对象上。调用辅助有两种方法:

  • 作为Template对象的一个方法
  • 通过getHelper()方法获取辅助对象本身

$this->escape()是最重要的辅助方法。每次echo或print分配变量时都需要使用它。(其他辅助方法会自动调用escape方法。)你可以这样调用它:

<?php
// template script
echo $this->escape($this->var);

或这样调用:

<?php
// template script
$e = $this->getHelper('escape');
echo $e($this->var);

Aura View的其他视图辅助如下:

  • $this->anchor($href, $text)返回一个<a href="$href">$text</a>标签
  • $this->attribs($list)从键值对数组$list返回一个以空格分隔的属性列表
  • $this->base($href)返回一个<base href="$href" />标签
  • $this->datetime($datastr, $format)返回格式化的时间字符串
  • $this->image($src)返回一个<img src="$src" />标签
  • $this->metas()返回一个对象,提供添加和取回<meta ... />标签的方法。
    • $this->metas()->addHttp($http_equiv, $content)添加一个<meta http-equiv="$http_equiv" content="$content" />标签。
    • $this->metas()->addName($name, $content)添加一个<meta name="$name" content="$content" />标签。
    • $this->metas()->get()从视图辅助返回所有添加的meta标签。
  • $this->scripts()返回一个对象,提供添加和取回<script ...></script>标签的方法。
    • $this->scripts()->add($src)添加一个script标签。
    • $this->scripts()->get()从视图辅助返回所有添加的script标签。
  • $this->styles()返回一个对象,提供添加和取回<link rel="stylesheet" ... />标签的方法。
    • $this->styles()->add($href)添加style标签。
    • $this->styles()->get()从视图辅助返回所有添加的link标签。
  • $this->title返回一个对象操作<title>...</title>标签。
    • $this->title()->set($title)设置title值。
    • $this->title()->append($suffix)title末尾追加。
    • $this->title()->prepend($prefix)在<ocde>title</code>前面追加。
    • $this->title()->get()返回经过适当转换后的title标签。
    • $this->title()->getRaw()返回未经转换后的title标签。


高级用法


模板查找

尽管我们可以使用fetch()方法获取一个绝对路径的模板脚本,但指定模板的一个或多个路径会更强大。然后我们同样可以使用fetch()方法获取该模板,Finder会在指定的路径中查找该模板。这允许我们指定的基准模板,并根据需要重写。

要使用Finder查找模板,我们要先告诉Finder到哪去找模板脚本:首先从Template对象中获取Finder对象,然后调用setPaths()方法。

<?php
// business logic
$finder = $template->getFinder();

// set the paths where templates can be found
$finder->setPaths(array(
    '/path/to/templates/foo',
    '/path/to/templates/bar',
    '/path/to/templates/baz',
));

现在,当你调用fetch()方法时,Template对象将会使用Finder在那些目录中查找我们指定的模板脚本。

例如,如果我们echo $template->fetch('tpl')Finder将会按序在每个目录中查找’tpl.php’模板脚本。这充许我们建立多个模板路径,并可以在基准模板之前的某个路径中放置替换模板。


模板组成

我们通常需要把一个模板分离成多块碎片。这充许我们保持不同内容碎片的逻辑独立性。例如,一个页面可以分为头部,导航,侧边栏等。

我们可以在一个模板脚本中使用$this->find()方法查找别一个模板,然后在需要的时候包含它include即可。例如:

<?php
    // template script
    $e = $this->getHelper('escape');
?>
<html>
<head>
    <?php include $this->find('head'); ?>
</head>
<body>
    <?php include $this->find('branding'); ?>
    <?php include $this->find('navigation'); ?>
    <p>Hello, <?php echo $e($this->var); ?>!</p>
    <?php include $this->find('footer'); ?>
</body>
</html>

以这种方式包含include进来的模板将会和当前模板共享同一个作用域。


模板碎片

模板碎片是作用域独立的模板分离方式。我们可以在一个模板脚本中使用fetch()方法获取另一个模板;但它们不共享同一个作用域(尽管$this仍可用)。另外,我们还可以向碎片模板传入数组参数(在碎片模板中会自动解析extract出变量。)

例如,如下碎片模板。。。

<?php
// partial template named '_partial.php'.
// note that we use $name, not $this->name.
$e = $this->getHelper('escape');
echo "    <li>" . $e($name) . "</li>" . PHP_EOL;

。。。我们可以在另一个模板中获取fetch()它:

<?php
// main template. assume $this->list is an array of names.
foreach ($this->list as $item) {
    $template_name = '_partial';
    $template_vars = array('name' => $item);
    echo $this->fetch($template_name, $template_vars);
}

这会在一个独立的作用域中执行$template_name模版脚本,并在模版中解析$templage_vars数组。


写视图辅助

自己写视图的话有两个工作要做:

  1. 写一个辅助类
  2. 在视图容器Container中把该类添加为服务

编写辅助类是很简单的:继承AbstractHelper类,并实现__invoke()方法。例如,下面的视图辅助对一个字符串执行ROT-13处理。

<?php
namespace vendor\view\helper;
use aura\view\helper\AbstractHelper;

class Obfuscate extends AbstractHelper
{
    public function __invoke($string)
    {
        return $this->escape(str_rot13($input));
    }
}

要养成好习惯,总是转换用户输入。错误的转换也比不转换要好。

我们已经写好了一个视图辅助类,现在要做的就是在视图辅助容器Container把它添加为服务,如:

<?php
// business logic
$hc = $template->getHelperContainer();

$hc->set('obfuscate', function() use ($hc) {
    return $hc->newInstance('vendor\view\helper\Obfuscate');
});

视图辅助容器Container中的服务名同时也是Template对象的方法名。也就是说我们可以使用$this->obfuscate()调用该辅助类:

<?php
// template script
echo $this->obfuscate('plain text');

注意,事实上辅助类可以使用任何名字,但是通常来说和服务名保持一致会更加直观。

请到aura\view\helper查看更复杂、更强大的例子。

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>