2018-03-17 13:50:04 +01:00
|
|
|
<?php
|
|
|
|
class InvalidTemplateException extends UnexpectedValueException{
|
|
|
|
|
|
|
|
}
|
|
|
|
class TplBlock {
|
2018-03-19 19:13:19 +01:00
|
|
|
/*
|
|
|
|
* Class TplBlog
|
|
|
|
* By gnieark https://blog-du-grouik.tinad.fr 2018
|
|
|
|
* Licenced under the GNU General Public License V3
|
|
|
|
* https://www.gnu.org/licenses/gpl-3.0.fr.html
|
|
|
|
*/
|
2018-03-17 13:50:04 +01:00
|
|
|
const blockStartStart = '<!-- BEGIN ';
|
|
|
|
const blockStartEnd = ' -->';
|
|
|
|
const blockEndStart = '<!-- END ';
|
|
|
|
const blockEndEnd = ' -->';
|
|
|
|
|
|
|
|
const startEnclosure = '{{';
|
|
|
|
const endEnclosure = '}}';
|
|
|
|
|
|
|
|
public $name = '';
|
|
|
|
private $vars = array();
|
|
|
|
private $subBlocs = array();
|
2018-03-20 07:42:08 +01:00
|
|
|
private $unusedRegex = "";
|
2018-03-17 13:50:04 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialise TplBlock
|
|
|
|
* Input object name
|
2018-03-19 19:13:19 +01:00
|
|
|
* Can be empty only for the top one block
|
2018-03-17 13:50:04 +01:00
|
|
|
*/
|
|
|
|
|
2018-03-19 11:56:15 +01:00
|
|
|
public function __construct($name = NULL){
|
2018-03-17 13:50:04 +01:00
|
|
|
$this->name = $name;
|
2018-03-20 07:42:08 +01:00
|
|
|
|
2018-03-21 19:03:56 +01:00
|
|
|
if(!is_null($name) && !ctype_alnum($name)){
|
|
|
|
throw new InvalidTemplateException("Only alpha-numerics chars are allowed on the block name");
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-20 07:42:08 +01:00
|
|
|
$this->unusedRegex = '/'
|
|
|
|
. self::blockStartStart
|
2018-03-21 19:03:56 +01:00
|
|
|
. ' *([a-z][a-z0-9.]*) *'
|
2018-03-20 07:42:08 +01:00
|
|
|
. self::blockStartEnd
|
|
|
|
. '(.*?)'
|
|
|
|
. self::blockEndStart
|
|
|
|
. ' *\1 *'
|
|
|
|
. self::blockEndEnd
|
|
|
|
. '/is'
|
|
|
|
;
|
|
|
|
|
2018-03-17 13:50:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add simple vars
|
|
|
|
* Input array structured like:
|
|
|
|
* {"key":"value","key2":"value2"}
|
|
|
|
*/
|
|
|
|
public function add_vars(ARRAY $vars){
|
|
|
|
$this->vars = array_merge($this->vars,$vars);
|
|
|
|
}
|
|
|
|
|
2018-03-19 19:13:19 +01:00
|
|
|
/*
|
|
|
|
* add_sub_block
|
|
|
|
* Input: a TplBlock object.
|
|
|
|
*/
|
2018-03-17 13:50:04 +01:00
|
|
|
public function add_sub_block(TplBlock $bloc){
|
2018-03-19 11:56:15 +01:00
|
|
|
if(is_null($bloc->name) || empty($bloc->name)){
|
2018-03-17 13:50:04 +01:00
|
|
|
throw new InvalidTemplateException("A sub tpl bloc can't have an empty name");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->subBlocs[$bloc->name][] = $bloc;
|
|
|
|
}
|
|
|
|
|
2018-03-20 16:43:25 +01:00
|
|
|
private function subBlockRegex($prefix, $blocName,$trim = true) {
|
2018-03-20 07:42:08 +01:00
|
|
|
return '/'
|
|
|
|
. self::blockStartStart
|
|
|
|
. preg_quote($prefix . $blocName)
|
|
|
|
. self::blockStartEnd
|
2018-03-20 16:43:25 +01:00
|
|
|
. (($trim === false)? '' : '(?:\R|)?' )
|
2018-03-20 07:42:08 +01:00
|
|
|
. '(.*?)'
|
2018-03-20 16:43:25 +01:00
|
|
|
. (($trim === false)? '' : '(?:\R|)?' )
|
2018-03-20 07:42:08 +01:00
|
|
|
. self::blockEndStart
|
|
|
|
. preg_quote($prefix . $blocName)
|
|
|
|
. self::blockEndEnd
|
|
|
|
. '/is';
|
|
|
|
}
|
|
|
|
|
2018-03-17 13:50:04 +01:00
|
|
|
/*
|
2018-03-20 16:43:25 +01:00
|
|
|
* Shake the template string and input vars
|
|
|
|
* Then returns the parsed text
|
2018-03-19 19:13:19 +01:00
|
|
|
* Input:
|
|
|
|
* $str String containing the template to parse
|
|
|
|
* $subBlocsPath String optional, for this class internal use. The path like "bloc.subbloc"
|
|
|
|
* $trim Boolean
|
|
|
|
* if true(default), the potentials Carriages returns beginning
|
|
|
|
* and ending the bloc are deleted
|
2018-03-17 13:50:04 +01:00
|
|
|
*/
|
2018-03-19 19:13:19 +01:00
|
|
|
public function apply_tpl_str($str,$subBlocsPath = "", $trim = true){
|
2018-03-17 13:50:04 +01:00
|
|
|
|
|
|
|
//replace all simple vars
|
|
|
|
$prefix = (empty($subBlocsPath)? "" : $subBlocsPath.".");
|
|
|
|
foreach($this->vars as $key=>$value){
|
|
|
|
|
|
|
|
$str = str_replace(self::startEnclosure . $prefix . $key . self::endEnclosure,
|
|
|
|
$value,
|
|
|
|
$str);
|
|
|
|
}
|
|
|
|
|
2018-03-19 21:08:01 +01:00
|
|
|
//parse blocs
|
2018-03-17 13:50:04 +01:00
|
|
|
foreach($this->subBlocs as $blocName => $blocsArr){
|
|
|
|
$str = preg_replace_callback(
|
2018-03-20 16:43:25 +01:00
|
|
|
$this->subBlockRegex($prefix, $blocName, $trim),
|
2018-03-19 19:13:19 +01:00
|
|
|
function($m) use($blocName,$blocsArr,$prefix, $trim) {
|
2018-03-17 13:50:04 +01:00
|
|
|
$out = "";
|
|
|
|
foreach($blocsArr as $bloc){
|
2018-03-19 21:08:01 +01:00
|
|
|
//recursion
|
2018-03-19 19:13:19 +01:00
|
|
|
$out.=$bloc->apply_tpl_str( $m[1] , $prefix . $blocName , $trim );
|
2018-03-17 13:50:04 +01:00
|
|
|
}
|
|
|
|
return $out;
|
2018-03-20 07:42:08 +01:00
|
|
|
}
|
|
|
|
,$str
|
2018-03-17 13:50:04 +01:00
|
|
|
);
|
|
|
|
}
|
2018-03-19 19:13:19 +01:00
|
|
|
|
2018-03-20 07:42:08 +01:00
|
|
|
// Delete unused blocs
|
|
|
|
$str = preg_replace($this->unusedRegex, "", $str);
|
2018-03-20 16:43:25 +01:00
|
|
|
return $str;
|
|
|
|
|
2018-03-17 13:50:04 +01:00
|
|
|
}
|
|
|
|
|
2018-03-19 19:13:19 +01:00
|
|
|
/*
|
|
|
|
* load a file, and pass his content to apply_tpl_str function.
|
|
|
|
*/
|
|
|
|
public function apply_tpl_file($file, $trim = true){
|
2018-03-17 13:50:04 +01:00
|
|
|
if(!$tplStr = file_get_contents($file)){
|
|
|
|
throw new InvalidTemplateException("Cannot read given file ".$file);
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-19 19:13:19 +01:00
|
|
|
return $this->apply_tpl_str($tplStr, null, $trim);
|
2018-03-17 13:50:04 +01:00
|
|
|
}
|
|
|
|
}
|