Skip to content

Instantly share code, notes, and snippets.

@thekid
Last active April 10, 2021 14:37
Show Gist options
  • Save thekid/8ce84b0d0de8fce5b6dd5faa22e1d716 to your computer and use it in GitHub Desktop.
Save thekid/8ce84b0d0de8fce5b6dd5faa22e1d716 to your computer and use it in GitHub Desktop.
XP Frontend example
vendor/autoload.php
{
"require": {
"xp-forge/frontend" : "dev-master",
"xp-forge/handlebars" : "dev-master"
}
}
<?php
use web\frontend\{Handler, Get, Param};
#[Handler]
class Home {
#[Get]
public function get(
#[Param('name')]
$param
) {
return ['name' => $param ?: 'World'];
}
}
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hello {{name}}</title>
</head>
<body>
<h1>Hello {{name}}</h1>
</body>
</html>
<?php
use web\Application;
use web\frontend\{Frontend, Templates, AssetsFrom};
class Site extends Application {
/** @return [:var] */
protected function routes() {
$assets= new AssetsFrom($this->environment->webroot());
$templates= new TemplateEngine($this->environment->webroot());
return [
'/favicon.ico' => $assets,
'/static' => $assets,
'/' => new Frontend(new Home(), $templates)
];
}
}
<?php
use com\handlebarsjs\{HandlebarsEngine, FilesIn};
use io\Path;
use web\frontend\Templates;
class TemplateEngine implements Templates {
private $backing;
public function __construct(Path $templates) {
$this->backing= (new HandlebarsEngine())->withTemplates(new FilesIn($templates));
}
public function write($name, $context, $out) {
$this->backing->write($this->backing->load($name), $context, $out);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment