Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Mark.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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 : '';
Expand Down
18 changes: 18 additions & 0 deletions tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand Down