Skip to content

Commit

Permalink
Better autorender feature (option('autorender') is no longer required…
Browse files Browse the repository at this point in the history
…) + docs + test
  • Loading branch information
Fabrice Luraine committed Jul 2, 2010
1 parent 389740d commit 57d7632
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1 +1,2 @@
.DS_STORE
.DS_STORE
.tmproj
28 changes: 28 additions & 0 deletions README.mkd
Expand Up @@ -295,6 +295,8 @@ Output is temporized so that it can easily handle large files.

## Before and after request ##

### Before ###

You can define a `before` function that will be executed before each request. This is very useful to define a default layout or passing common variables to the templates

function before($route)
Expand All @@ -303,6 +305,7 @@ You can define a `before` function that will be executed before each request. Th
set('site_title', 'My Website');
}


The current matching route is also passed to the before function, so you can test it. It's an array as returned by the internal `route_find` function, with those values:

* `method` (HTTP method)
Expand All @@ -312,6 +315,8 @@ The current matching route is also passed to the before function, so you can tes
* `options` (route options)
* `params` (current params)

### After ###

An `after` output filter is also available. It's executed after each request and can apply a transformation to the output (except for `render_file` outputs which are sent directly to the output buffer).

function after($output, $route){
Expand All @@ -325,6 +330,29 @@ An `after` output filter is also available. It's executed after each request and

The current executed route is also available for `after` function.

### Autorender ###

You can define your own `autorender` function to make automatic rendering depending on current matching route. It will be executed if your controller returns a null output.

dispatch('/', 'hello');
function hello()
{
# process some stuff...
set('name', 'Bob');
# but don't return anything
# ( like if you were ending this function with return null; )
}

function autorender($route)
{
$view = $route['function'] . ".html.php";
return html($view);
}

In this example, when url `/` is called, `hello()` is executed and then `autorender()` renders the matching `hello.html.php` view.


## Configuration ##

You can define a `configure` that will be executed when application is launched (at the begining of the `run` execution ).
Expand Down
3 changes: 1 addition & 2 deletions lib/limonade.php
Expand Up @@ -329,7 +329,6 @@ function run($env = null)
option('session', LIM_SESSION_NAME); // true, false or the name of your session
option('encoding', 'utf-8');
option('gzip', false);
option('autorender', false);
option('x-sendfile', 0); // 0: disabled,
// X-SENDFILE: for Apache and Lighttpd v. >= 1.5,
// X-LIGHTTPD-SEND-FILE: for Apache and Lighttpd v. < 1.5
Expand Down Expand Up @@ -409,7 +408,7 @@ function autoload_controller($callback)

# 6.4 Call matching controller function and output result
$output = call_user_func_array($route['function'], array_values($route['params']));
if(is_null($output) && option('autorender')) $output = call_if_exists('autorender', $route);
if(is_null($output)) $output = call_if_exists('autorender', $route);
echo after(error_notices_render() . $output, $route);
}
else halt(SERVER_ERROR, "Routing error: undefined function '{$route['function']}'", $route);
Expand Down
2 changes: 1 addition & 1 deletion lib/limonade/abstract.php
Expand Up @@ -149,7 +149,7 @@ function before_render($content_or_func, $layout, $locals, $view_path)


/**
* Called only if option('autorender') is enabled and rendering $output is_null,
* Called only if rendering $output is_null,
* like in a controller with no return statement.
*
* @abstract this function might be defined by user
Expand Down
10 changes: 10 additions & 0 deletions tests/apps/02-outputs.php
Expand Up @@ -26,4 +26,14 @@ function jpeg_file()
return render_file(dirname(dirname(__FILE__)).'/data/deer.jpg');
}

dispatch('/autorender', 'empty_controller');
function empty_controller()
{

}

function autorender($route){
return "AUTORENDERED OUTPUT for ".$route['function'];
}

run();
6 changes: 6 additions & 0 deletions tests/output.php
Expand Up @@ -127,6 +127,12 @@ function before_render($content_or_func, $layout, $locals, $view_path)

}

function test_output_autorender()
{
$response = test_request(TESTS_DOC_ROOT.'02-outputs.php/autorender', 'GET');
assert_equal($response, 'AUTORENDERED OUTPUT for empty_controller');
}

end_test_case();


Expand Down

0 comments on commit 57d7632

Please sign in to comment.