commit 740532a4a7ca46eff3d43ac324caaf39f9bf074e Author: Gnieark Date: Sat Mar 17 13:50:04 2018 +0100 first commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..6b32696 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# tplBlock diff --git a/class.TplBlock.php b/class.TplBlock.php new file mode 100644 index 0000000..0afdeda --- /dev/null +++ b/class.TplBlock.php @@ -0,0 +1,86 @@ +'; + const blockEndStart = ''; + + const startEnclosure = '{{'; + const endEnclosure = '}}'; + + public $name = ''; + private $vars = array(); + private $subBlocs = array(); + + /* + * Initialise TplBlock + * Input object name + */ + + public function __construct(STRING $name =""){ + $this->name = $name; + } + + /* + * Add simple vars + * Input array structured like: + * {"key":"value","key2":"value2"} + */ + public function add_vars(ARRAY $vars){ + $this->vars = array_merge($this->vars,$vars); + } + + public function add_sub_block(TplBlock $bloc){ + if(empty($bloc->name)){ + throw new InvalidTemplateException("A sub tpl bloc can't have an empty name"); + return false; + } + $this->subBlocs[$bloc->name][] = $bloc; + } + + /* + * Shake template and input vars and returns the text + */ + public function apply_tpl_str($str,$subBlocsPath = ""){ + + //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); + } + + //Isolate blocs + + foreach($this->subBlocs as $blocName => $blocsArr){ + $str = preg_replace_callback( + '/' . self::blockStartStart . preg_quote($blocName) . self::blockStartEnd . + '(.*?)'. + self::blockEndStart . preg_quote($blocName). self::blockEndEnd. + '/is', + function($m) use($blocName,$blocsArr,$prefix) { + $out = ""; + foreach($blocsArr as $bloc){ + $out.=$bloc->apply_tpl_str( $m[1] , $prefix . $blocName ); + } + return $out; + } + ,$str + ); + } + return $str; + } + + public function apply_tpl_file($file){ + if(!$tplStr = file_get_contents($file)){ + throw new InvalidTemplateException("Cannot read given file ".$file); + return false; + } + return $this->apply_tpl_str($tplStr); + } +} diff --git a/test-TplBlock/sample.txt b/test-TplBlock/sample.txt new file mode 100644 index 0000000..a6346a1 --- /dev/null +++ b/test-TplBlock/sample.txt @@ -0,0 +1,15 @@ +hello {{title}} {{name}} {{firstname}} + +The first primes numbers are: + + - {{primes.number}} + + +Lets list all divisors of: + + +{{number.value}} Have those divisors: + + - {{number.divisor.value}} + + \ No newline at end of file diff --git a/test-TplBlock/test.php b/test-TplBlock/test.php new file mode 100644 index 0000000..16c63e3 --- /dev/null +++ b/test-TplBlock/test.php @@ -0,0 +1,26 @@ +add_vars(array( + "name" => "Gnieark", + "title" => "Monsieur", + "firstname" => "Grouik" + ) + ); + +$primes = array(1,2,3,5,7,11); + +foreach($primes as $prime){ + $tplPrime = new TplBlock('primes'); + $tplPrime->add_vars(array('number' => $prime)); + $tpl->add_sub_block($tplPrime); + + +} + + + + +echo $tpl->apply_tpl_file("sample.txt"); \ No newline at end of file