-
{?categories}
- {$name} {/?}
Wallogit.com
2017 © Pedro Peláez
Skhema is a templating engine with an elegant and compact notation, that I initially developed for use by Bramble., (*1)
{@template}, (*2)
{$variable}, (*3)
{#include}, (*4)
{^extend}, (*5)
{.define}, (*6)
{?source}, (*7)
{/close}, (*8)
/ is irrelevant.{%call}, (*9)
$manager = manager = TemplateManager::create((TEMPLATE_PATH);
$output = $manager->evaluate('Example', $data);
A root named binding will apply to all matching template instances. There is currently no way to specify that a template usage cannot be bound by name. I probably won't change this because it would be very poor design to have a template structure where that is a possibility. Such silliness will not be allowed., (*10)
For now, binding is still very basic. In the Bramble MVC pattern, the Model is responsible for querying the database and transforming the results into the template mappings. The following snippets show part of the process of mapping query results to the expected template parameters and assigning them to a root named binding., (*11)
$posts_sql = '
SELECT p.ID, p.Date, p.Title, p.Slug, p.Content, u.DisplayName FROM Posts p
INNER JOIN Users u ON p.AuthorID = u.ID
WHERE p.Type = 2
AND p.Date BETWEEN :start AND :end
ORDER BY p.Date DESC
';
$list[] = [
'author' => $row['DisplayName'],
'date' => date('Y-m-d', $time),
'url' => FormatURL($time, $row['Slug']),
'title' => $row['Title'],
'content' => $row['Content'],
'categories' => $categories,
];
return [
'Posts' => [
'title' => $title,
'list' => $list
]
];
Functions and variables are implemented as an evaluation token with a stack of filters, using a shared syntax. Filters can be stacked on variables, functions, and other filters., (*12)
{%cycle[white,LightGray]}
{%format-url[post]}
{%first[list/time]:format-date[atom]}
{$content:subvert[code,root,header=3]}
The function/filter needs to be registered and the arguments are the filter options, the current binding context, and the input value (for filters). Any applicable filter options are the responsibility of the filter handler., (*13)
$manager->register('format-date', function($options, $context, $date) {
// return the formatted date using the specified options
});
This is a basic template with variables, inline template definitions, includes, and binding source. The inline templates are probably not a common way of doing things, but they work the same as included instances. In this particular case, they simply serve to change the named binding context. The {$content} variable in this case is intended to be populated by an inheriting template., (*14)
{@TemplateBase}
<!DOCTYPE html>
<html>
<head>
<title>{$title}</title>
</head>
<body>
<div id="header">
{@Header}
<h1><a href="{$root-url}">jacere.net</a></h1>
{@Navigation}
<div id="nav">
<ul>
{?nav-list}
<li><a href="{$url}">{$text}</a></li>
{/?}
</ul>
</div>
{/@}
{/@}
</div>
<div id="content">
{$content}
</div>
<div id="sidebar">
{#Sidebar}
</div>
</body>
</html>
{/@}
Here is an example of inheriting this base template to show a list of posts. The {$content} variable is defined as a binding source, which is basically a loop over the named data context., (*15)
{@Posts}
{^TemplateBase}
{.content}
{?list}
{#PostSection}
{/?}
{/.}
{/@}
{@PostSection}
{/@}
This example shows an inheritance hierarchy where each template extends or defines the functionality of the parent., (*16)
{@SidebarSection}
{$title}
{$content}
{/@}
{@SidebarSectionList}
{^SidebarSection}
{.content}
This project is licensed under the terms of the MIT license., (*17)