akawalko/latte-template-renderer 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

akawalko/latte-template-renderer

最新稳定版本:v1.0.0

Composer 安装命令:

composer require akawalko/latte-template-renderer

包简介

Interface implementation for a class that renders tests/templates for string and PSR-7 response objects using Latte engine - a Next-Generation Templating System for PHP.

README 文档

README

Interface implementation for a class that renders templates for string and PSR-7 response objects using Latte engine - a Next-Generation Templating System for PHP.

Installation

You can download this library using Composer:

Require php: >=8.0

composer require akawalko/latte-template-renderer

Setup

Example integration using the PHP-DI container.

// app/dependencies.php
return function (ContainerBuilder $containerBuilder) {
        LatteRenderer::class => function (ContainerInterface $c) {
            $latte = new \Latte\Engine();
            $latte->setLoader(new FileLoader($c->get('templates.directory.host')));
            $latte->setTempDirectory($c->get('templates.directory.temp_directory'));
            $latte->setAutoRefresh($c->get('templates.directory.auto_refresh'));

            // If you have any additional filters or extensions, register them now.
            //$latte->addExtension(new SomekindExtension());
            //$latte->addFilter('some_filter', fn(string $s) => ...);

            return new LatteTemplateRenderer($latte);
        },
        // A convenient alias
        'template' => get(TemplateRenderer::class),
};

Methods

__get(string $name);

Get template variable. Return mixed value.

__set(string $name, $value): void;

Set template variable.

__isset(string $name): bool;

Determine if a variable is declared and is different than null.

__unset(string $name): void;

Unset a given variable

getVar(string $name);

Get template variable. Return mixed value.

setVar(string $name, $value): self;

Set template variable.

getVars(): array;

Get template variables.

setVars($data = []): self;

Set template variables from associative arrays or object. When an object is given, this method will work in a way similar to the spread operator known from handling arrays. That is, each object property will be assigned to the template as a standalone, e.g. Let's consider this example,

$obj = new \stdClass();
$obj->firstname = 'John';
$obj->lastname = 'Connor';
$obj->age = '34';

$latteRenderer->setVars($obj);

In the template, however, the data will be available without using a prefix in the form of an object

// this
{$firstname}<br>
{$lastname}<br>
{$age}<br>
// instead of
{$someObjectname->firstname}<br>
{$someObjectname->$lastname}<br>
{$someObjectname->$age}<br>

For this to work, the given object must meet 1 of 3 conditions:

  • implement JsonSerializable interface
  • implement toArray() method
  • simply be an object of the stdClass class

However, there is a caveat when your class implements the JsonSerializable interface. Namely, when the jsonSerialize() method returns a scalar instead of an array, its value will be assigned to a key called object_single_var. Subsequent assignments will overwrite this value. At this moment it is not possible to set a key name for the returned scalar. This may change in the future depending on how useful this feature will be.

If you are not sure what data the objects will return after executing the jsonSerialize method, it will be better to assign them using setVar(string $name, $value) method.

renderToString(string $templatePath, $data = []): string;

Render the template to string with given data.

renderToResponse(ResponseInterface $response, string $templatePath, $data = []): ResponseInterface;

Render the template to PSR compliant Response class with given data.

render(...$arguments);

A shortcut method that executes renderToString() or renderToResponse() based on the arguments passed. Return ResponseInterface or string.

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-01-10