diff --git a/Mark.php b/Mark.php index 9c69bba..0bbf8be 100644 --- a/Mark.php +++ b/Mark.php @@ -149,7 +149,7 @@ static public function up($template, $context = [], $options = []) { } // Evaluating an array, which might be a block expression. - else if (is_array($ctx)) { + else if (is_array($ctx) || $child) { $result = self::_eval($ctx, $filters, $child); } @@ -406,6 +406,9 @@ private static function _pipes() { 'limit' => function($arr, $count, $idx = 0) { return array_slice($arr, $idx, $count); }, + 'split' => function($str, $separator = ',') { + return explode($separator, $str); + }, 'choose' => function($bool, $iffy, $elsy = false) { if (!!$bool) return $iffy; return $elsy ? $elsy : ''; diff --git a/tests.php b/tests.php index fa19ee9..bf09aae 100644 --- a/tests.php +++ b/tests.php @@ -31,6 +31,7 @@ 'motto' => "life is like a box of chocolates", 'obj' => [ 'truthy' => true, 'falsy' => false ], 'chars' => [ ["a","b","c"], ["d","e","f"] ], + 'splits' => "a,b,c and d", ]; function beforeEach() { @@ -391,6 +392,23 @@ public function getName() { // expect($result)->toEqual("Num: 123.0"); }); +it("resolves string split into array", function () use ($context) { + $template = "{{splits|split}}"; + $result = Mark::up($template, $context); + expect($result[0])->toEqual("a"); + expect($result[1])->toEqual("b"); + expect($result[2])->toEqual("c and d"); + + $template = "{{splits|split> and }}"; + $result = Mark::up($template, $context); + expect($result[0])->toEqual("a,b,c"); + expect($result[1])->toEqual("d"); + + $template = "{{splits|split|join> + }}"; + $result = Mark::up($template, $context); + expect($result)->toEqual("a + b + c and d"); +}); + it("resolves multiple pipes on simple array", function () use ($context) { $template = "brothers: {{brothers|sort|join> @ }}"; $result = Mark::up($template, $context);