buzzingpixel / php-template-engine
Requires
- php: >=8.2
- laminas/laminas-escaper: ^2.12
- spatie/php-cloneable: ^1.0
Requires (Dev)
- buzzingpixel/symfony-var-dumper-decorator: ^1.0
- doctrine/coding-standard: ^11.0.0
- phpstan/phpstan: ^1.10
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-strict-rules: ^1.5
- squizlabs/php_codesniffer: ^3.6
- symfony/var-dumper: ^6.2
README
简单、基础的PHP模板。
使用方法
安装
composer require buzzingpixel/php-template-engine
渲染模板
从TemplateEngineFactory
获取TemplateEngine
实例。
use BuzzingPixel\Templating\TemplateEngineFactory; $templateEngine = (new TemplateEngineFactory())->create();
需要设置的唯一项是模板路径。
$templateEngine->templatePath('/path/to/template.phtml');
(注意:pthml
仅是一种约定。TemplateEngine不关心扩展名)
变量是可选的。如果您要设置任何变量,可以一次性添加
$templateEngine->vars([ 'foo' => 'bar', 'baz' => 'foo', ]);
…或者您可以逐个添加
$templateEngine->addVar('foo', 'bar')->addVar('baz', 'foo');
准备好后,在TemplateEngine
实例上调用render()
,您的模板将被渲染,内容将作为字符串返回。以下是一个完整示例。
use BuzzingPixel\Templating\TemplateEngineFactory; $renderedContent = (new TemplateEngineFactory())->create() ->templatePath(__DIR__ . '/my/template.phtml') ->addVar('foo', 'bar') ->render();
模板化
为了在您的IDE中获得PHP模板的自动完成,您可以断言$this
是\BuzzingPixel\Templating\TemplateEngine
的一个实例。您还可以断言在模板中期望的任何变量
<?php assert($this instanceof \BuzzingPixel\Templating\TemplateEngine); assert(is_string($foo)); ?>
模板继承、扩展和部分
扩展另一个模板相对简单。只需在模板的某个地方运行$this->extends('/path/to/some/template.phtml')
(我喜欢在顶部做这个)。模板中的所有渲染内容都将分配给名为layoutContent
的部分。
如上所述,默认情况下,在扩展时,所有未放置在部分中的内容都将分配给layoutContent
部分。这很好,因为它意味着如果您只渲染一件事情并将其推送到堆栈上,您不需要做很多工作。因此,一个简单的扩展可能看起来像这样
/my-template.phtml
:
<?php assert($this instanceof \BuzzingPixel\Templating\TemplateEngine); assert(is_string($pageTitle)); $this->extends(__DIR__ . '/layout.phtml'); ?> Hello World!
/layout.phtml
:
<?php assert($this instanceof \BuzzingPixel\Templating\TemplateEngine); assert(is_string($pageTitle)); ?> <html> <head> <title><?= $this->html($pageTitle) ?></title> </head> <body> <?= $this->getSection('layoutContent') ?> </body> </html>
您可以在扩展中无限深入。扩展的模板可以反过来扩展另一个模板。
但是,您可能还希望为扩展的模板设置其他部分,例如侧边栏。这可能会看起来像这样
/my-template.phtml
:
<?php assert($this instanceof \BuzzingPixel\Templating\TemplateEngine); assert(is_string($pageTitle)); $this->extends(__DIR__ . '/layout.phtml'); ?> <?php $this->sectionStart('sidebar') ?> <ul> <li>Foo</li> <li>Bar</li> </ul> <?php $this->sectionEnd() ?> Hello World!
/layout.phtml
:
<?php assert($this instanceof \BuzzingPixel\Templating\TemplateEngine); assert(is_string($pageTitle)); ?> <html> <head> <title><?= $this->html($pageTitle) ?></title> </head> <body> <?php if ($this->hasSection('sidebar')): ?> <?= $this->getSection('sidebar') ?> <?php endif ?> <?= $this->getSection('layoutContent') ?> </body> </html>
部分
代码的可重用性是一个大问题,这就是为什么部分很受关注。部分的工作方式有点像这样
/my-partial.phtml
<?php assert($this instanceof \BuzzingPixel\Templating\TemplateEngine); assert(is_array($items)); ?> <?php foreach ($items as $item): ?> <li><?= $item ?></li> <?php endforeach ?>
/my-template.phtml
:
<?php assert($this instanceof \BuzzingPixel\Templating\TemplateEngine); assert(is_string($pageTitle)); $this->extends(__DIR__ . '/layout.phtml'); ?> <?php $this->sectionStart('sidebar') ?> <ul> <?= $this->partial(__DIR_ '/my-partial.phtml', [ 'Foo', 'Bar', ]) ?> </ul> <?php $this->sectionEnd() ?> Hello World!
部分是TemplateEngine
的完整实例,因此它们具有任何其他模板的所有功能。它们不继承调用模板的变量上下文。
转义
TemplateEngine
有转义辅助方法,您应该在使用来自不受信任来源的输出时使用这些方法。
<?php assert($this instanceof \BuzzingPixel\Templating\TemplateEngine); assert(is_string($id)); assert(is_string($js)); assert(is_string($css)); assert(is_string($url)); assert(is_string($pageTitle)); $this->extends(__DIR__ . '/layout.phtml'); ?> <style> <?= $this->css($css) ?> </style> <h1><?= $this->html($pageTitle) ?></h1> <a id="<?= $this->attr($id) ?>" href="<?= $this->url($url) ?>" > My Link </a> <script> <?= $this->js($js) ?> </script>