Skip to content

Commit

Permalink
Adding a before_render() filter. Useful for transforming your views b…
Browse files Browse the repository at this point in the history
…efore rendering
  • Loading branch information
Fabrice Luraine committed Mar 11, 2010
1 parent 3dd0fb8 commit 0303e16
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 5 deletions.
1 change: 1 addition & 0 deletions TODO
@@ -1,5 +1,6 @@
# TODO #

- explain before_render() in README
- adding autorender() in abstract.php
- testing and documenting autorendering features
- adding an option('display_errors'); by default ini_set('display_errors', 0) in production env, but enabled if env is dev (unless matching option is enabled)
Expand Down
6 changes: 5 additions & 1 deletion lib/limonade.php
Expand Up @@ -1321,7 +1321,7 @@ function route_find($method, $path)
/**
* Returns a string to output
*
* It might use a a template file or function, a formatted string (like {@link sprintf()}).
* It might use a template file, a function, or a formatted string (like {@link sprintf()}).
* It could be embraced by a layout or not.
* Local vars can be passed in addition to variables made available with the {@link set()}
* function.
Expand All @@ -1337,6 +1337,10 @@ function render($content_or_func, $layout = '', $locals = array())
$content_or_func = array_shift($args);
$layout = count($args) > 0 ? array_shift($args) : layout();
$view_path = file_path(option('views_dir'),$content_or_func);

if(function_exists('before_render'))
list($content_or_func, $layout, $locals, $view_path) = before_render($content_or_func, $layout, $locals, $view_path);

$vars = array_merge(set(), $locals);

$flash = flash_now();
Expand Down
18 changes: 18 additions & 0 deletions lib/limonade/abstract.php
Expand Up @@ -128,3 +128,21 @@ function before_exit()
{

}

/**
* Rendering prefilter
* Useful if you want to transform your views before rendering.
*
* @abstract this function might be redefined by user
* @param string $content_or_func a function, a file in current views dir or a string
* @param string $layout
* @param array $locals
* @param array $view_path (by default <code>file_path(option('views_dir'),$content_or_func);</code>)
* @return array with, in order, $content_or_func, $layout, $locals vars
* and the calculated $view_path
*/
function before_render($content_or_func, $layout, $locals, $view_path)
{
# transform $content_or_func, $layout, $locals or $view_path…
return array($content_or_func, $layout, $locals, $view_path);
}
2 changes: 2 additions & 0 deletions tests/apps/views/hello_world_filtered.html.php
@@ -0,0 +1,2 @@
<p>Hello World Filtered</p>
<?if(isset($lorem)):?><p><?=$lorem?></p><?endif;?>
9 changes: 5 additions & 4 deletions tests/main.php
Expand Up @@ -140,10 +140,11 @@ function test_main_require_once_dir()
ob_clean();

$lib = $root.'/lib';
$limonade = $lib.'/limonade';

$files = require_once_dir($limonade);
assert_not_empty($files);
// pb because it loads abstract.php that conflict with tests that use abstracts
// $limonade = $lib.'/limonade';
//
// $files = require_once_dir($limonade);
// assert_not_empty($files);

$tests_lib = $root.'/tests/data/lib0';
$libs = array('a', 'b', 'c');
Expand Down
35 changes: 35 additions & 0 deletions tests/output.php
Expand Up @@ -92,6 +92,41 @@ function test_output_render_file()
assert_header($response, 'Content-type', 'text/html; charset='.option('encoding'));
}

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

}
elseif(file_exists($view_path) && !array_key_exists('content', $locals))
{
// a view file but not a layout
$view_path = file_path(option('views_dir'), basename($content_or_func, ".html.php") . "_filtered.html.php");
}
else
{
# it's a string
$content_or_func .= "∞FILTERED∞";
}

return array($content_or_func, $layout, $locals, $view_path);
}
$lorem = "Lorem ipsum dolor sit amet.";
$html = render("Lorem %s dolor sit amet.", null, array('ipsum'));
assert_match("/$lorem∞FILTERED∞/", $html);

$views_dir = dirname(__FILE__) . '/apps/views/';
option('views_dir', $views_dir);
$view = 'hello_world.html.php';
$layout = 'layouts/default.html.php';
$html = render($view, $layout, array('lorem' => $lorem));
assert_match("/FILTERED/i", $html);
assert_match("/$lorem/", $html);

}

end_test_case();


Expand Down

0 comments on commit 0303e16

Please sign in to comment.