Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Requesting assets from a view
  • I have a Controller_Template controller with methods that put views into the content of the default page template, i.e. the methods end with this type of thing:
    $this->template->content = View::forge('my/view', $data);
    

    In the page template (views/template.php) I have put a line to render general assets:
    <?php echo Asset::render('general'); ?>
    

    Now, in my controller I can add assets to the general group, and they are rendered properly in the delivered page, e.g.:
    Asset::js('jquery-1.7.min.js', array(), 'general');
    

    However, assets being a largely view type of thing, rather than controller oriented, I would like to put that asset request into the view, i.e. in my/view.php from the example above. That does not appear to work. I am guessing the outer page template is being constructed *before* the content view is rendered. Is that right? Is that the way it works? Is that the way it should work? Is there a general way to ensure the outer page template, with its header containing references to assets used, is constructed last, after any views are put together? Do I perhaps need to render the view in the controller and pass it out to the outer template as a HTML string, rather than a view object? I can kind of see why this is happening - the page template is just PHP and executes in order, and does not get to the "echo $content" until *after* it has constructed the HTML header, and the $content template is not evaluated (converted to HTML) until the flow reaches the "echo $content". Any ideas? -- Jason
  • FuelPHP utilizes a mechanism called lazy rendering, which means that views are only rendered when they are being converted to string (i.e. when they are echo'd or explicitly cast to string). That indeed means that your page template is rendered first, and then any view objects passed to it. If you don't want that, use View::forge('myview')->render(); which will assign the rendered version to the template.
  • Thanks - that looks a straight forward approach. I see the magic method __toString() calls up render() - I wondered how echoing a template object returned the HTML. I've never come acorss that magic method before. __sleep() and __wake() are handy for stripping an object down to its bare minimum before serialising it, then rebuilding it while unserializing it, and I've used them before. Yeah, been using PHP for ten years, and still learning about some of its neat features. Edit: it is not just a case of returning a rendered version of the view from the controller:
    $this->template->content = View::forge('my/view', $data)->render();
    

    Instead it needs to be passed in with an explicit instruction not to XML encode it. This works in the controller, to render the view first, then pass it to $content in the page template:
    $this->template->set('content', View::forge('my/view', $data)->render(), false);
    

    as this gets XML-encoded as it is being passed to the content variable in the template.
  • Correct, that is a side-effect of early rendering, you need to make sure the rendered HTML is not escaped.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion