unclecheese/reflection-templates 问题修复 & 功能扩展

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

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

unclecheese/reflection-templates

Composer 安装命令:

composer require unclecheese/reflection-templates

包简介

Provides introspection of SilverStripe templates with an API for getting variables and blocks, much like the PHP ReflectionClass.

README 文档

README

A set of classes that introspect SilverStripe templates, getting metadata about variables and blocks, much like PHP's ReflectionClass.

Installation

composer require unclecheese/reflection-templates:dev-master

Requirements

SilverStripe 3.1 or higher

Usage

Given a template such as this:

<div>
  <h2>$Headline</h2>
  <div>$Image.CroppedImage(200,200)</div>
  <h3>$Category.Title</h3>
  <% if $Featured %>it's featured<% end_if %>
  <ul>
  <% loop $Items %>
    <li>$Title ($Date.Nice)</li>
    <% if $Articles %>
    <ul>
      <% loop $Articles %>
      <li>This article, called $ArticleTitle is related to $Up.Title <% if $Published %>published<% end _if %></li>
      <% end_loop %>
     </ul>
    <% end_if %>
  <% end_loop %>
  
  <% with $FeaturedProduct %>
    <h3>$Description</h3>
  <% end_with %>
</div>

We can introspect it using a ReflectionTemplate like so:

 $reflector = ReflectionTemplate::create();
 $reflector->process(file_get_contents('/path/to/template.ss'));
  
 foreach($reflector->getTopLevelVars() as $varName => $type) {
 	echo "The template variable $varName is likely a $type\n";
 }
 
 foreach($reflector->getTopLevelBlocks() as $block) {
 	 echo "There is a block at the top level named {$block->getName()}\n";
 	 echo $block->isLoop() ? "\tThis block is a loop\n" : "\tThis block is a with\n";
 	 foreach($block->getVars() as $var => $type) {
 		echo "\tThe top level block {$block->getName()} contains a variable named $var that is likely a $type\n";
 	 }
 	 foreach($block->getChildren() as $child) {
 		echo "\tThere is a child block named {$child->getName()}. It has the following vars:\n";
 		foreach($child->getVars() as $v => $t) {
 			echo "\t\tThe nested block {$child->getName()} contains a variable named $v that is likely a $t\n";
 		}
 	 }
 }	 	
	 	

This produces the following result:

The template variable Headline is likely a Text
The template variable Image is likely a Image
The template variable Category is likely a has_one
The template variable Featured is likely a Boolean
There is a block at the top level named Items
	This block is a loop
	The top level block Items contains a variable named Title that is likely a Text
	The top level block Items contains a variable named Date that is likely a Date
	There is a child block named Articles. It has the following vars:
		The nested block Articles contains a variable named ArticleTitle that is likely a Text
		The nested block Articles contains a variable named Published that is likely a Boolean
There is a block at the top level named FeaturedProduct
	This block is a with
	The top level block FeaturedProduct contains a variable named Description that is likely a Text

Contextual template reflection

You can use one of the two context-sensitive reflectors to surface only variables and blocks that are user-defined.

  • SiteTreeReflectionTemplate comes loaded with context about methods and variables that are made available to all SiteTree and ContentController contexts, and filters out things like $Menu, $SiteConfig, etc.
  • EmailReflectionTemplate works similarly, filtering out variables like To, Subject, etc., that are made available to all emails.

For all that is good and holy in this world, WHY?!

I found myself in need of it recently, and I had all this code kicking around from the old SilverSmith project, and decided its best not left to sit and rot into oblivion. Hopefully someone else can make use of this insanity.

Todo

Add a task that will generate PHP classes given a template

unclecheese/reflection-templates 适用场景与选型建议

unclecheese/reflection-templates 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.56k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2014 年 12 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 unclecheese/reflection-templates 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2014-12-01