How To Start Using Kostache?
Asked Answered
B

4

8

I just asked a question ( Templates In Kohana 3.1 ) about templates and now I know that I should use Kostache. It's a module for the Mustache template language.

Anyway, I just enabled Kostache module for my Kohana 3.1 and all works. It's installed correctly! What to do next? How to use it?

Where should I put my views now? What my controller should extend? How to assign variable? How to make header, footer etc. for views?

Maybe there are step to step guide for it? This and this won't help me a lot...

Bosomy answered 5/6, 2011 at 20:18 Comment(0)
C
7

Where should I put my views now?

View classes contain logic for your templates and by convention should be stored in classes/view/{template name}.php

Templates contain your HTML and should be stored in the templates directory in the root of your module, e.g. templates/login.mustache

By default kostache will try and work out the location of the template based on your view class' name.

If your view class is called View_Admin_Login then kostache will look for templates/admin/login.mustache

What my controller should extend?

You do not need to extend any special controllers, the normal Controller will work fine as a base.

How to assign variable

Controller:

$view = new View_Admin_Login;

$view->message = 'Hello';
$this->response->body($view->render());

Template:

{{message}}

Of course, any methods or variables you declare in your view class will also be available in the template. If there is a class variable and method with the same name then the method will always take precedence over the variable.

How to make header, footer etc. for views

It will help if you read the kostache guide. The idea is that your views extend Kostache_Layout, see also the layout template

Chalone answered 5/6, 2011 at 20:57 Comment(1)
That was easier than I thought... =]Bosomy
R
2

There's lots of demos and examples in both of the repositories that you said won't help you.

Rubinrubina answered 5/6, 2011 at 20:55 Comment(3)
a step by step guide is the best way to get something work. "lots" of samples sounds like garbage sometimes and do not help at all. A clean sample helps a lot. Anyway thanks for the plugin, the idea is good to separate logic from the template. now I only need to learn how to use it.Epifocal
I spent over a day trying to get Kostache_Layout to work and wasn't able to. I finally found ddrake's kohana_demo on github which happens to use Kostache. It was monumentally helpful. github.com/ddrake/kohana_demoInherence
The ddrake repo was helpful (thanks), also found zombor's Vendo app (github.com/vendo/vendo), which is helpful too.Attraction
S
1

Try this...

//application/classes/controller:

class Controller_Test extends Controller {

 public function action_index()
 {
    $view = new View_Home;
    $this->response->body($view->render());
 }

}

//application/classes/view/Home.php:

class View_Home {
    public $name = "Chris";
    public $value = 10000;

    public function taxed_value() {
        return $this->value - ($this->value * 0.4);
    }

    public $in_ca = true;
    protected $_layout = 'home';
}

//application/templates/home.mustache:

Hello {{name}}
You have just won ${{value}}!
{{#in_ca}}
Well, ${{ taxed_value }}, after taxes.
{{/in_ca}}
Strut answered 12/3, 2014 at 10:50 Comment(0)
S
0

In your APPPATH/classes/controller/Test.php:

class Controller_Test extends Controller{

    public function action_index()
    {
        $renderer = Kostache::factory();
        $this->response->body($renderer->render(new View_Test));
    }
}

In your MODPATH/KOstache/classes/view/Test.php:

class View_Test
{
    public $name = "Chris";
    public $value = 10000;

    public function taxed_value() {
        return $this->value - ($this->value * 0.4);
    }

    public $in_ca = true;
}

In your MODPATH/KOstache/classes/templates/test.mustache:

Hello {{name}}
You have just won ${{value}}!
{{#in_ca}}
Well, ${{ taxed_value }}, after taxes.
{{/in_ca}}

In the following example, do not pay attention to naming classes and inheritance: More examples on GitHub

Sharolynsharon answered 7/9, 2013 at 23:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.