html5/template 问题修复 & 功能扩展

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

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

html5/template

Composer 安装命令:

composer require html5/template

包简介

Inline Templating component

README 文档

README

Downloads this Month Latest Stable Version

Html5 Template - Template Rendering

angularjs-like inline-templates for PHP7.

Planned as replacement for PHPTAL - which is quite fine but not enough extensible for cluster / platform usage.

Gismo Template does not precompile any code. It generates the pages on the fly.

Features:

  • Works with HTML5 and XHTML (XHTML preferred)
  • IDE supported template-development
  • Generate HTML, plain-Text or struct-data from one file
  • Supports macros
  • Import other templates from virtual storage (cloud-ready)
  • Verbose and useful error-messages with lineNo and xpath
  • Secure and powerful expression engine (Symfony Component also used by Twig)

Example

Template:

<div go-if="showHeader==true">Welcome {{ user.name }}</div>
<div go-foreach="products as curProduct">
    <span go-bind="curProduct.name"></span>
</div>

Parser:

$template = new HtmlTemplate();
echo $template->render("template.xhtml", ["showHeader" => true, "producs"=> [ new Product("prod1"), new Product("Product2)" ]]);

Install using Composer

composer require html5/template

Attributes

  • go-if: Condition
  • go-foreach: Loop
  • go-repeat: Loop
  • go-continue: Loop start-over
  • go-break: Quit loop
  • go-bind: Inject string data
  • go-html: Inject html data

Conditions

<div go-if="name == 'Matthias'">
    Hello {{ name }}
<div>

Foreach Loops

<div go-foreach="products as product">
    Product Title: {{product.title}} 
</div>
<div go-foreach="data as curKey => curVal">
    Key: {{curKey}} Value: {{curVal}}
</div>

Repeat Loops

<div go-repeat="100 indexBy myIndex">
    Line {{ myIndex }}: Hello
</div>

break / continue a loop

You can use conditions to break or continue a loop

<div go-loop="100 indexBy index">
    <go-break go-if="! count(productList) < index"/>
    ...
</div>

or use continue to skip elements:

<div go-foreach="products as product">
    <go-continue go-if="product.isHidden == true"/>
    ...
</div>

Bind escaped Value

<span go-bind="user.name">Name to show if user.name is null</span>

The data will be escaped by htmlspecialchars()

Bind Html-Code

<div go-html="page.htmlCode">Show <b>this</b> if page.htmlCode is null</div>

Add CSS Classes dynamicly

<div go-class="{cssClassName: name=='Matthes', otherClassName: highlight==true}">..</div>

Show/Hide Elements

<div go-show="showIt==true"></div>
<div go-hide="hideIt==true"></div>

if hidden it adds the css-class ng-hide (also used by angularjs)

Elements

  • go-text: Inject Text
  • go-define: Define Variables in Scope
  • go-dump: Dump a variable or scope
  • go-macro: Define a macro
  • go-callmacro: Call a macro
  • go-struct: Return array data sections defined by go-section
  • go-section: Define or overwrite struct data

Interceptors

Macros

Macros can be used to create Output on multiple positions. To define a macro use the go-macro-Element:

<go-macro name="printDemoTable(headers, data)">
    <table>
        <thead>
            <tr>
                <td go-foreach="headers as curHeader">{{ curHeader }}</td>
            </tr>
        </thead>
        <tbody>
            ...
        </tbody>
    </table>
</go-macro>

To generate your Table use go-callmacro - Element:

<body>
    <go-callmacro name="printDemoTable(shoppingCart.headers, shoppingCart.items)"/>
</body>

Benchmark

Small Example from above:

Parsing + Rendering: <0.01s

Big demo with ~2000 Lines of template-code

Template-Parsing: <10ms
Rendering: <15ms

Installing V8JS on Ubuntu 16.04

Since there is no apt package for v8js you have to do it manually:

sudo add-apt-repository ppa:pinepain/libv8-5.2
sudo apt-get update
sudo apt-get install libv8-dev  g++ cpp php-pear php7.0-dev
sudo pecl install v8js

sudo bash
echo "extension=v8js.so" > /etc/php/7.0/apache2/conf.d/20-v8js.ini
echo "extension=v8js.so">/etc/php/7.0/cli/conf.d/20-v8js.ini

service apache2 restart

Author

Written 2016 by Matthias Leuffen http://leuffen.de

html5/template 适用场景与选型建议

html5/template 是一款 基于 HTML 开发的 Composer 扩展包,目前已累计 3.12k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 08 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「template」 「HTML5」 「html」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 0
  • 开发语言: HTML

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-08-16