loonies/kohana-notice

Render does not work as expected

Closed this issue · 6 comments

Why in earth render would return an array instead rendering the page.
Note: version 0.3 (3.2 branch)

I think version 0.2 does a great job

Notice::render method was tightly coupled with view instance, which is wrong IMO. A developer couldn't use some other view rendering engine/parser (like mustache) or use response output as json etc.

Please, refer to the userguide for explanation:

  • usage.md - Displaying notices
  • examples.md - Display the notices

If you want old behaviour extend Notice class and add a wrapper function (something like Notice::view).

I see your point, but i think it's not feasible and need extra typing for all the notices which would lose the simplicity of notice class. Besides render should mean returning the string not array.

A solution probably create driver base class which you can override, for example Notice::$driver = 'default' using Kohana default views, while Notice::$driver = 'kostache' will render using kostache.

How do you mean "need extra typing"? Just put view rendering to the base template layout class/view. You have to do this only once (this is how I'm doing it).

I'm not convinced about returning back view rendering. There was an attempt in 57081c8, lines L59-69 which was not flexibile and ended badly. A driver based view is out of the question. It would require an interface (design by contract) and drivers, which would complicate things even more.

Any name suggestion for Notice::render?

Well, i still convinced about using the back view rendering.
Basically what i mean is, if we want to use another template engine, then we just override the render function.

Probably the best name would be Notice::render_as_array().. or Notice::render($as_array = TRUE);

Mind to share the code how you do it right now?
Because what in my head probably different with your code :)

Mustache/KOstache example

View class:

abstract class View_Admin_Layout extends Kostache_Layout {

    public $charset;
    public $title = 'Title';

    ...

    public function notice()
    {
        $notices = Notice::render();

        return View::factory('notice')
                ->set('notices', $notices);
    }
}

Template file:

<div>{{& notice}}</div>
<p>Page example</p>

Kohana view example

Some custom factory class:

$notices = Notice::render();

$template = View::factory('template');
$template->notices = View::factory('notice')->set('notices', $notices);

View file:

<div><?php echo $notices; ?></div>
<p>Page example</p>

It is even more flexible to handle notices this way. I can, for instance, change the notice view file programmatically. Also, consider I want to return AJAX response as JSON data.

if ($is_ajax)
{
    $response = array(
        'data' => array(....),
        'notices' => Notice::render(),
    );

    $response = json_encode($response);
}

The thing is, I will always get well defined set of data from the Notice::return method. I will than pass those data to the view processor (Mustache, Kohana view, JSON, ...) to handle that. This is very useful when you want to display an error message below the input field where the error occured.

I thought once again about this. ATM I'm not going to change my mind.