flush
[pharext/pharext.org] / app / bootstrap / plates.php
1 <?php
2
3 namespace app;
4
5 require_once __DIR__."/config.php";
6 require_once __DIR__."/web.php";
7
8 use League\Plates;
9
10 use http\Env\Request;
11 use http\Env\Response;
12
13 $injector->share(Plates\Engine::class)
14 ->define(Plates\Engine::class, [
15 ":directory" => __DIR__."/../views",
16 ":fileExtension" => "phtml"
17 ])
18 ->prepare(Plates\Engine::class, function(Plates\Engine $view) use($injector) {
19 $view->addData([
20 "config" => $injector->make(Config::class),
21 "baseUrl" => $injector->make(BaseUrl::class),
22 "request" => $injector->make(Request::class),
23 "response" => $injector->make(Response::class),
24 ]);
25 $view->registerFunction("shorten", function($text) {
26 if (strlen($text) < 78) {
27 return $text;
28 }
29 return current(explode("\n", wordwrap($text)))."…";
30 });
31 $view->registerFunction("utc", function($d) {
32 return date_create($d)->setTimeZone(new \DateTimeZone("UTC"));
33 });
34 $view->registerFunction("md", function($string, $file = false) {
35 if ($file) {
36 $md = \MarkdownDocument::createFromStream($string);
37 } else {
38 $md = \MarkdownDocument::createFromString($string);
39 }
40 $md->compile(\MarkdownDocument::AUTOLINK);
41 return $md->getHtml();
42 });
43 });
44