TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
Kostache v3.0
  • Hello-

    With the release of Kohana 3.3 today, I've pushed to master PSR-0 support in Kostache. I've bumped the version because it's an api change.

    Also I started working on a new version which incorporates Mustache.php v2.0. It's radically different, so if you want to give me feedback, just check out the feature/mustache-2.0 branch in git.

  • I would like to help out with this if I can...

  • Neat.. have to dive into it to check/test it.. mustache v2 is really a lot of code compared to v1, lol.. oop, nice! Thanks for the work so far.

  • I just need to port layouts over and then it will be done and released.

  • I can't find anything on v2 compared to v1. Talking about Mustache for PHP specifically...

  • Yes, I am missing the documentation/changelog too on git, but the code is just completely replaced (not sure about the basic few api function though).. it seems it's just more robust in every way.

  • Awesome.

    I'll be looking into it tonight.

  • a simple vie call triggers the following error:

    
    ErrorException [ Recoverable Error ]: Argument 2 passed to Kohana_Kostache::__construct() must be of the type array, object given
    MODPATH/kostache/classes/Kohana/Kostache.php [ 59 ]
    54   * @param   array   partial paths
    55   * @return  void
    56   * @uses    Kostache::template
    57   * @uses    Kostache::partial
    58   */
    59  public function __construct($template = NULL, array $partials = NULL)
    60  {
    61      if ( ! $template)
    62      {
    63          if ($this->_template)
    64          {
    

    with:

    
        $view = new View_Example;
        echo $view;
    

    what could be the problem?

  • What version is this?

  • I finished layouts in the new mustache-2.0 branch. Please check it out if you have time: https://github.com/zombor/KOstache/tree/feature/mustache-2.0

    I'll be merging this to master in a couple days. It's done.

  • I tried all branches

    master: image

    mustache-2.0 branch: image

  • Please paste your code you are using. You are obviously passing an object as the second parameter, and that's not allowed.

  • controller:

    public function action_index()
    {
        $view = new View_Example;
        echo $view;
    }
    

    View_Example:

    class View_Example extends Kostache
    {
        public $foo = 'bar';
    }
    

    example.mustache:

    This is a {{foo}}
    

  • I don't see how that code would cause that error.

  • I have downloaded a fesh zip of ko3.3 and kostache, and set up a new app with the code from above, and I still get no results.

    what could be wrong with a so simple demo thing?

  • Ok I got it. If you get the files from the cli with git (kostache 3 and mustache 1), then the example above works. If you download kostache zips from the github site, then there are a buncs of errors. I always carefully checked the init.php of the module, so finding the mustache autoloader could be not the problem.

    Currently I can only use kostache 3 with mustache 1.1. Kostache 4 with mustache 2.0.2 simply does not work for me. ErrorException [ Warning ]: Missing argument 1 for Kohana_Kostache::__construct(), called in /Applications/MAMP/htdocs/application/classes/Controller/Portfolio.php on line 46 and defined

    controller line 46: $this->view = new View_Portfolio_List;

    So far I know one should be able to use different kostache versions with different mustache versions.

  • Is there a Kostache 4?

  • at least in the KOstache/classes/Kohana/Kostache.php

    const VERSION = '4.0.0';

  • If you download kostache zips from the github site, then there are a buncs of errors

    Right, because the zip doesn't include the submodules.

    So far I know one should be able to use different kostache versions with different mustache versions.

    No. Kostache <=3 is made for Mustache.php < 2. Kostache v4 is made for Mustache.php v2.0. They are not compatible.

    Kostache 4 with mustache 2.0.2 simply does not work for me.

    The usage is completely different than v3. Did you read the readme?

  • Is Kostache v4 considered stable and ready for production use?

    I have a client who insists that I can't use stuff until it has been officially announced. All he sees here is the announcement about v3 so he's worried that v4 isn't really ready.

  • Is Kostache v4 considered stable and ready for production use?

    It's in master. It's released :)

    All he sees here is the announcement about v3 so he's worried that v4 isn't really ready.

    I "announced" it on twitter.

  • Thanks. I'll use it.

  • @zsolt, note the change in the implementation of the view class.

    From:

    class View_Example extends Kostache
    

    To:

    class View_Example
    

    The view class no longer inherits from Kostache but is passed into an instance of the Kostache renderer. The difference is subtle, but when you call "new View_Welcome" (inheriting from Kostache), it is trying to instantiate another Kostache object without the required $engine parameter that is usually provided by the Kostache::factory().

  • thanks @nightfury I don;t know what I am doing wrong, but it is not working. Error ErrorException [ Warning ]: file_get_contents(): Filename cannot be empty (MODPATH/kostache/classes/Mustache/Loader/Kohana.php [ 33 ])

    my simple test:

    class Controller_Home extends Controller {
    
        private $view;
        private $content;
    
        public function action_index()
        {
            $renderer = Kostache::factory();
            $kostache_4_test = new View_Home_Index;
            $myview->message = 'kostache 4.0 ...';
            $this->response->body($renderer->render($kostache_4_test));
        }
    }

    class View_Home_Index {
    
        protected $_layout = 'layout';
        protected $_partials = array(
            'head'       => 'partials/head',
            'navigation' => 'partials/navigation',
            'footer'     => 'partials/footer',
            'text'       => 'home/text',
        );
    
        public function message()
        {
            return $this->message;
        }
    }

    partials are in their places

    and the view object seems to be good, any ideas?

    object View_Home_Index(5) {
        protected _partials => array(5) (
            "head" => string(13) "partials/head"
            "navigation" => string(19) "partials/navigation"
            "footer" => string(15) "partials/footer"
            "background" => string(19) "partials/background"
            "text" => string(9) "home/text"
        )
        protected _layout => string(6) "layout"
        public title => string(32) "Sándor Zsolt | tervezőgrafikus"
        public description => string(121) "Kohana homepage. Kohana is an HMVC PHP5 framework
         that provides a rich set of components for building web applications."
        public message => string(16) "kostache 4.0 ..."
    }
    

  • ErrorException [ Warning ]: file_get_contents(): Filename cannot be empty

    This probably means it can't find your template. Make sure it's named correctly.

    Also, your view object looks like it has way too much stuff in it. It shouldn't have _partials or _layout anymore.

  • file names are correct, they are working with kostache 3. Also I have the __mustache cache file at application/cache.

    How do you use partials then? Only with {{>partial}} in the mustache file?

  • file names are correct, they are working with kostache 3

    You should debug the template loader and see what's going on then.

    How do you use partials then? Only with {{>partial}} in the mustache file?

    yeah, the renderer automatically finds them in your filesystem from the template loader.

  • @thanks zombor, sorry, one of my partials was at templates/home/ not in templates/partial

    how do you deal with ajax calls now? $this->view->render_layout = FALSE;

  • https://github.com/zombor/KOstache#layouts

    There's a subclassed renderer for layouts.

  • How do we dynamically load partials? For example if a user is admin show partial 'adminmenu' if not show usermenu. Does this mean that I have to place logical operations in the .mustache file?

  • Quick code to get you an idea

    view class

    class View_Example extends Kostache {
    
        protected $_partials = array(
            'foo'  => 'partials/foo',
            'menu' => NULL,
        );
    
        public function set_menu_partial($name)
        {
            $this->_partials['menu'] = 'partials/menu/'.$name;
        }
    }
    

    controller

    $user = new User;
    $user->role('admin');
    
    $view = new View_Example;
    
    if ($user->has_role('admin'))
    {
        $view->set_menu_partial('admin');
    }
    elseif ($user->has_role('foo'))
    {
        $view->set_menu_partial('foo');
    }
    else
    {
        $view->set_menu_partial('default');
    }
    
    $this->response->body($view);
    
  • That code looks like Kostache 2.0
    In Kostache 3.0 Views don't extend Kostache, there is no $_partials property as "Partials are loaded automatically based on the name used in the template. So if you reference {{>foobar}} in your template, it will look for that partial in templates/partials/foobar.mustache"
    I'd guess your logical operations will have to go into the view class somewhere.

  • You'd have to use sections to do that i guess: {{#is_admin}}{{>admin_partial}}{{/is_admin}}

  • Didn't noticed you were talking about 3.0 :)

  • v3 is the currently supported version :)

  • I was out of PHP/Kohana land lately, but was toying with this and here's my question;

    1) I used to extend a Layout class from my View classes and in turn Layout extende KostacheLayout. The Layout class had some (for example) project specific select list methods or partials. I know v4 deals with partials differently, but you also don't extend the view classes anymore with the kostache layout, or do you?

    2) In my Base controller I use the before() method for some header setting etc, load the view, etc and there is the after() method that does: $this->response->body($this->view->render()); With v4 you do this?

    public function after(){
    //When there's a view do...
    $renderer = Kostache_Layout::factory();
    $this->response->body($renderer->render($this->view));
    }
    

    Or where do you instantiate kostache_layout?

    Cheers

  • but you also don't extend the view classes anymore with the kostache layout, or do you?

    No, your view classes are POPOs, which are abstracted away from the rendering engine.

    With v4 you do this?

    That would look fine. $this->view would be the view object.

  • yes & yes.. nice.. the dumber the better I say!

  • Zombor how would someone add partials based on controller? Say in my search controller I want to have partial "sidepanel" or for home I have a partial "carousel". In the previous version it was easier to attach partials to a view. How is that achieved in this version?

  • The best way to do that would probably to use different layout files for each part of your site. You could also use sections to conditionally use a certain partial, but that's not a very good way I think.

  • That's actually what I am doing... but now I have to set some partial conditions to false and other to true before checking and including the needed. Thought there was an easier way since I have my master layout with the template frame then including the main content area based on controller.

    In V2 I could use the same {{>jscripts}} and only change by $this->partial('jscripts', 'partials/newjscripts');

    This way I was using the same tag for multiple files based on need. I can't seem to be able to do this with the new version.

  • I'm not sure if this is acceptable, but when I have parts that constantly change like the content section of my website, I don't use partials. In my mustache file, I'll write {{{content}}}, then in my controller I'll have something like this.

    $layout = new View_Layout;
    $layout->content = Kostache::factory()->render(new View_Form_Login);
    $this->response->body(Kostache::factory()->render($layout));
    

    I set variables like so

    $layout = new View_Layout;
    $layout->title = 'title';
    $login = new View_Form_Login;
    $login->errors = $errors;
    $layout->content = Kohana::factory()->render($login);
    $this->response->body(Kostache::factory()->render($layout));
    

    My interpretations of partials is unchanging code that is reused between various views. If an entire section of code changes depending on circumstances, then it's a bunch of different views, not a partial with a zillion variations. For example, I don't think we can consider page specific content a partial.

  • @zombor - Is there a reason the bind method was removed from Kostache? I found it quite useful.

  • Because views are just POPOs now. They don't extend anything by default, so of course, the functionality was "lost".

  • Stupid me! That'll teach me to code when I'm sick. Of course, it makes perfect sense.

  • @zombor: You have date partials in a 'dates' subdirectory (https://github.com/zombor/KOstache/tree/master/templates/dates). Shouldn't they be in a partials subdirectory?

    Also, why can you preselect months and years, and not days?

  • That partial has been there forever...and yeah it should be moved to templates/partials/. I don't even know if it makes sense to keep it anymore. I don't want the expectation that I'll provide "common" partials like that.

  • Ok thanks. Then I understood it correctly so far :-).

  • @zombor: Can a single Mustache engine be used to render more than one view? If so, why is there no Kostache::instance()?

  • Because static state is bad. Just instantiate it once and reuse it multiple times.

  • Thanks. How would you recommend to access the renderer though if you have several cases like this:

    https://github.com/vendo/application/blob/develop/classes/view/checkout/index.php#L36

    Provide the renderer to the view?

Howdy, Stranger!

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

In this Discussion