vakata/views

简单的PHP模板引擎

2.0.2 2016-10-27 11:39 UTC

This package is auto-updated.

Last update: 2024-09-10 05:33:41 UTC


README

Latest Version on Packagist Software License Build Status Code Climate Tests Coverage

这是一个简单的PHP模板引擎。

安装

通过Composer

$ composer require vakata/views

用法

use vakata\views\Views;

$views = new vakata\views\Views();

// register template dirs
$views->dir('/path/to/templatedir');
$views->dir('/path/to/otherdir', 'other');

// a variable available in all templates
$views->share("siteTitle", "test");

// variables available in all templates
$views->share(["a" => 1, "b" => 2]);

// render a template from the first dir:
$views->render('profile', ['user' => 'Test']);

// render a template from a named dir:
$views->render('other::user', ['user' => 'Test']);

// the above is the same as
$v = $views->get('other::user');
$v->render(['user'=>'Test']);

一个示例模板可能看起来像这样

<?php $this->layout('master.layout', ['masterParam' => 'master-value']); ?>

Content

<?php $this->sectionStart("sidebar"); ?>
Here is some unfiltered content: <?= $b ?> 
Here is some filtered content: <?= $this->e($user) ?> 
<?php $this->sectionStop(); ?>

Here is some filtered and transformed content:
<?= $this->e($user, 'trim|strtouuper') ?> 

Include a child template:
<?= $this->insert('nameddir::include', ['optional' => 'params']); ?>

至于主模板(它本身也可以有自己的主模板)

I am master!

<?= $this->section("sidebar") ?>

<?= $masterParam ?> <?= $a ?>

The unnamed part of the above template:
<?= $this->section() ?>

API文档中了解更多

测试

$ composer test

贡献

请参阅CONTRIBUTING以获取详细信息。

安全

如果您发现任何与安全相关的问题,请通过github@vakata.com发送电子邮件,而不是使用问题跟踪器。

鸣谢

许可证

MIT许可证(MIT)。请参阅许可证文件以获取更多信息。