TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
Kostache discussion
  • Since I can't reply to the "official" thread (http://forum.kohanaframework.org/discussion/5722/mustache-kohana-module-for-v3-logic-less-views), I thought I'd open a new one, until that's resolved. So here goes:

    I've found out, that the view class' render() method is being called multiple times, once for a template and every time the partial is processed. It probably makes sense somewhere, but that makes this method inappropriate for any "final preparations". Where else can I do these? The constructor is not the best place, too, because passing parameters to the view via set() or bind() is happening after the instance is constructed.

    The only way I see for now is to specifically check for $template parameter being NULL in render() method.
  • How about copying View_Kohana_Layout to your app folder and adding a call to your own prep() method or something within the render() method?
  • LordZicon, creating a View_Layout class in the application/classes/view/layout.php file should work

    czukowski, the partials are indeed also rendered using the same function. You could create a View_Layout class in which you add the required functionality to do your 'final preparations' once.
  • Yes, I went with this:

    class Kostache extends Kohana_Kostache
    {
    public function before_render()
    {
    // Nothing by default
    }

    public function render($template = NULL, $view = NULL, $partials = NULL)
    {
    if (NULL === $template)
    {
    $this->before_render();
    }

    return parent::render($template, $view, $partials);
    }
    }


    But I'm also interested in how people are dealing with such situations.
  • why exactly do you need to run code on render? what's a use case?
  • Well, you have something like that in your own class: http://github.com/synapsestudios/kohana-projecttemplate/blob/dev/0.5.x/application/classes/view/website.php#L48 :)

    But seriously, I'm creating a script that run some environment checks and display the results and the idea was to run them before the render. Though after some thinking I refactored that so that I don't need to do anything in render() method now.

    But I can think of a case where you set some properties of a view class, that do some calculations, which in turn affects how the output of multiple view's methods would look like. Of course, I can come up with another way to do it, than in render() method, it was just the first thing that came up my mind.
  • > The only way I see for now is to specifically check for $template parameter being NULL in render() method.

    What happens when you pass in a template? ;)
  • What's supposed to happen? :)

    I don't pass anything (nor I want to), Mustache does. $template is NULL for the template itself and the partial template code for the partials.

    It wasn't obvious for me from the beginning, that the render() method is going to be called more than once, so I assumed that was a good place to do some stuff, by the analogy with the Controller's before() and after() methods. Now I know I can't do just anything there.
  • I'm thinking Kostache needs before() and after() methods just like Controllers to action upon the View before and after rendering. This would solve the need to adapting the render() method directly or creating extra methods like Zeelot3k's _initialise().

    I guess me saying this should mean I make the commit, but I'm at work at the moment.
  • I would file an issue in the mustache.php repository and see what he says.
  • I don't think you can get around the render() being called multiple times, especially for partials.

    Zeeolot3k's initialize() method is an easy shortcut for the constructor

    I think the before/after methods would be an ok-solution. I think I could use them in my current project :)
  • I'm still not sure whether it is a Mustache issue or something that should be solved in Kostache.
  • All my class basically does is provide autoloading for templates and partials. I don't actually provide any new functionality, and it should be kept that way.
  • @zombor I know what you are saying however I am (I can only speak for myself) using Kostache as an entire View solution in an MVC framework, is that what Mustache.php is actually designed for or is it more of just template parser? If it is more than just that then I guess I should raise the issue with the main implementation of Mustache rather than Mustache.php.
  • Also an after method would be awesome for putting the generated View into a layout as your View_Layout class does by extending render().
  • Since it looks like the main Kostache thread is closed or atleast not letting me post, I thought I'd ask m Kostache questions here.

    I've just started to try out Kostache and just Mustache in general. I've been trying to extend the View_Layout class and add other partials to the main layout. I'm not able to get my nav partial to get processed. will show an empty set of ul tags, but nothing in between. Any help would be great thanks.
    Here is the pastbin link: http://pastebin.com/Z8wVR5n0
  • Partials are template without classes, not the other way around. If you add the 'nav' => 'nav' partial that means you need to create the templates/nav.mustache file and the html in there will render in the scope of the class that uses it as a partial.
  • So would that mean in my View_Site class I would have a nav method or property? Also, does that mean if I extend from the View_Site class could i change the nav property if needed?
  • No, it would look for templates/nav.mustache and you could use that in your templates with {{>nav}}

    The scope of things in {{>nav}} would resolve to the current view class.
  • thanks zombor and Zeelot3k, I've got it straightened out now.