-
-
Save dwayne/5844572 to your computer and use it in GitHub Desktop.
Revisions
-
eric1234 revised this gist
May 22, 2013 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -21,14 +21,14 @@ function apply_content_filters($in) { function layout_shutdown() { global $layout; global $use_layout; global $error_triggered; if( !$use_layout ) return; $content = apply_content_filters(ob_get_contents()); ob_end_clean(); if( $error_triggered || is_null($layout) ) { echo $content; return; } -
eric1234 revised this gist
Feb 1, 2013 . 2 changed files with 79 additions and 20 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,78 @@ Usage ===== Creating a Layout File ---------------------- Create a file called `layout.php`. Where you want the content to appear add `<?php echo $content ?>`. Here is an example: <!DOCTYPE html> <html> <head> <title><?php echo $title ?></title> </head> <body> <div id="page"> <?php echo $content ?> </div> </body> </html> This should be placed in the same directory a your scripts. Using the Layout File --------------------- Now just add the following to the top of every script (or some include the scripts pull in): <?php require_once 'layout.php'; ?> Any content outputted in your script will be placed where $content is outputted. Configuration ------------- The `$title` variable is automatically pulled into the layout scope. To set the title simply place the following in your script: <?php $title = 'Home page' ?> You can place this before or after the layout require. If you want to change where the layout file is located just set the $layout variable before calling layout. For example: <?php $layout = __DIR__.'/path/to/layout.php'; require_once 'layout.php'; ?> Content Filters --------------- This script provides an excellent opportunity to apply a filter to the entire output. Similar to an after_filter in Rails. Simply add a callback function to the global variable `$content_filters`. You can do this before the call to `layout.php` (so you are defining `$content_filters` or after by simply appending. Example: <?php require_once 'layout.php'; $content_filter[] = 'adjust_markup'; ?> The callback should take the HTML as input and return the final HTML. Content filters only apply to the `$content` and not the layout. Also if the layout is not used then the content filters are not called. Handling Redirects ================== Redirects are automatically handled. If you set the Location header and exit the layout script will automatically avoid rendering the layout and instead just exit. 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 charactersOriginal file line number Diff line number Diff line change @@ -1,31 +1,12 @@ <?php # https://gist.github.com/2151128 if( !array_key_exists('layout', get_defined_vars()) ) $layout = realpath('./layout.php'); $use_layout = true; if(is_null($layout)) $use_layout = false; function apply_content_filters($in) { global $content_filters; foreach($content_filters as $filter) { -
eric1234 revised this gist
Jan 9, 2013 . 1 changed file with 40 additions and 16 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -2,36 +2,60 @@ /* * Require before you start outputting content and all content will be * automatically captured and placed in a layout file (defaults to * layout.php located in the same directory as the script). * * Set $layout before the script ends to pick a different layout file * (should be first in the include path). If you set to null then no * layout will be used but you must set it to null before calling this * file. * * If a redirect is issued (location header set) then the layout will * automatically not be used. */ if( !array_key_exists('layout', get_defined_vars()) ) $layout = realpath('./layout.php'); $use_layout = true; if(is_null($layout)) $use_layout = false; # Will iterate through the functions registered in the $content_filters # array and run all of them on the input returning the output. This # function is tied to the ob_start() output filter providing a way to # easily adjust the content. # # NOTE: These filters are NOT called if a layout is not used. function apply_content_filters($in) { global $content_filters; foreach($content_filters as $filter) { $in = call_user_func($filter, $in); } return $in; } if( !isset($content_filters) ) $content_filters = array(); if( $use_layout ) ob_start(); function layout_shutdown() { global $layout; global $use_layout; if( !$use_layout ) return; $content = apply_content_filters(ob_get_contents()); ob_end_clean(); global $error_triggered; if( $error_triggered ) { echo $content; return; } foreach(headers_list() as $header) if( preg_match('/^Location/', $header) ) return; global $title; require $layout; } register_shutdown_function('layout_shutdown'); -
eric1234 revised this gist
Mar 21, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -26,7 +26,7 @@ function layout_shutdown() { if(is_null($layout)) $use_layout = false; foreach(headers_list() as $header) if( preg_match('/^Location/', $header) ) $use_layout = false; if( $use_layout ) { require $layout; -
eric1234 created this gist
Mar 21, 2012 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,37 @@ <?php /* * Require before you start outputting content and all content will be * automatically captured and placed in a layout file (defaults to layout.php * located in the same directory as the script). * * Set $layout before the script ends to pick a different layout file * (should be first in the include path). If you set to null then no layout * will be used. * * If a redirect is issued (location header set) then the layout will * automatically not be used. */ if( !array_key_exists('layout', get_defined_vars()) ) $layout = 'layout.php'; ob_start(); function layout_shutdown() { global $layout; $content = ob_get_contents(); ob_end_clean(); $use_layout = true; if(is_null($layout)) $use_layout = false; foreach(headers_list() as $header) if( preg_match('/^Location/', $header) ) $use_layout = true; if( $use_layout ) { require $layout; } else { echo $content; } } register_shutdown_function('layout_shutdown');