Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- layout.php:
- <?php
- class layout extends Slim_View
- {
- static protected $_layout = NULL;
- public static function set_layout($layout=NULL)
- {
- self::$_layout = $layout;
- }
- public function render( $template ) {
- extract($this->data);
- $templatePath = $this->getTemplatesDirectory() . '/' . ltrim($template, '/');
- if ( !file_exists($templatePath) ) {
- throw new RuntimeException('View cannot render template `' . $templatePath . '`. Template does not exist.');
- }
- ob_start();
- require $templatePath;
- $html = ob_get_clean();
- return $this->_render_layout($html);
- }
- public function _render_layout($_html)
- {
- if(self::$_layout !== NULL)
- {
- $layout_path = $this->getTemplatesDirectory() . '/' . ltrim(self::$_layout, '/');
- if ( !file_exists($layout_path) ) {
- throw new RuntimeException('View cannot render layout `' . $layout_path . '`. Layout does not exist.');
- }
- ob_start();
- require $layout_path;
- $_html = ob_get_clean();
- }
- return $_html;
- }
- }
- ?>
- --------
- index.php:
- <?php
- require 'Slim/Slim.php';
- require 'layout.php';
- // instantiate my custom view
- $layout = new layout();
- // seems you need to specify during construction
- $app = new Slim(array('view' => $layout));
- $app->config(array(
- 'templates.path' => './ui',
- ));
- // specify the a default layout
- layout::set_layout('default_layout.php');
- $app->get('/', function() use ($app) {
- // you can override the layout for a particular route
- // layout::set_layout('index_layout.php');
- $app->render('index.php',array());
- });
- $app->run();
- ?>
- ------------
- default_layout.php:
- <html>
- <head>
- <title>My Title</title>
- </head>
- <body>
- <!-- $_html contains the output from the view -->
- <?= $_html ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement