定制 hokoo/templater 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

hokoo/templater

Composer 安装命令:

composer require hokoo/templater

包简介

Keep your templates in one place apart from your php code.

README 文档

README

Latest Stable Version PHPUnit Tests

HTML Templater for PHP.

Anatomy logo

Lightweight, simple but powerful templater for rendering HTML templates in PHP.

I believe that you'd like to

  • keep your HTML clean and simple, and
  • maintain the logic in PHP, not in HTML.

Table of Contents

Requirements

PHP 8.0 and later.

Installation

composer require hokoo/templater

Getting Started

Here are some examples of how to use the templater.

🅰️ Tags

Example:

<div class="article">
    <h2>
        <!-- This is a tag named "title". -->
        {{title}}
    </h2>
    
    <div class="content">
        <!-- This is a tag named "content". -->
        {{content}}
    </div>

</div>

PHP code:

use iTRON\Anatomy\Templater;
$templater = new Templater();

return $templater->render( $html, [
    'title'   => 'The Best Title',
    'content' => 'Anything you want',
] );

The result will be:

<div class="article">
    <h2>
        <!-- This is a tag named "title". -->
        The Best Title
    </h2>
    
    <div class="content">
        <!-- This is a tag named "content". -->
        Anything you want
    </div>

</div>
Read more theory about Tags

Tag - a point in the template where you can insert a value. Tag should be considered as a placeholder for a value.

In the Anatomy's paradigm, the 'tag' is only entity that can be replaced with a value.

The tag is a string that starts with {{ and ends with }}.

So, in the example above, the {{title}} and {{content}} are tags. Let's render the template with the values.

🅰️ Predefined tags

Example:

<div class="{{#class=[first|second|third]}}"></div>

PHP code:

$templater = new \iTRON\Anatomy\Templater();

$result = $templater->render( $html, [
    'class' => 0,
] );

The result will be:

<div class="first"></div>
Read more theory about Predefined Tags Predefined tags are tags that can render only values predefined by the template.

Predefined tag's modifier can only accept an integer value as index of one of the predefined values (starting from 0). Any invalid modifier value (non-integer or integer that points beyond of the array) will be considered as 0.

The default values' delimiter is |. You can change it by setting the delimiter property of the tag.

<div class="{{#class=[first!!second!!third] delimiter=[!!]}}"></div>

🅰️ Blocks & Containers

Example:

<div class="feed">
    <h1>{{title}}</h1>

    {{content}}
    
    <!-- This is a block named "article". -->
    [[#article]]
    <div class="article">
        <h2>{{title}}</h2>
    
        <div class="content">
            {{content}}
        </div>
    </div>
    [[/article]]
</div>

PHP code:

// This is a container. 
// It can contain blocks and texts.
$content = new \iTRON\Anatomy\Container();

// Let's put block "article" into the container.
$content->addBlock( 'article', [
    'title'   => 'The Best Title',
    'content' => 'Anything you want',
] );

// Another one.
$content->addBlock( 'article', [
    'title'   => 'There is no title',
    'content' => 'There is no content.',
] );

// Or you can add a plain text.
$content->addText( 'Plain text.' );

$templater = new iTRON\Anatomy\Templater();

return $templater->render( $html, [
    'title'   => "Feed's Title",
    'content' => $content
] );

The result will be:

<div class="feed">
    <h1>Feed's Title</h1>
    
    <!-- This is a block named "article". -->
    <div class="article">
        <h2>The Best Title</h2>
    
        <div class="content">
            Anything you want
        </div>
    </div>
    
    <div class="article">
        <h2>There is no title</h2>
    
        <div class="content">
            There is no content.
        </div>
    </div>
  
    Plain text.
  
</div>
Read more theory about Blocks & Containers

Blocks are a way to have a component-like structure in the template. You can consider blocks as a template inside a template.

You can define as many blocks as you want and render them in any order.

You can put one block into another block. This is how you can create a nested structure without any restrictions on the depth of nesting.

🅰️ Detached mode

Since we consider blocks as an almost independent template, you can use blocks out of context of the main template.

Suppose, you need to render the "article" block from the template above in a different place. Great, there's no need to duplicate the block's code. Just render the block separately.

$templater = new \iTRON\Anatomy\Templater();

echo $templater->renderBlock(
    $html, 
    'article', 
    [ 
      'title' => 'Title',
      'content' => 'Content'
    ] 
);

The result would be:

<div class="article">
    <h2>Title</h2>
    
    <div class="content">
        Content
    </div>
</div>

Back compatibility

Version 3 is still supported. Syntax of the older versions is not supported anymore.

To use v3 syntax, you have to instantiate the legacy version of the templater:

$templater = new \iTRON\Templater\Templater();

// Instead of
// $templater = new \iTRON\Anatomy\Templater();

hokoo/templater 适用场景与选型建议

hokoo/templater 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.12k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2024 年 01 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 hokoo/templater 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 hokoo/templater 我们能提供哪些服务?
定制开发 / 二次开发

基于 hokoo/templater 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.12k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 19
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: GPL-3.0-only
  • 更新时间: 2024-01-05