(not tested, have to go) Delete non setted vars

master
gnieark 6 years ago
parent 71c97e6d91
commit 0ba4f6eafe

@ -100,6 +100,13 @@ class TplBlock
*/ */
private $trim = true; private $trim = true;
/**
* Should we replace non set template vars by an empty string?
*
* @var boolean
*/
private $replaceNonGivenVars = true;
/** /**
* Initialize TplBlock * Initialize TplBlock
* *
@ -107,6 +114,7 @@ class TplBlock
* *
* @param string $name The template name * @param string $name The template name
*/ */
public function __construct($name = "") public function __construct($name = "")
{ {
// Checks that name is valid. // Checks that name is valid.
@ -145,7 +153,6 @@ class TplBlock
public function addVars(array $vars) public function addVars(array $vars)
{ {
$this->vars = array_merge($this->vars, $vars); $this->vars = array_merge($this->vars, $vars);
return $this; return $this;
} }
@ -237,6 +244,11 @@ class TplBlock
// Delete unused blocs. // Delete unused blocs.
$str = preg_replace($this->unusedRegex, "", $str); $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; return $str;
} }
@ -265,7 +277,6 @@ class TplBlock
public function doTrim() public function doTrim()
{ {
$this->trim = true; $this->trim = true;
return $this; return $this;
} }
@ -277,7 +288,31 @@ class TplBlock
public function dontTrim() public function dontTrim()
{ {
$this->trim = false; $this->trim = false;
return $this; 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 * @license GNU General Public License V3
* @link https://github.com/gnieark/tplBlock/ * @link https://github.com/gnieark/tplBlock/
*/ */
require_once "../vendor/autoload.php"; require_once __DIR__."/../autoload.php";
use TplBlock\TplBlock; use TplBlock\TplBlock;

@ -139,4 +139,6 @@ class TplBlockTest extends TestCase
{ {
new TplBlock("kjsd54.5"); new TplBlock("kjsd54.5");
} }
} }

Loading…
Cancel
Save