Grima 2018-08
Whispering into Alma's ear
Public Member Functions | Protected Member Functions | Private Attributes | List of all members
zemowsplat\Splat Class Reference

Simple template engine. More...

Public Member Functions

  __construct ()
  Setup default scopes and paths, but leave base dirs empty. More...
 
  splat ( $name, $scope=array())
  Main function: execute template $name with given $scope More...
 
  addPaths (array $paths)
  Add a resolution of $name to $relativePath More...
 
  addBases ( $bases)
  Add a base directory to resolve $relativePath to $absolutePath More...
 

Protected Member Functions

  push (array $scope)
  push a scope onto the stack More...
 
  peek ()
  peek at current scope More...
 
  pop ()
  pop a scope off the stack More...
 
  nameToPaths ($name)
  Resolve a template $name to an array of absolute $paths. More...
 

Private Attributes

  $paths
 
  $bases
 
  $stack
 

Detailed Description

Simple template engine.

Definition at line 12 of file grima-splats.php.

Constructor & Destructor Documentation

◆ __construct()

zemowsplat\Splat::__construct ( )

Setup default scopes and paths, but leave base dirs empty.

Definition at line 45 of file grima-splats.php.

45 {
46 $this->stack = [ [
47 'e' => 'htmlspecialchars',
48 't' => [ $this, 'splat' ],
49 ] ];
50 $this->paths = [];
51 $this->bases = [];
52 }

Member Function Documentation

◆ addBases()

zemowsplat\Splat::addBases (   $bases )

Add a base directory to resolve $relativePath to $absolutePath

Parameters
string | array $bases - beginnings of absolute paths to search for relative paths

Definition at line 111 of file grima-splats.php.

111 {
112 foreach( (array) $bases as $base ) {
113 $this->bases[] = $base;
114 }
115 }

◆ addPaths()

zemowsplat\Splat::addPaths ( array  $paths )

Add a resolution of $name to $relativePath

Parameters
array $paths - associative array of $name => $path pairs to setup path resolution

Definition at line 101 of file grima-splats.php.

101 {
102 foreach( $paths as $name => $path ) {
103 $this->paths[$name][] = $path;
104 }
105 }

◆ nameToPaths()

zemowsplat\Splat::nameToPaths (   $name )
protected

Resolve a template $name to an array of absolute $paths.

In case of additions, the array may be long, but is usually a single path to be included in order to print the template.

Parameters
string $name - name of the template to lookup
Returns
array $paths - absolute paths that should be included in order to print the template

Definition at line 125 of file grima-splats.php.

125 {
126 $ret = array();
127 @$onlyOne = array_merge((array) $this->paths[$name], [ "$name.php", ]);
128 foreach( $onlyOne as $file ) {
129 foreach( $this->bases as $base ) {
130 $path = $base . DIRECTORY_SEPARATOR . $file;
131 if( is_readable($path) ) {
132 $ret[] = $path;
133 break 2;
134 }
135 }
136 }
137 @$adds = array_merge((array) $this->paths[$name.'[]'], [ "$name-add.php", ]);
138 foreach( $adds as $file ) {
139 foreach( $this->bases as $base ) {
140 $path = $base . DIRECTORY_SEPARATOR . $file;
141 if( is_readable($path) ) {
142 $ret[] = $path;
143 }
144 }
145 }
146 return $ret;
147 }

◆ peek()

zemowsplat\Splat::peek ( )
protected

peek at current scope

Returns
array $scope - the current scope

Definition at line 29 of file grima-splats.php.

29 {
30 $length = sizeof($this->stack);
31 return $length ? $this->stack[ $length - 1 ] : array();
32 }

◆ pop()

zemowsplat\Splat::pop ( )
protected

pop a scope off the stack

Returns
array $scope - the former scope

Definition at line 38 of file grima-splats.php.

38 {
39 return array_pop($this->stack);
40 }

◆ push()

zemowsplat\Splat::push ( array  $scope )
protected

push a scope onto the stack

Parameters
array $scope - the new scope to be merged with old scope

Definition at line 21 of file grima-splats.php.

21 {
22 $this->stack[] = array_merge((array) $this->peek(), (array) $scope);
23 }
peek()
peek at current scope
Definition: grima-splats.php:29

◆ splat()

zemowsplat\Splat::splat (   $name,
  $scope = array() 
)

Main function: execute template $name with given $scope

Default variables

There are two convenience variables:

  • $e = htmlspecialchars - suitable for
    <div class="<?=$e($className)?>"><?=$e($textContent)?></div>
  • $t = $this->splat - suitable for calling more templates.

Suggested template contents

Technically any PHP code is valid in the template, but it is intended to be used on text/html heavy templates with some:

<?=$e($var)?>

and some

<?php if(isset($var)): ?> ... <?php else: ?> ... <?php endif ?>

and some

<?php foreach($var as $key=>$val): ?> ... <?php endforeach ?>

with some

<?=$t('subtemplate',['var'=>$val])?>

and that's basically it.

Parameters
string $name - name of the template to execute
array $scope - additional variables in scope of the template

Definition at line 87 of file grima-splats.php.

87 {
88 $this->push($scope);
89 $scope = $this->peek();
90 foreach( $this->nameToPaths($name) as $file ) {
91 $exe = new SplatExecutionContext($file, $scope);
92 $exe->splat();
93 }
94 $this->pop();
95 }
nameToPaths($name)
Resolve a template $name to an array of absolute $paths.
Definition: grima-splats.php:125
pop()
pop a scope off the stack
Definition: grima-splats.php:38
push(array $scope)
push a scope onto the stack
Definition: grima-splats.php:21

Member Data Documentation

◆ $bases

zemowsplat\Splat::$bases
private

Definition at line 14 of file grima-splats.php.

◆ $paths

zemowsplat\Splat::$paths
private

Definition at line 13 of file grima-splats.php.

◆ $stack

zemowsplat\Splat::$stack
private

Definition at line 16 of file grima-splats.php.


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