class SSTemplateEngine implements TemplateEngine, Flushable (View source)

Parses template files with an *.ss file extension, or strings representing templates in that format.

In addition to a full template in the templates/ folder, a template in templates/Content or templates/Layout will be rendered into $Content and $Layout, respectively.

A single template can be parsed by multiple nested SSTemplateEngine instances through $Layout/$Content placeholders, as well as <% include MyTemplateFile %> template commands.

Caching

Compiled templates are cached, usually on the filesystem. If you put ?flush=1 on your URL, it will force the template to be recompiled.

Traits

A class that can be instantiated or replaced via DI

Provides extensions to this object to integrate it with standard config API methods.

Config options

global_key string

Default prepended cache key for partial caching

Properties

protected static array $topLevel

List of models being processed

Methods

public static 
create(mixed ...$args)

An implementation of the factory method, allows you to create an instance of a class

public static 
singleton(string $class = null)

Creates a class instance by the "singleton" design pattern.

public static 
config()

Get a configuration accessor for this class. Short hand for Config::inst()->get($this->class, .....).

public
mixed
uninherited(string $name)

Gets the uninherited value for the given config option

public
__construct(string|array $templateCandidates = [])

Instantiate a TemplateEngine

public static 
string
execute_template(array|string $template, ViewLayerData $data, array $overlay = [], ScopeManager|null $scope = null)

Execute the given template, passing it the given data.

public static 
flush()

Triggered early in the request when someone requests a flush.

public static 
void
flushTemplateCache(bool $force = false)

Clears all parsed template files in the cache folder.

public static 
void
flushCacheBlockCache(bool $force = false)

Clears all partial cache blocks.

public
bool
hasTemplate(string|array $templateCandidates)

Check if there is a template amongst the template candidates that this rendering engine can use.

public
string
renderString(string $template, ViewLayerData $model, array $overlay = [], bool $cache = true)

Render the template string.

public
string
render(ViewLayerData $model, array $overlay = [], ScopeManager|null $scope = null)

Render the template which was selected during instantiation or which was set via setTemplate().

public
setTemplate(string|array $templateCandidates)

Set the template which will be used in the call to render()

public
setParser(TemplateParser $parser)

Set the template parser that will be used in template generation

public
getParser()

Returns the parser that is set for template generation

public
setPartialCacheStore(CacheInterface $cache)

Set the cache object to use when storing / retrieving partial cache blocks.

public
CacheInterface
getPartialCacheStore()

Get the cache object to use when storing / retrieving partial cache blocks.

protected
string
includeGeneratedTemplate(string $cacheFile, ViewLayerData $model, array $overlay, array $underlay, ScopeManager|null $inheritedScope = null)

An internal utility function to set up variables in preparation for including a compiled template, then do the include

protected
array|null
getSubtemplateFor(string $subtemplate)

Get the appropriate template to use for the named sub-template, or null if none are appropriate

protected
string
parseTemplateContent(string $content, string $template = "")

Parse given template contents

Details

static Injectable create(mixed ...$args)

An implementation of the factory method, allows you to create an instance of a class

This method will defer class substitution to the Injector API, which can be customised via the Config API to declare substitution classes.

This can be called in one of two ways - either calling via the class directly, or calling on Object and passing the class name as the first parameter. The following are equivalent: $list = DataList::create(SiteTree::class); $list = SiteTree::get();

Parameters

mixed ...$args

Return Value

Injectable

static Injectable singleton(string $class = null)

Creates a class instance by the "singleton" design pattern.

It will always return the same instance for this class, which can be used for performance reasons and as a simple way to access instance methods which don't rely on instance data (e.g. the custom SilverStripe static handling).

Parameters

string $class

Optional classname to create, if the called class should not be used

Return Value

Injectable

The singleton instance

static Config_ForClass config()

Get a configuration accessor for this class. Short hand for Config::inst()->get($this->class, .....).

Return Value

Config_ForClass

mixed uninherited(string $name)

Gets the uninherited value for the given config option

Parameters

string $name

Return Value

mixed

__construct(string|array $templateCandidates = [])

Instantiate a TemplateEngine

Parameters

string|array $templateCandidates

A template or pool of candidate templates to choose from. The template engine will check the currently set themes from SSViewer for template files it can handle from the candidates.

static string execute_template(array|string $template, ViewLayerData $data, array $overlay = [], ScopeManager|null $scope = null)

Execute the given template, passing it the given data.

Used by the <% include %> template tag to process included templates.

Parameters

array|string $template
ViewLayerData $data
array $overlay

Associative array of fields (e.g. args into an include template) to inject into the template as properties. These override properties and methods with the same name from $data and from global template providers.

ScopeManager|null $scope

Return Value

string

static flush()

Triggered early in the request when someone requests a flush.

static void flushTemplateCache(bool $force = false)

Clears all parsed template files in the cache folder.

Parameters

bool $force

Set this to true to force a re-flush. If left to false, flushing will only be performed once a request.

Return Value

void

static void flushCacheBlockCache(bool $force = false)

Clears all partial cache blocks.

Parameters

bool $force

Set this to true to force a re-flush. If left to false, flushing will only be performed once a request.

Return Value

void

bool hasTemplate(string|array $templateCandidates)

Check if there is a template amongst the template candidates that this rendering engine can use.

Parameters

string|array $templateCandidates

Return Value

bool

string renderString(string $template, ViewLayerData $model, array $overlay = [], bool $cache = true)

Render the template string.

Doesn't include normalisation such as inserting js/css from Requirements API - that's handled by SSViewer.

Parameters

string $template
ViewLayerData $model

The model to get data from when rendering the template.

array $overlay

Associative array of fields (e.g. args into an include template) to inject into the template as properties. These override properties and methods with the same name from $data and from global template providers.

bool $cache

Return Value

string

string render(ViewLayerData $model, array $overlay = [], ScopeManager|null $scope = null)

Render the template which was selected during instantiation or which was set via setTemplate().

Doesn't include normalisation such as inserting js/css from Requirements API - that's handled by SSViewer.

Parameters

ViewLayerData $model

The model to get data from when rendering the template.

array $overlay

Associative array of fields (e.g. args into an include template) to inject into the template as properties. These override properties and methods with the same name from $data and from global template providers.

ScopeManager|null $scope

Return Value

string

Exceptions

MissingTemplateException

TemplateEngine setTemplate(string|array $templateCandidates)

Set the template which will be used in the call to render()

Parameters

string|array $templateCandidates

A template or pool of candidate templates to choose from. The template engine will check the currently set themes from SSViewer for template files it can handle from the candidates.

Return Value

TemplateEngine

SSTemplateEngine setParser(TemplateParser $parser)

Set the template parser that will be used in template generation

Parameters

TemplateParser $parser

Return Value

SSTemplateEngine

TemplateParser getParser()

Returns the parser that is set for template generation

Return Value

TemplateParser

SSTemplateEngine setPartialCacheStore(CacheInterface $cache)

Set the cache object to use when storing / retrieving partial cache blocks.

Parameters

CacheInterface $cache

Return Value

SSTemplateEngine

CacheInterface getPartialCacheStore()

Get the cache object to use when storing / retrieving partial cache blocks.

Return Value

CacheInterface

protected string includeGeneratedTemplate(string $cacheFile, ViewLayerData $model, array $overlay, array $underlay, ScopeManager|null $inheritedScope = null)

An internal utility function to set up variables in preparation for including a compiled template, then do the include

Parameters

string $cacheFile

The path to the file that contains the template compiled to PHP

ViewLayerData $model

The model to use as the root scope for the template

array $overlay

Any variables to layer on top of the scope

array $underlay

Any variables to layer underneath the scope

ScopeManager|null $inheritedScope

The current scope of a parent template including a sub-template

Return Value

string

protected array|null getSubtemplateFor(string $subtemplate)

Get the appropriate template to use for the named sub-template, or null if none are appropriate

Parameters

string $subtemplate

Return Value

array|null

protected string parseTemplateContent(string $content, string $template = "")

Parse given template contents

Parameters

string $content

The template contents

string $template

The template file name

Return Value

string