asgard/templating

v0.3.1 2016-05-13 11:31 UTC

This package is not auto-updated.

Last update: 2024-09-14 15:21:55 UTC


README

# 模板化

Build Status

模板化是一个简单的包,它提供接口以构建自己的模板系统。它还提供PHP模板系统和Viewable类。

## 安装 如果你正在处理Asgard项目,你不需要安装这个库,因为它已经是标准库的一部分。

composer require asgard/templating 0.*

## 接口

### TemplateEngineInterface

public function templateExists($template); #check that a template exists
public function getTemplateFile($template); #return the file corresponding to the template name
public function createTemplate(); #return a new instance of the template class (implementing TemplateInterface)

### TemplateInterface

public function getEngine();
public function setEngine(TemplateEngineInterface $engine);
public function setTemplate($template);
public function getTemplate();
public function setParams(array $params=[]);
public function getParams();
public function render($template=null, array $params=[]);
public static function renderFile($file, array $params=[]);

## PHP模板

PHPTemplate类实现了TemplateInterface。

创建一个新的模板

$template = new Asgard\Templating\PHPTemplate('template.php', ['title'=>'Hello!']);

设置模板

$template->setTemplate('template2.php');

获取模板

$template->getTemplate(); #template2.php

设置参数

$template->setParams(['title'=>'Hello!']);

获取参数

$template->getParams(); #['title'=>'Hello!']

渲染模板

$template->render();

使用参数渲染特定模板

$template->render('template.php', ['title'=>'Hello!']);

静态渲染模板

Asgard\Templating\PHPTemplate::renderFile('template.php', ['title'=>'Hello!']);

## 可渲染

可渲染特性提供方法,以便类可以轻松地使用模板进行渲染。

### 使用

class Abc {
	use \Asgard\Templating\Viewable;

	public function test($param1, $param2) {
		return 'test';
	}
}

$abc = new Abc;

$abc->setTemplateEngine($engine);
#the engine will be passed to any template used by the class
#$abc->getTemplateEngine(); to get the engine

$abc->run('test', ['param1', 'param2']);

### 渲染

public function test() {
	return 'test';
}
#run('test') returns 'test'

public function test() {
	echo 'test';
}
#run('test') returns 'test'

public function test() {
	return new MyTemplate('template.php', [/*..*/]);
}
#run('test') will call ->render() on the template and return the result

public function test() {
	$this->view = new MyTemplate('template.php', [/*..*/]);
}
#run('test') will call ->render() on $this->view and return the result

public function test() {
	$this->view = 'template';
}
#if the object as a TemplateEngine, it will create a template instance and pass 'template.php' to it.
#if not, Viewable will use its own default rendering technique.

### 默认渲染

当模板名称传递给 $this->view,且对象没有自己的TemplateEngine时,Viewable将尝试解决与模板名称相对应的模板文件,包含它并传递自己的变量。

例如

#Viewable class test method
public function test() {
	$this->title = 'Hello!';
	$this->view = 'template'; #template matches /var/www/project/templates/template.php
}

#template.php
echo '<h1>'.$title.'</h1>';

将返回

<h1>Hello!</h1>

你可以通过以下方式帮助可渲染对象解决模板文件

$abc->addTemplatePathSolver(function($obj, $template) {
	$file = '/var/www/project/templates/'.$template.'.php';
	if(file_exists($file))
		return $file;
});

### 静态渲染

Abc::fragment('tes', [$param1, ...]);

### 贡献

请将所有问题和拉取请求提交到 asgardphp/asgard 仓库。

许可证

Asgard框架是开源软件,许可协议为 MIT许可