grifix / view
此包已被 废弃 并不再维护。未建议替代包。
简单的模板引擎,支持槽位和继承
dev-main
2022-12-06 07:44 UTC
Requires
- php: ^8.1
- grifix/array-wrapper: dev-main
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2023-07-26 07:07:45 UTC
README
基于PHP的一个非常简单的模板引擎,支持槽位和继承
安装
composer require grifix/view
用法
基本模板
template.php
<?php
declare(strict_types=1);
use \Grifix\View\TemplateInterface;
/** @var $this TemplateInterface */
?>
<h1><?=$this->getVar('title')?></h1>
<p><?=$this->getVar('content')?></p>
index.php
declare(strict_types=1);
use \Grifix\View\ViewFactory;
echo ViewFactory::create('/path/to/views')->createView('template.php')->render([
'title' => 'Hello',
'content' => 'Hello world!'
])
将渲染
<h1>Hello</h1>
<p>Hello world!</p>
继承
layout.php
<?php
declare(strict_types=1);
use \Grifix\View\TemplateInterface;
/** @var $this TemplateInterface */
?>
<h1><?php $this->startSlot('title')?>Hello<?php $this->endSlot()?></h1>
<p><?php $this->startSlot('content')?>Hello world<?php $this->endSlot()?></p>
template.php
<?php
declare(strict_types=1);
use \Grifix\View\TemplateInterface;
/** @var $this TemplateInterface */
$this->inherits('layout.php');
?>
<?php $this->startSlot('content');?>
Lorem ipsum dolor sit amet, consectetur
<?php $this->endSlot();?>
index.php
declare(strict_types=1);
use Grifix\View\ViewFactory;
echo ViewFactory::create('/path/to/views')->createView('template.php')->render();
将渲染
<h1>Hello</h1>
<p>Lorem ipsum dolor sit amet, consectetur</p>
带有父槽位内容的继承
template.php
<?php
declare(strict_types=1);
use \Grifix\View\TemplateInterface;
/** @var $this TemplateInterface */
$this->inherits('layout.php');
?>
<?php $this->startSlot('content');?>
<?=$this->renderParenSlot()?>
Lorem ipsum dolor sit amet, consectetur
<?php $this->endSlot();?>
将渲染
<h1>Hello</h1>
<p>Hello world! Lorem ipsum dolor sit amet, consectetur</p>