Skip to content

Commit

Permalink
Merge pull request #4 from luyadev/delimiter
Browse files Browse the repository at this point in the history
delimiter
  • Loading branch information
nadar committed Jun 15, 2021
2 parents 6f54fc6 + b5304a9 commit f481844
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/).

## 1.2.0 (15. June 2021)

+ [#4](https://github.com/luyadev/yii-helpers/pull/4) Added option to define the delimiter in `StringHelper::template` function.

## 1.1.1 (6. April 2021)

+ [#2](https://github.com/luyadev/yii-helpers/issues/2) Fix issue where the highlight text has been highlight to.
Expand Down
8 changes: 5 additions & 3 deletions src/helpers/StringHelper.php
Expand Up @@ -432,12 +432,14 @@ public static function isNummeric($value, $strict = true)
*
* @param string $template The template to parse. The template may contain double curly brackets variables.
* @param array $variables The variables which should be available in the template.
* @param boolean $removeEmpty Whether variables in double curly brackets should be removed event the have not be assigned by $variables array.
* @param boolean $removeEmpty Whether variables in double curly brackets should be removed, even the have not be assigned by $variables array.
* @param string $leftDelimiter The delimiter for the variable on the left, default is `{{` {@since 1.2.0}
* @param string $rightDelimiter The delimiter for the variable on the right, default is `}}` {@since 1.2.0}
* @return string
*/
public static function template($template, array $variables = [], $removeEmpty = false)
public static function template($template, array $variables = [], $removeEmpty = false, $leftDelimiter = '{{', $rightDelimiter = '}}')
{
preg_match_all("/{{(.*?)}}/", $template, $matches, PREG_SET_ORDER);
preg_match_all("/$leftDelimiter(.*?)$rightDelimiter/", $template, $matches, PREG_SET_ORDER);

if (empty($matches)) {
return $template;
Expand Down
7 changes: 7 additions & 0 deletions tests/helpers/StringHelperTest.php
Expand Up @@ -253,6 +253,13 @@ public function testTemplate()
$this->assertSame('<p>bar </p>', StringHelper::template('<p>{{ foo }} {{unknown}}</p>', ['foo' => 'bar', 'xyz' => 'abc'], true));
}

public function testTemplateDelimiterChange()
{
$this->assertSame('<p>bar</p>', StringHelper::template('<p>bar</p>'));
$this->assertSame('<p>bar</p>', StringHelper::template('<p>{foo}</p>', ['foo' => 'bar'], true, '{', '}'));
$this->assertSame('<p>bar</p>', StringHelper::template('<p>{ foo }</p>', ['foo' => 'bar'], true, '{', '}'));
}

public function testTextList()
{
$this->assertSame(['foo', 'bar'], StringHelper::textList('foo ,bar'));
Expand Down

0 comments on commit f481844

Please sign in to comment.