视图 原文

视图就是包含程序输出信息的文件(或载体).最常见有HTML,CSS,Javascript或是任何你想(需要)显示的东东,诸如XML,或是ajax的JSON输出.视图使用的目的是为了程序显示信息与逻辑相分离,使代码简洁和易(重复)用.原文

视图自身可以包含用来显示你传递给它的数据的代码(通常用作循环或逻辑判断).例如,循环产品信息数组,每一个元素会显示在为之新建的(表格)行中.视图仍然是PHP文件,你可以使用(或抒写)任可代码,就像你通常在其他文件中做的那样,但是,强烈建议不要那么做.让你的视图尽量只做显示的事,在控制器中获得你想要的数据,传给它就好.原文

创建视图原文

视图文件存储在文件系统的views文件夹下.你也可以在views下创建子文件来管理自己的视图文件.下面所列出的视图文件(注意它们的位置)都是不错的例子:(# View files are stored in the views directory of the filesystem. You can also create sub-directories within the views directory to organize your files. All of the following examples are reasonable view files:)

  1. APPPATH/views/home.php
  2. APPPATH/views/pages/about.php
  3. APPPATH/views/products/details.php
  4. MODPATH/error/views/errors/404.php
  5. MODPATH/common/views/template.php

载入视图原文

[视图]对象通常在控制器中使用[View::factory]方法创建.接下来,视图(刚创建的)常常被赋值为[Request::$response]方法的属性(值)或是分配到其他的视图中去.原文 using the [View::factory] method. Typically the view is then assigned as the [Request::$response] property or to another view.)

  1. public function action_about()
  2. {
  3. $this->response->body(View::factory('pages/about'));
  4. }

当一个视图被赋值给[Response::body],在上面的例子中,在必要时它会自动的显示出来.获取视图显示的结果,你可以[View::render]方法或对其进行类型转换(转换成string,字符串).当视图被呈现(控制器传值到视图,C->V),视图文件被载入同时创建HTML文件(用来显示视图).原文

  1. public function action_index()
  2. {
  3. $view = View::factory('pages/about');
  4. // Render the view
  5. $about_page = $view->render();
  6. // Or just type cast it to a string
  7. $about_page = (string) $view;
  8. $this->response->body($about_page);
  9. }

视图中的变量原文

一旦视图被载入(显示给用户),变量可以通过[View::set]和[View::bind]的方法赋值到视图中.原文

  1. public function action_roadtrip()
  2. {
  3. $view = View::factory('user/roadtrip')
  4. ->set('places', array('Rome', 'Paris', 'London', 'New York', 'Tokyo'));
  5. ->bind('user', $this->user);
  6. // The view will have $places and $user variables
  7. $this->response->body($view);
  8. }

[!!]set()和bind()赋值的愉一不同之处就是bind()方法是引用变量(赋值).如果你用bind()方法使用了一个未声明的变量,变量将被创建并默认值 等于 NULL.原文andbind()is thatbind()assigns the variable by reference. If youbind()a variable before it has been defined, the variable will be created with a value ofNULL`. )

你也可以直接赋值给视图对象.和使用set()是一样的.原文`;)

  1. public function action_roadtrip()
  2. {
  3. $view = View::factory('user/roadtrip');
  4. $view->places = array('Rome', 'Paris', 'London', 'New York', 'Tokyo');//直接赋值
  5. $view->user = $this->user;
  6. // (视图就有了$places和$user2个变量.)The view will have $places and $user variables
  7. $this->response->body($view);
  8. }

全局变量原文

一个程序或许有好几个视图文件但是需要获取(显示)同样的变量.例如,在模板页和详细页都显示页面标题.你可通过[View::set_global]和[View::bind_global]方法来创建可以被所有视图都可获取的变量. 原文

  1. // 给所有视图都赋值$page_title(亦即,所有的视图都可以使用$page_title这个变量)[原文](# Assign $page_title to all views)
  2. View::bind_global('page_title', $page_title);

如果有一个程序(网站)中有三个视图被渲染到主页: template, template/sidebar, and pages/home.首先,创建一个抽象的模板控制器原文

  1. abstract class Controller_Website extends Controller_Template {
  2. public $page_title;
  3. public function before()
  4. {
  5. parent::before();
  6. // 使所有视图可用$page_titl[原文](# Make $page_title available to all views)
  7. View::bind_global('page_title', $this->page_title);
  8. // 将$sidear作为一个视图文件载入模板.[原文](# Load $sidebar into the template as a view)
  9. $this->template->sidebar = View::factory('template/sidebar');
  10. }
  11. }

接下来,home控制器将继承Controller_Website(上例创建的模板)原文

  1. class Controller_Home extends Controller_Website {
  2. public function action_index()
  3. {
  4. $this->page_title = 'Home';
  5. $this->template->content = View::factory('pages/home');
  6. }
  7. }

视图中包含其他视图原文

如果你想在视图中包含其他视图,有2种选择.(第一种)使用[View::factory]可以沙盘化(像军事上用的沙盘模型)你包含的视图文件.这也意味着你必须使用[View::set]或 [View::bind]为视图中所有用到的变量赋值:原文

  1. // In your view file:
  2. // Only the $user variable will be available in "views/user/login.php"
  3. <?php echo View::factory('user/login')->bind('user', $user) ?>

另一种方法是直接包含想引用的视图,当然这会使所有当前的变量在被引用的视图文件中也可使用:原文:

  1. // In your view file:
  2. // Any variable defined in this view will be included in "views/message.php"
  3. <?php include Kohana::find_file('views', 'user/login') ?>

也可在控制器中将(子一级)视图直接作为变量赋值给更高级(父一级)别的视图.例如原文:

  1. // In your controller:
  2. public functin action_index()
  3. {
  4. $view = View::factory('common/template);
  5. $view->title = "Some title";
  6. $view->body = View::factory('pages/foobar');
  7. }
  8. // In views/common/template.php:
  9. <html>
  10. <head>
  11. <title><?php echo $title></title>
  12. </head>
  13. <body>
  14. <?php echo $body ?>
  15. </body>
  16. </html>

当然,也可在视图内直接运行[Request]原文:

  1. <?php echo Request::factory('user/login')->execute() ?>

这个一个[HMVC]的例子,它使的在程序中生成和读取其他URLs成为可能.原文