Skip to content

Commit e3ff6b9

Browse files
Merge pull request #117 from utopia-php/feat-template-escape-html-by-default
feat: escape html by default in view params
2 parents ad6f7e6 + 5b62a82 commit e3ff6b9

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/View.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,16 @@ public function __construct(string $path = '')
7777
*
7878
* @throws Exception
7979
*/
80-
public function setParam(string $key, mixed $value): static
80+
public function setParam(string $key, mixed $value, bool $escapeHtml = true): static
8181
{
8282
if (\strpos($key, '.') !== false) {
8383
throw new Exception('$key can\'t contain a dot "." character');
8484
}
8585

86+
if (is_string($value) && $escapeHtml) {
87+
$value = \htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
88+
}
89+
8690
$this->params[$key] = $value;
8791

8892
return $this;

tests/ViewTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,10 @@ public function testCanFilterNewLinesToParagraphs()
8383
{
8484
$this->assertEquals('<p>line1</p><p>line2</p>', $this->view->print("line1\n\nline2", View::FILTER_NL2P));
8585
}
86+
87+
public function testCanSetParamWithEscapedHtml()
88+
{
89+
$this->view->setParam('key', '<html>value</html>');
90+
$this->assertEquals('&lt;html&gt;value&lt;/html&gt;', $this->view->getParam('key', 'default'));
91+
}
8692
}

0 commit comments

Comments
 (0)