vlucas / bulletphp

A resource-oriented micro PHP framework
http://bulletphp.com
BSD 3-Clause "New" or "Revised" License
416 stars 50 forks source link

Enhancement -possibly a new event type #42

Closed sam2332 closed 10 years ago

sam2332 commented 10 years ago

i would like a event for "onBeforeTemplateLoad" with access to the templating data for modification

does one exist?

vlucas commented 10 years ago

No event like that exists currently - can I see some example code that you need the event for so I can better understand the need and how to implement it?

sam2332 commented 10 years ago

alright ill work on an example

sam2332 commented 10 years ago
$app->on('beforeTemplateRender',function($path,&$data)use($app){
    $data['websiteName']='Test Website';
    $data['widget']=$app->template('widget');
});

$app->path('/', function($request) {
    return $app->template('home',array(
        'activeNav'=>'home',
    ));
});

something like that so i can put my website varibles code in a common place when its loaded on every page

vlucas commented 10 years ago

I see. That does seem useful. I would probably change the event params to accept a Template object:

$app->on('beforeTemplateRender',function($path, Bullet\View\Template $tpl) use($app){
    $tpl->set('websiteName', 'Test Website');
    $tpl->set('widget', $app->template('widget'));
    return $tpl;
});
sam2332 commented 10 years ago

THAT LOOKS ALOT BETTER! xD if we can do this it will make bulletphp code look even more sexy!

sam2332 commented 10 years ago

omg this also could allow for returing 404s perfect

sam2332 commented 10 years ago

are you gonna work on this or should i fork and merge?

vlucas commented 10 years ago

You can already return and handle 404's! Here's how I do it: https://github.com/vlucas/twicebreaker/blob/master/app/common.php#L115

Go ahead and fork to give this a shot if you want - I won't be able to get to it for another few days.

Also, you can use view partials for the widget within your layout view.

layouts/application.html.php

echo $view->partial('widget');

The partial call instantiates and returns a new Bullet\View\Template object and turns the layout wrapping off for the partial.

sam2332 commented 10 years ago

i think its cleaner for the designer to put the templating in the controller personally but thanks for the info i didn't know that worked i might take a swing at adding the event

vlucas commented 10 years ago

I think I am going to implement this in bullet now - stay tuned :)

vlucas commented 10 years ago

This feature has been added. Here's what the syntax looks like:

// Example for ensuring AJAX requests don't get rendered with layouts
use Bullet;
$app->on('beforeResponseHandler', function(Request $request, Response $response, $rawResponse) use($app) {
    if ($request->isAjax() && $rawResponse instanceof \Bullet\View\Template) {
        $rawResponse->layout(false);
    }
});

The reason for the separate $rawResponse variable is because this event is called BEFORE any response handling - so it gives you the raw response returned directly from the called route. Keep in mind that according to the Bullet response docs, this could be a wide variety of things - a string, integer, boolean, array, etc. so the type checking is left up to you.

sam2332 commented 10 years ago

sweet cant wait to try it out