PHPackages                             unclecheese/reflection-templates - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. unclecheese/reflection-templates

ActiveSilverstripe-module[Templating &amp; Views](/categories/templating)

unclecheese/reflection-templates
================================

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

512.6k3[4 PRs](https://github.com/unclecheese/silverstripe-reflection-templates/pulls)2PHP

Since Oct 18Pushed 6y ago1 watchersCompare

[ Source](https://github.com/unclecheese/silverstripe-reflection-templates)[ Packagist](https://packagist.org/packages/unclecheese/reflection-templates)[ RSS](/packages/unclecheese-reflection-templates/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (2)

Reflection Templates for SilverStripe
=====================================

[](#reflection-templates-for-silverstripe)

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

Installation
------------

[](#installation)

`composer require unclecheese/reflection-templates:dev-master`

Requirements
------------

[](#requirements)

SilverStripe 3.1 or higher

Usage
-----

[](#usage)

Given a template such as this:

```

  $Headline
  $Image.CroppedImage(200,200)
  $Category.Title
  it's featured

    $Title ($Date.Nice)

      This article, called $ArticleTitle is related to $Up.Title published

    $Description

```

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

[](#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?!
--------------------------------------------------

[](#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](http://github.com/unclecheese/SilverSmith) project, and decided its best not left to sit and rot into oblivion. Hopefully someone else can make use of this insanity.

Todo
----

[](#todo)

Add a task that will generate PHP classes given a template

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/654636?v=4)[Aaron Carlino](/maintainers/unclecheese)[@unclecheese](https://github.com/unclecheese)

### Embed Badge

![Health badge](/badges/unclecheese-reflection-templates/health.svg)

```
[![Health](https://phpackages.com/badges/unclecheese-reflection-templates/health.svg)](https://phpackages.com/packages/unclecheese-reflection-templates)
```

###  Alternatives

[mustache/mustache

A Mustache implementation in PHP.

3.3k44.6M291](/packages/mustache-mustache)[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9682.1M97](/packages/roots-acorn)[whitecube/nova-flexible-content

Flexible Content &amp; Repeater Fields for Laravel Nova.

8053.0M25](/packages/whitecube-nova-flexible-content)[mopa/bootstrap-bundle

Easy integration of twitters bootstrap into symfony2

7042.9M33](/packages/mopa-bootstrap-bundle)[limenius/react-bundle

Client and Server-side react rendering in a Symfony Bundle

3871.2M](/packages/limenius-react-bundle)[nicmart/string-template

StringTemplate is a very simple string template engine for php. I've written it to have a thing like sprintf, but with named and nested substutions.

2101.7M30](/packages/nicmart-string-template)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
