Skip to content

Commit 8000aed

Browse files
authored
add support for filter (#201)
1 parent d8fd54a commit 8000aed

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/Liquid/StandardFilters.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,19 @@ public static function raw($input)
150150
}
151151

152152

153+
/**
154+
* Converts into JSON string
155+
*
156+
* @param mixed $input
157+
*
158+
* @return string
159+
*/
160+
public static function json($input)
161+
{
162+
return json_encode($input);
163+
}
164+
165+
153166
/**
154167
* Escape a string
155168
*

tests/Liquid/StandardFiltersTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,36 @@ public function testRaw()
174174
}
175175
}
176176

177+
public function testJson()
178+
{
179+
$data = array(
180+
array(
181+
"before" => "Anything",
182+
"after" => "\"Anything\"",
183+
),
184+
array(
185+
"before" => 3,
186+
"after" => 3,
187+
),
188+
array(
189+
"before" => array(1, 2, 3),
190+
"after" => "[1,2,3]",
191+
),
192+
array(
193+
"before" => array("one" => 1, "two" => 2, "three" => 3),
194+
"after" => "{\"one\":1,\"two\":2,\"three\":3}",
195+
),
196+
array(
197+
"before" => array("one" => 1, "two" => array(1, 2, 3), "three" => 3),
198+
"after" => "{\"one\":1,\"two\":[1,2,3],\"three\":3}",
199+
),
200+
);
201+
202+
foreach ($data as $testCase) {
203+
$this->assertEquals($testCase['after'], StandardFilters::json($testCase['before']));
204+
}
205+
}
206+
177207
public function testEscape()
178208
{
179209
$data = array(

0 commit comments

Comments
 (0)