Skip to content

Commit

Permalink
added view & escape functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
xinral committed Feb 12, 2024
1 parent 70420d2 commit 9ab7024
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
6 changes: 6 additions & 0 deletions examples/components/header.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1><?php echo $title; ?></h1>
<p><?php echo $desc; ?></p>

<?php
echo $view->escape($htmlval);
?>
4 changes: 3 additions & 1 deletion examples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@
$template = new TemplateEngine($config);

echo $template->render('header', [
'title' => 'my title',
'title' => 'test',
'desc' => 'lorem ipsum dolor sit amet',
'htmlval' => '<script>alert(1);</script>'
]);
13 changes: 9 additions & 4 deletions src/TemplateEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public function render(string $slug, array $args = []) {
$slug = $slug.'.php';
$paths = $this->config;

$view = new View();
array_push($args, $view);

$paths = array_map(function($path) use ($slug) {
return $path . '/' . $slug;
}, $paths);
Expand All @@ -33,14 +36,16 @@ public function render(string $slug, array $args = []) {
}

extract($args, EXTR_SKIP);
foreach($args as $value) {
$value = htmlspecialchars($value);
return $value;
}

ob_start();
include($file);
$output = ob_get_clean();
return $output;
}
}

class View extends TemplateEngine {
public function escape(string $val) {
return htmlentities($val);
}
};

0 comments on commit 9ab7024

Please sign in to comment.