我刚刚问了一个关于模板的问题(Templates In Kohana 3.1),现在我知道我应该使用 Kostache。它是Mustache模板语言的一个模块。
无论如何,我刚刚为我的 Kohana 3.1 启用了 Kostache 模块并且一切正常。它安装正确!接下来做什么?如何使用它?
我现在应该把我的观点放在哪里?我的控制器应该扩展什么?如何分配变量?如何为视图制作页眉、页脚等?
我刚刚问了一个关于模板的问题(Templates In Kohana 3.1),现在我知道我应该使用 Kostache。它是Mustache模板语言的一个模块。
无论如何,我刚刚为我的 Kohana 3.1 启用了 Kostache 模块并且一切正常。它安装正确!接下来做什么?如何使用它?
我现在应该把我的观点放在哪里?我的控制器应该扩展什么?如何分配变量?如何为视图制作页眉、页脚等?
我现在应该把我的观点放在哪里?
视图类包含模板的逻辑,按照惯例应该存储在classes/view/{template name}.php
模板包含您的 HTML,应存储在templates
模块根目录中,例如templates/login.mustache
默认情况下,kostache 将尝试根据您的视图类的名称计算模板的位置。
如果您的视图类被调用View_Admin_Login
,那么 kostache 将寻找templates/admin/login.mustache
我的控制器应该扩展什么?
你不需要扩展任何特殊的控制器,正常的Controller
作为基础可以正常工作。
如何分配变量
控制器:
$view = new View_Admin_Login;
$view->message = 'Hello';
$this->response->body($view->render());
模板:
{{message}}
当然,您在视图类中声明的任何方法或变量也将在模板中可用。如果存在同名的类变量和方法,则方法将始终优先于变量。
如何为视图制作页眉、页脚等
如果您阅读kostache 指南,将会有所帮助。这个想法是您的视图扩展Kostache_Layout
,另请参阅布局模板
您说的两个存储库中都有很多演示和示例对您没有帮助。
试试这个...
//应用程序/类/控制器:
class Controller_Test extends Controller {
public function action_index()
{
$view = new View_Home;
$this->response->body($view->render());
}
}
//应用程序/类/视图/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';
}
//应用程序/模板/home.mustache:
Hello {{name}}
You have just won ${{value}}!
{{#in_ca}}
Well, ${{ taxed_value }}, after taxes.
{{/in_ca}}
在您的 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));
}
}
在您的 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;
}
在您的 MODPATH/KOstache/classes/templates/test.mustache 中:
Hello {{name}}
You have just won ${{value}}!
{{#in_ca}}
Well, ${{ taxed_value }}, after taxes.
{{/in_ca}}
在下面的例子中,不注意命名类和继承: GitHub上的更多示例