slimphp / PHP-View

A Simple PHP Renderer for Slim 3 & 4 (or any other PSR-7 project)
MIT License
263 stars 60 forks source link

Ability to render partial view, output as string instead of directly to stream #16

Closed eko3alpha closed 8 years ago

eko3alpha commented 8 years ago

Not so much a bug but a suggestion...

Slim2's view class had 2 methods available ( and 1 private ) in its view class. One allowed you to return a rendered template as a string, the other does what PHP-View render() effectively does.

// Slim2
public function fetch($template, $data = null) // returns rendered partial as string
public function display($template, $data = null) // outputs to screen

protected function render($template, $data = null) // does the parsing and passes to fetch/display

PHP-View only has one option available

// Slim3
public function render(ResponseInterface $response, $template, array $data = [])

The problem with the new implementation is that I cannot render and store partials as strings to be used in other parts of the app. Before I create my own implementation thats 99% similar, I wanted to chime in to see if this feature would be added to PHP-Views.

For example there is no way for me to do this, load up a few partials "alert_view" and "content_view" and then use an "empty_wrapper"

        $view['alert_view'] = $this->view->fetch('modules/alert_view.php', $view);
        $view['content_view'] = $this->view->fetch('login_view.php', $view);
        $this->view->display('wrappers/wrapper_empty_view.php', $view);

Since making the interface like Slim2 is probably not an option as you are using render() as Slim2 display(). Maybe create a new method named:

getPartial, partial, renderPartial?

that will allow you to get the output of a rendered template as a string.

geggleto commented 8 years ago

Just so that I am clear, you would like a object oriented wrapper around php's include ?

I guess if we Object orient it we can create a different scope so you don't leak variables from one view to the other.

I will take a look at this, sometime this week.

geggleto commented 8 years ago

Closing, fixed in #19 , #21