Last active
April 26, 2017 16:15
-
-
Save xappz/b12bf663933dfb6b26387d502e8e90aa to your computer and use it in GitHub Desktop.
Use php-pug with Slim3
This file contains hidden or 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 | |
// DIC configuration | |
$container = $app->getContainer(); | |
$container['renderer'] = function ($c) { | |
$settings = $c->get('settings')['renderer']; | |
return new \Renderers\PugRenderer($settings); | |
}; |
This file contains hidden or 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 | |
if (PHP_SAPI == 'cli-server') { | |
// To help the built-in PHP dev server, check if the request was actually for | |
// something which should probably be served as a static file | |
$url = parse_url($_SERVER['REQUEST_URI']); | |
$file = __DIR__ . $url['path']; | |
if (is_file($file)) { | |
return false; | |
} | |
} | |
require __DIR__ . '/../vendor/autoload.php'; | |
session_start(); | |
// Instantiate the app | |
$settings = require __DIR__ . '/../src/settings.php'; | |
$app = new \Slim\App($settings); | |
// Set up dependencies | |
require __DIR__ . '/../src/dependencies.php'; | |
// Register routes | |
require __DIR__ . '/../src/routes.php'; | |
// Run app | |
$app->run(); |
This file contains hidden or 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 | |
namespace Renderers; | |
use \Pug\Pug as Pug; | |
class PugRenderer { | |
private $mPath; | |
private $mPug; | |
public function __construct($settings) { | |
$this->mPath = $settings['template_path']; | |
$this->mPug = new Pug(array( | |
'prettyprint' => true, | |
'extension' => '.pug', | |
//'cache' => '../cache/' | |
)); | |
} | |
public function render($response, $file, $data) { | |
$filepath = $this->mPath . $file; | |
$body = $response->getBody(); | |
$body->write($this->mPug->render($filepath, $data)); | |
} | |
} |
This file contains hidden or 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 | |
// Routes | |
$app->get('/[{name}]', function ($request, $response, $args) { | |
// Render index view | |
return $this->renderer->render($response, 'index.pug', $args); | |
}); |
This file contains hidden or 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 | |
return [ | |
'settings' => [ | |
// Renderer settings | |
'renderer' => [ | |
'template_path' => __DIR__ . '/../templates/', | |
], | |
], | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment