From 680c6e01f6086ca33bbccd65aafa83d25d1e7f4b Mon Sep 17 00:00:00 2001 From: William ANGER Date: Wed, 27 Mar 2013 02:03:01 +0100 Subject: [PATCH 1/3] implemented missing 'split' pipe --- Mark.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Mark.php b/Mark.php index 9c69bba..14da4b1 100644 --- a/Mark.php +++ b/Mark.php @@ -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 : ''; From 85ad486733488c4918e2bad34dc8354e287d6c73 Mon Sep 17 00:00:00 2001 From: William ANGER Date: Wed, 27 Mar 2013 06:45:09 +0100 Subject: [PATCH 2/3] added unit testing for 'split' pipe --- tests.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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); From 887ceffd50fcf8990853625efeedee053d7eef4a Mon Sep 17 00:00:00 2001 From: William ANGER Date: Wed, 27 Mar 2013 06:48:18 +0100 Subject: [PATCH 3/3] fixed missing case that prevented pipe-returned arrays from being looped on like described in README example "{{names|split:;}} {{.}} {{/names}}" see : Markup.js README ('split' pipe documentation) --- Mark.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Mark.php b/Mark.php index 14da4b1..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); }