net_bazzline/php_component_template 问题修复 & 功能扩展

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

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

net_bazzline/php_component_template

Composer 安装命令:

composer require net_bazzline/php_component_template

包简介

free as in freedom free software template engine for php

README 文档

README

This project aims to deliver an easy to use, free as in freedom and fast template engine for php (code name: yepte - yet another php template engine).

The build status of the current master branch is tracked by Travis CI: Build Status Latest stable

The scrutinizer status are: code quality

The versioneye status is: Dependency Status

Take a look on openhub.net.

Why

I wanted to create a lean (in matter of lines of code and number of files) fast, extendable but expendable template engine for php. This project does not aim to shake on the throne of the big template engine available for php. They have different goals, more manpower and different ideas behind.

Personally, I like the php-text-template but sebastian has a different approach in mind (writing something to a file). Adding my goals to his project would add more complexity to his library.

Available Templates

Currently, this component tries to solve three problems when dealing with php templates. All Templates are stackable, meaning you can assign one template key with another template instance.

RuntimeContentBasedTemplate solve the problem to replacing content stored in a string.

FileBasedTemplate solves the problem replacing content stored in a file.

ComplexFileBasedTemplate solves the problem replacing complex content stored in a file. This is commonly known as the view in php frameworks.

CallableComplexFileBasedTemplateManager solves the problem externalise reusable template tasks. This is commonly known as the view helper pattern.

Notes

What is a complex content?

Complex content contains code like:

$isFoo = ($bar === 'foo');
if ($isFoo) {
     /* ... */ 
} else { 
    /*  ... something else */ 
}

What is a callable?

Callable is an other word for the famous view helper design pattern. The template provides a method called "registerCallable" to register a callable and bind it to a name.

//assuming the file in the relative path 'template.phtml' has the following content
//<?php $this->foobar('foo', 'bar');
$myViewHelper = function($foo, $bar) {
    return 'there is no ' . $foo . ' without a ' . $bar;
}
$template = new CallableComplexFileBasedTemplateManager('template.phtml');
$template->registerCallable('foobar', $myViewHelper);
echo $template->render() . PHP_EOL;
//expected result: there is no foo without a bar

What kind of complex content should I use?

Well, it is up to you and the code is pretty flexible.

My two cents are, limit yourself to foreach. if/else is one step further to "adding business logic to the template". switch is another step into this direction.

Usage

use Net\Bazzline\Component\Template\RuntimeContentBasedTemplate;

//create a instance
$template = new RuntimeContentBasedTemplate();

//set content
$template->setContent('there is no {one} without a {two}');

//assign variable one by one ...
$template->assignOne('one', 'foo');
//... or by providing an array
$template->assignMany(array('one' => 'foo'));
//you can also assign a template to a template
//  this is used if a layout template is defined with a {content} key for e.g.
$template->assignOne('content', $otherTemplate);

//you can render it in different ways
//1) explicit calling the method
echo $template->render();
//2) casting it to a string
echo (string) $template;
//3) using it as a function
//  you can also provide all optional parameters like in the constructor
echo $template();

Install

By Hand

mkdir -p vendor/net_bazzline/php_component_template
cd vendor/net_bazzline/php_component_template
git clone https://github.com/bazzline/php_component_template .

With Packagist

composer require net_bazzline/php_component_template:dev-master

API

API is available at bazzline.net.

History

  • upcomming
    • @todo
      • add examples
      • add download per months icon
      • add refuse/take/resign if needed and useful
      • add unit tests
      • implement caching
      • add support for different template extensions
      • refactor internals and replace array data with array of immutable domain objects
    • moved test autoloading into fitting configuration
    • moved zend-expressive-template adapter code into own repository
    • removed TryToInstallZendExpressiveTemplate command
    • removed zend-expressive-template as suggested package
    • restructured composer.json
  • 3.1.3 - released at 31.01.2016
    • added zend-expressive-template as suggested package
    • added TryToInstallZendExpressiveTemplate command that installs zend-expressive-template if requirements are met in development mode
    • added building for php 7
    • added test for stacking template into a template
    • moved to psr-4 auto loading
    • removed building for php 5.3.3
  • 3.1.2 - released at 26.01.2016
    • updated dependencies
  • 3.1.1 - released at 11.12.2015
    • refactored CallableComplexFileBasedTemplateManager::registerCallable()
    • updated dependencies
  • 3.1.0 - released at 28.10.2015
  • 3.0.0 - released at 09.10.2015
    • added links to travis, scrutinizer, openhub, versioneye
    • added the DelimiterInterface
    • implemented __invoke in all templates to speed up usage and rendering
    • implemented reset
    • refactored existing templates by introducing AbstractFileBasedTemplate and slicing out the delimiter handling
    • renamed setOpenDelimiter to setOpeningDelimiter
    • renamed FileTemplate to FileBasedTemplate
    • renamed StringTemplate to RuntimeContentBasedTemplate
    • renamed ViewTemplate to ComplexFileBasedTemplate
    • shifted order from filePath and variables in the constructor of RuntimeContentBasedTemplate and FileBasedContent
    • started link section
    • updated ComplexFileBasedTemplate variable handling by adding "EXTR_SKIP" to the extract method
    • updated dependency
  • 2.1.0 - released at 06.10.2015
    • added throws RuntimeException to __toString() method description
    • added ViewTemplate
    • add isAssigned
  • 2.0.0 - released at 02.10.2015
    • added TemplateInterface
    • decoupled file based template from generic template
      • renamed class Template to AbstractTemplate
      • introduced FileTemplate and StringTemplate
  • 1.1.0 - released at 01.10.2015
    • fixed major bug in "assignOne" (now it is working as expected)
  • 1.0.0 - released at 01.10.2015
    • initial release

Links to other libraries

Final Words

Star it if you like it :-). Add issues if you need it. Pull patches if you enjoy it. Write a blog entry if you use it :-D.

net_bazzline/php_component_template 适用场景与选型建议

net_bazzline/php_component_template 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 111 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 10 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: LGPLv3
  • 更新时间: 2015-10-01