playsms/tpl

简单的PHP模板引擎

1.0.11 2023-11-17 10:26 UTC

This package is auto-updated.

Last update: 2024-09-17 14:03:42 UTC


README

简单的PHP模板引擎

安装

通过编辑 composer.json 使用 composer 进行安装。

最小 composer.json

{
	"require": {
		"playsms/tpl": "1.*"
	}
}

更多有关 composer 的信息可以在其网站上找到 https://getcomposer.org.cn

此软件包也可以在不使用 composer 的情况下安装。您只需简单地包含 src/Playsms/Tpl.php 即可。

使用示例

一个示例模板文件 the_page.html

<div>
	<p>This is the title: {{ title }}</p>
	<p>This is the content: {{ content }}</p>
	<p>And this is the data: {{ $data }}</p>
	<loop.lines>
	<p style='background-color: {{ lines.hexcode }}'>Color: {{ lines.color }}</p>
	</loop.lines>
</div>

使用模板文件 the_page.html 的示例 PHP 文件 show_page.php

<?php

require 'vendor/autoload.php';

$data = 'THE DATA HERE';

$loops = array(
	'lines' => array(
		array('color' => 'Red',   'hexcode' => '#FF0000'),
		array('color' => 'Green', 'hexcode' => '#00FF00'),
		array('color' => 'Blue',  'hexcode' => '#0000FF'),
	),
);

$tpl = new \Playsms\Tpl;

$tpl->setTemplate('./templates/test6.html');

$tpl->setVars(array(
	'title' => 'THE TITLE HERE',
	'content' => 'THE CONTENT HERE',
	))
	->setLoops($loops)
	->setInjects(array('data'));

$tpl->compile();

echo $tpl->getCompiled();

compile() 之后,您可以使用 getCompiled() 获取编译内容

<div>                                                                                                                                                        
    <p>This is the title: THE TITLE HERE</p>                                                                                                                 
    <p>This is the content: THE CONTENT HERE</p>                                                                                                             
    <p>And this is the data: THE DATA HERE</p>                                                                                                               
                                                                                                                                                             
    <p style='background-color: #FF0000'>Color: Red</p>                                                                                                      
                                                                                                                                                             
    <p style='background-color: #00FF00'>Color: Green</p>                                                                                                    
                                                                                                                                                             
    <p style='background-color: #0000FF'>Color: Blue</p>                                                                                                     
                                                                                                                                                             
</div>

更多示例请参阅 examples 文件夹。

其他文档可以在 docs 文件夹中找到。