-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Description
Even in it's current version, \rnb\template\get() has made things (code reuse, creating truly dynamic templates) a lot easier. But it's a bit hacky and rough on the edges. I don't really want to do this:
<?php
// templates/template-name.php
$data = $data ?? false; // potential source of wtfs
if ($data) { ?>
<div class="bg--<?=$data['background']?>">
<h1><?=$data['title']?></h1>
</div>
<?php } else {
// Can't actually return false to stop execution and die() is a bit too much
// => everything has to be wrapped in if-blocks
trigger_error('You must provide template with data.', E_USER_WARNING);
}
// some-file.php
\rnb\template\get('templates/template-name.php', [
'data' => [
'title' => 'Hello world!',
'background' => 'hotpink'
]
]);
when I could do this:
<?php
// templates/template-name.php
namespace themename;
function template_name($title = 'Default', $background = 'white', $any, $number, $of, $params) {
// Complex templates could utilize classes if required
if (!$data) { trigger_error('You must provide template with data.', E_USER_WARNING); } ?>
<div class="bg--<?=$background?>">
<h1><?=$title?></h1>
</div>
<?php
}
// some-file.php
\rnb\template\get('template_name', [
// One could ask why not pass params to function directly? template_name($title, $background);
// Because PHP sucks. You can't *really* have multiple default arguments. Using an array lets
// us work around that.
'title' => 'Hello world!',
'background' => 'hotpink'
]);
// internally all files from templates/ have been loaded (or not, if it's expensive)
// same variable creating loop
// and finally something like
// call_user_func([THEME_NAMESPACE."\$templatefn_name"], $title, $background);
I am not sure how PHP works internally when requiring a file multiple times, but if they are read from disk every time, and even if it caches them in memory, this should still be almost as fast (small function overhead) while being a lot cleaner.
Did I mention this would also allow template calls in actions? \rnb\template\add_as_action('some_place_in_theme', 'template_name', ['title' => Hello!'])
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels