Add \Handlebars\SafeString class

This commit is contained in:
Dmitriy Simushev 2014-02-03 08:35:17 +00:00
parent 8650422483
commit 79373a193a
5 changed files with 120 additions and 47 deletions

View File

@ -0,0 +1,78 @@
<?php
/**
* This file is part of Handlebars-php
*
* PHP version 5.3
*
* @category Xamin
* @package Handlebars
* @author fzerorubigd <fzerorubigd@gmail.com>
* @author Behrooz Shabani <everplays@gmail.com>
* @author Dmitriy Simushev <simushevds@gmail.com>
* @copyright 2013 Authors
* @license MIT <http://opensource.org/licenses/MIT>
* @version GIT: $Id$
* @link http://xamin.ir
*/
namespace Handlebars;
/**
* Handlebars base string
*
* @category Xamin
* @package Handlebars
* @author fzerorubigd <fzerorubigd@gmail.com>
* @copyright 2013 Authors
* @license MIT <http://opensource.org/licenses/MIT>
* @version Release: @package_version@
* @link http://xamin.ir
*/
class BaseString
{
private $_string;
/**
* Create new string
*
* @param string $string input source
*/
public function __construct($string)
{
$this->_string = $string;
}
/**
* To String
*
* @return string
*/
public function __toString()
{
return $this->getString();
}
/**
* Get string
*
* @return string
*/
public function getString()
{
return $this->_string;
}
/**
* Create new string
*
* @param string $string input source
*
* @return void
*/
public function setString($string)
{
$this->_string = $string;
}
}

View File

@ -0,0 +1,33 @@
<?php
/**
* This file is part of Handlebars-php
*
* PHP version 5.3
*
* @category Xamin
* @package Handlebars
* @author Dmitriy Simushev <simushevds@gmail.com>
* @copyright 2014 Authors
* @license MIT <http://opensource.org/licenses/MIT>
* @version GIT: $Id$
* @link http://xamin.ir
*/
namespace Handlebars;
/**
* Handlebars safe string. Can be used in line helpers as wrapper for result to
* indicate that there is no need to escape the result.
*
* @category Xamin
* @package Handlebars
* @author Dmitriy Simushev <simushevds@gmail.com>
* @copyright 2014 Authors
* @license MIT <http://opensource.org/licenses/MIT>
* @version Release: @package_version@
* @link http://xamin.ir
*/
class SafeString extends BaseString
{
}

49
src/Handlebars/String.php Executable file → Normal file
View File

@ -8,6 +8,7 @@
* @package Handlebars
* @author fzerorubigd <fzerorubigd@gmail.com>
* @author Behrooz Shabani <everplays@gmail.com>
* @author Dmitriy Simushev <simushevds@gmail.com>
* @copyright 2013 Authors
* @license MIT <http://opensource.org/licenses/MIT>
* @version GIT: $Id$
@ -28,50 +29,6 @@ namespace Handlebars;
* @link http://xamin.ir
*/
class String
class String extends BaseString
{
private $_string;
/**
* Create new string
*
* @param string $string input source
*/
public function __construct($string)
{
$this->_string = $string;
}
/**
* To String
*
* @return string
*/
public function __toString()
{
return $this->getString();
}
/**
* Get string
*
* @return string
*/
public function getString()
{
return $this->_string;
}
/**
* Create new string
*
* @param string $string input source
*
* @return void
*/
public function setString($string)
{
$this->_string = $string;
}
}
}

View File

@ -408,7 +408,7 @@ class Template
$current[Tokenizer::ARGS] = implode(' ', $args);
$result = $this->_section($context, $current);
if ($escaped) {
if ($escaped && !($result instanceof SafeString)) {
$escape_args = $this->handlebars->getEscapeArgs();
array_unshift($escape_args, $result);
$result = call_user_func_array(

View File

@ -246,6 +246,11 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
});
$this->assertEquals('<strong>Test</strong>', $engine->render('{{{markupHelper}}}', array()));
$this->assertEquals('&lt;strong&gt;Test&lt;/strong&gt;', $engine->render('{{markupHelper}}', array()));
$engine->addHelper('safeStringTest', function() {
return new \Handlebars\SafeString('<strong>Test</strong>');
});
$this->assertEquals('<strong>Test</strong>', $engine->render('{{safeStringTest}}', array()));
}
/**