Skip to content

Commit 9ab7024

Browse files
committed
added view & escape functionality
1 parent 70420d2 commit 9ab7024

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

examples/components/header.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<h1><?php echo $title; ?></h1>
2+
<p><?php echo $desc; ?></p>
3+
4+
<?php
5+
echo $view->escape($htmlval);
6+
?>

examples/index.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@
1111
$template = new TemplateEngine($config);
1212

1313
echo $template->render('header', [
14-
'title' => 'my title',
14+
'title' => 'test',
15+
'desc' => 'lorem ipsum dolor sit amet',
16+
'htmlval' => '<script>alert(1);</script>'
1517
]);

src/TemplateEngine.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public function render(string $slug, array $args = []) {
1313
$slug = $slug.'.php';
1414
$paths = $this->config;
1515

16+
$view = new View();
17+
array_push($args, $view);
18+
1619
$paths = array_map(function($path) use ($slug) {
1720
return $path . '/' . $slug;
1821
}, $paths);
@@ -33,14 +36,16 @@ public function render(string $slug, array $args = []) {
3336
}
3437

3538
extract($args, EXTR_SKIP);
36-
foreach($args as $value) {
37-
$value = htmlspecialchars($value);
38-
return $value;
39-
}
4039

4140
ob_start();
4241
include($file);
4342
$output = ob_get_clean();
4443
return $output;
4544
}
45+
}
46+
47+
class View extends TemplateEngine {
48+
public function escape(string $val) {
49+
return htmlentities($val);
50+
}
4651
};

0 commit comments

Comments
 (0)