buzzingpixel/php-template-engine

1.0.0 2023-03-28 04:21 UTC

This package is auto-updated.

Last update: 2024-09-27 02:39:30 UTC


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>