sunnysideup / reflection-templates

提供对SilverStripe模板的反射,包括获取变量和块的API,类似于PHP的ReflectionClass。

安装: 199

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 3

分支: 4

类型:silverstripe模块

1.0.0 2019-11-24 23:49 UTC

This package is auto-updated.

Last update: 2024-08-25 10:35:50 UTC


README

一系列类,用于检查SilverStripe模板,获取关于变量和块元数据,类似于PHP的ReflectionClass。

安装

composer require unclecheese/reflection-templates:dev-master

要求

SilverStripe 3.1或更高版本

使用方法

给定一个模板,例如这个

<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>

我们可以使用ReflectionTemplate来检查它,如下所示

 $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";
 		}
 	 }
 }	 	
	 	

这将产生以下结果

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

上下文模板反射

您可以使用两个上下文相关的反射器之一来仅提取用户定义的变量和块。

  • SiteTreeReflectionTemplate包含有关所有SiteTreeContentController上下文中提供的方法和变量的上下文信息,并过滤掉如$Menu$SiteConfig等。
  • EmailReflectionTemplate的工作方式类似,过滤掉如ToSubject等对所有邮件都可用变量。

在这个世界上所有美好的事物,为什么要有这样的需求?

最近我发现我需要它,我有一堆从旧的SilverSmith项目留下的代码,决定最好不让它们闲置在角落里烂掉。希望有人能利用这疯狂的东西。

待办事项

添加一个任务,根据模板生成PHP类