Merge pull request #5 from gnieark/TplBlock-behaviour-#4
Tpl block behaviour #4
This commit is contained in:
commit
ce4bdaf4ee
41
TplBlock.php
41
TplBlock.php
|
@ -100,6 +100,13 @@ class TplBlock
|
|||
*/
|
||||
private $trim = true;
|
||||
|
||||
/**
|
||||
* Should we replace non set template vars by an empty string?
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
private $replaceNonGivenVars = true;
|
||||
|
||||
/**
|
||||
* Initialize TplBlock
|
||||
*
|
||||
|
@ -107,6 +114,7 @@ class TplBlock
|
|||
*
|
||||
* @param string $name The template name
|
||||
*/
|
||||
|
||||
public function __construct($name = "")
|
||||
{
|
||||
// Checks that name is valid.
|
||||
|
@ -145,7 +153,6 @@ class TplBlock
|
|||
public function addVars(array $vars)
|
||||
{
|
||||
$this->vars = array_merge($this->vars, $vars);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
@ -238,6 +245,11 @@ class TplBlock
|
|||
// Delete unused blocs.
|
||||
$str = preg_replace($this->unusedRegex, "", $str);
|
||||
|
||||
//Replace non setted vars by empty string
|
||||
if($this->replaceNonGivenVars) {
|
||||
$str = preg_replace( "/" .self::STARTENCLOSURE .'([a-z][a-z0-9.]*)' .self::ENDENCLOSURE ."/", '', $str );
|
||||
}
|
||||
|
||||
return $str;
|
||||
}
|
||||
|
||||
|
@ -265,7 +277,6 @@ class TplBlock
|
|||
public function doTrim()
|
||||
{
|
||||
$this->trim = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
@ -277,7 +288,31 @@ class TplBlock
|
|||
public function dontTrim()
|
||||
{
|
||||
$this->trim = false;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable the behaviour: Non given vars will be replaced by an empty string.
|
||||
*
|
||||
* @return TplBlock For chaining.
|
||||
*/
|
||||
public function doReplaceNonGivenVars()
|
||||
{
|
||||
$this->replaceNonGivenVars = true;
|
||||
return $this;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable the behaviour: Non given vars will be replaced by an empty string.
|
||||
*
|
||||
* @return TplBlock For chaining.
|
||||
*/
|
||||
public function dontReplaceNonGivenVars()
|
||||
{
|
||||
$this->replaceNonGivenVars = false;
|
||||
return $this;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
* @license GNU General Public License V3
|
||||
* @link https://github.com/gnieark/tplBlock/
|
||||
*/
|
||||
require_once "../vendor/autoload.php";
|
||||
require_once __DIR__."/../autoload.php";
|
||||
|
||||
use TplBlock\TplBlock;
|
||||
|
||||
|
|
|
@ -139,4 +139,22 @@ class TplBlockTest extends TestCase
|
|||
{
|
||||
new TplBlock("kjsd54.5");
|
||||
}
|
||||
|
||||
public function testIfRemoveNonGivenVarsWorks(){
|
||||
|
||||
$tpl = new TplBlock();
|
||||
$resultWithReplace = $tpl
|
||||
->doReplaceNonGivenVars()
|
||||
->applyTplStr("Hello {{name}}");
|
||||
|
||||
$resultWithoutReplace = $tpl
|
||||
->dontReplaceNonGivenVars()
|
||||
->applyTplStr("Hello {{name}}");
|
||||
|
||||
|
||||
$this->assertContains("name",$resultWithoutReplace);
|
||||
$this->assertFalse(strpos("name", $resultWithReplace));
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user