Skip to content

Commit

Permalink
Adding example for before_render()
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabrice Luraine committed Mar 11, 2010
1 parent eb4ce64 commit ea858b3
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
98 changes: 98 additions & 0 deletions examples/example06/index.php
@@ -0,0 +1,98 @@
<?php
# Before render example

require_once dirname(dirname(dirname(__FILE__))).'/lib/limonade.php';



function configure()
{
option('env', ENV_DEVELOPMENT);
layout('html_default_layout');
}

function before_render($content_or_func, $layout, $locals, $view_path)
{
if(is_callable($content_or_func))
{

}
elseif(file_exists($view_path))
{
# we can distinguish layouts from normal views if we want
# $is_a_layout = array_key_exists('content', $locals);
$view_path = render_filter_rewrite_short_tags($view_path);
}
else
{
# it's a string
$content_or_func .= "<small>added before render…</small>";
}

return array($content_or_func, $layout, $locals, $view_path);
}

/**
* a filter for rewriting views without short_open_tags
*
* @param string $view_path
* @return string $path to converted view file
*/
function render_filter_rewrite_short_tags($view_path)
{
if (option('rewrite_short_tags') == true && (boolean)@ini_get('short_open_tag') === false)
{
# cache path, maybe in tmp/cache/rsot_views/
$cache_path = file_path(option('rewrite_sot_cache_dir'), $view_path);
if(!file_exists($cache_path) || (filemtime($cache_path) != filemtime($view_path)))
{
$view = file_get_contents($view_path);
$transformed_view = preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', $view));
# TODO: store it in cache
# …
}
$view_path = $cache_path;
}
return $view_path;
}

dispatch('/', 'index');
function index()
{
return html('<p>Hellooo!</p>');
}

dispatch('/error', 'index_error');
function index_error()
{
return halt('Error!');
}

run();

# _INLINE templates___________________________________________________________

function html_default_layout($vars){ extract($vars);?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Before render filter test</title>
</head>
<body>
<article>
<?=$content;?>
</article>
<hr>
<nav>
<p><strong>Menu:</strong>
<a href="<?=url_for('/')?>">Index</a> |
<a href="<?=url_for('/error')?>">Error</a>
</p>
</nav>
<hr>
</body>
</html>
<?};

?>
1 change: 1 addition & 0 deletions examples/index.php
Expand Up @@ -17,6 +17,7 @@
<li><a href="example03/">example 03: routes with options, and conditional before function</a></li>
<li><a href="example04/">example 04: autorendering example</a></li>
<li><a href="example05/">example 05: using <code>content_for()</code></a></li>
<li><a href="example06/">example 06: using <code>before_render()</code> filtering</a></li>
<li><a href="urlrewrite/"> Url Rewriting with Limonade.</a></li>
</ul>
</body>
Expand Down

0 comments on commit ea858b3

Please sign in to comment.