Stud.IP  6.0
Factory Class Reference

Public Member Functions

array __construct (protected string $path)
 
 set_path (string $path)
 
 get_path ()
 
 open (Template|string $template)
 
 get_template_file (string $template0)
 
 get_template_handler (string $template)
 
 add_handler (string $extension, string $class, array $options=[])
 
 get_absolute_path (string $template)
 
 find_template (string $template)
 
 get_extension (string $file)
 
 render (Template|string $template, array $attributes=[], Template|string|null $layout=null)
 

Protected Attributes

array $handlers
 

Constructor & Destructor Documentation

◆ __construct()

array __construct ( protected string  $path)

Constructor of TemplateFactory.

Parameters
string$paththe template include path

Member Function Documentation

◆ add_handler()

add_handler ( string  $extension,
string  $class,
array  $options = [] 
)

Registers a handler for templates with a matching extension.

Parameters
string$extensionthe extension of the templates to handle
class-string<Template>$class the name of the already loaded class
array$optionsoptional; an array of options which is used when constructing a new instance

◆ find_template()

find_template ( string  $template)

Find template given w/o extension.

Parameters
string$templatethe template's filename w/o extension
Returns
string|null null if there no such file could be found, a string containing the complete file name otherwise

◆ get_absolute_path()

get_absolute_path ( string  $template)

Returns the absolute path to the template. If the given argument starts with a slash or with a protocoll, this method just returns its arguments.

Parameters
string$templatean incomplete template name
Returns
string an absolute path to the incomplete template name

◆ get_extension()

get_extension ( string  $file)

Returns the file extension if there is one.

Parameters
string$filean possibly incomplete template file name
Returns
string|null a string containing the file extension if there is one, NULL otherwise

◆ get_path()

get_path ( )

Returns the include path of the factory

Returns
string the current include path

◆ get_template_file()

get_template_file ( string  $template0)

This method returns the absolute filename of the template

Parameters
string$template0a template string
Returns
string an absolute filename
Exceptions
TemplateNotFoundExceptionif the template could not be found

◆ get_template_handler()

get_template_handler ( string  $template)

Matches an extension to a template handler.

Parameters
string$templatethe template
Returns
array|null an array containing the class name and an array of options of the matched extension; or NULL if the extension did not match

◆ open()

open ( Template|string  $template)

Open a template of the given name using the factory method pattern. If a string was given, the path of the factory is searched for a matching template. If this string starts with a slash or with /+:\/\//, the string is interpreted as an absolute path. Otherwise the path of the factory will be prepended. After that the factory searches for a file extension in this string. If there is none, the directory where the template is supposed to live is searched for a file starting with the template string and a supported file extension. At last the factory instantiates a template object of the matching template class.

Examples:

$factory->open('/path/to/template') does not prepend the factory's path but searches for "template.*" in "/path/to"

$factory->open('template') prepends the factory's path and searches there for "template.*"

$factory->open('template.php') prepends the factory's path but does not search and instantiates a PHPTemplate instead

This method returns it's parameter, if it is not a string. This functionality is useful for helper methods like render_partial

Parameters
Template | string$templateA name of a template.
Returns
Template the factored object
Exceptions
TemplateNotFoundExceptionif the template could not be found

◆ render()

render ( Template|string  $template,
array  $attributes = [],
Template|string|null  $layout = null 
)

Class method to parse, render and return the presentation of a template.

Parameters
Template | string$templateA name of a template or a template
array$attributesAn associative array of attributes and their associated values.
Template | string | null$layoutA name of a layout template or a template.
Returns
string A string representing the rendered presentation.
Exceptions
TemplateNotFoundException

◆ set_path()

set_path ( string  $path)

Sets a new include path for the factory and returns the old one.

Parameters
string$paththe new path
Returns
string the old path

Field Documentation

◆ $handlers

array $handlers
protected
Initial value:
= [
'php' => [PhpTemplate::class, []]

mapping of file extensions to supported template classes


The documentation for this class was generated from the following file: