Last active
April 10, 2021 14:37
-
-
Save thekid/8ce84b0d0de8fce5b6dd5faa22e1d716 to your computer and use it in GitHub Desktop.
XP Frontend example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vendor/autoload.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"xp-forge/frontend" : "dev-master", | |
"xp-forge/handlebars" : "dev-master" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use web\frontend\{Handler, Get, Param}; | |
#[Handler] | |
class Home { | |
#[Get] | |
public function get( | |
#[Param('name')] | |
$param | |
) { | |
return ['name' => $param ?: 'World']; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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) | |
]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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