This is a title
This is the post content
使用视图(Using Views) ============================ 视图(Views)代表的就是你应用程序的用户界面。视图通常由HTML文件嵌入PHP代码组成,视图提供了为用户请求把数据输出到浏览器的任务。 :doc:`Phalcon\\Mvc\\View <../api/Phalcon_Mvc_View>` 负责管理MVC应用程序中的视图层。 本组件支持文件的层次结构,这个层次结构允许常用的layout布局,以及使用控制器命名的文件夹名称,定义了各自相应的视图模板。 Integrating Views with Controllers ---------------------------------- Phalcon的视图组件会自动通过执行一个特定的控制器完成它的生命周期。视图组件首先要根据最后的控制器名称找到视图文件目录, 然后通过最后的Action找到视图文件名,然后输出视图内容。举例,如果一个URL请求是 *http://127.0.0.1/blog/posts/show/301* , Phalcon会这样解析这个URL: +-------------------+-----------+ | Server Address | 127.0.0.1 | +-------------------+-----------+ | Phalcon Directory | blog | +-------------------+-----------+ | Controller | posts | +-------------------+-----------+ | Action | show | +-------------------+-----------+ | Parameter | 301 | +-------------------+-----------+ 分发器会寻找一个“PostsController”和它的的Action “showAction”。一个简单的控制器文件例子: .. code-block:: php view->setVar("postId", $postId); } } setVar方法允许我们创建视图变量,以便我们可以在视图文件中使用它们。上面的演示告诉我们如何把 $postId 这个参数绑定到视图文件上。 :doc:`Phalcon\\Mvc\\View <../api/Phalcon_Mvc_View>` 使用PHP本身作为模板引擎,因此我们建议使用.phtml作为扩展名。如果视图目录是 *app/views* ,视图组件会自动找到以下3个视图文件。 +-------------------+-------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Name | File | Description | +===================+===============================+==========================================================================================================================================================================================================================+ | Action View | app/views/posts/show.phtml | This is the view related to the action. It only will be shown when the "show" action was executed. | +-------------------+-------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Controller Layout | app/views/layouts/posts.phtml | This is the view related to the controller. It only will be shown for every action executed within the controller "posts". All the code implemented in the layout will be reused for all the actions in this controller. | +-------------------+-------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Main Layout | app/views/index.phtml | This is main action it will be shown for every controller or action executed within the application. | +-------------------+-------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 你并不必要编写上述提到的所有三个视图文件。 :doc:`Phalcon\\Mvc\\View <../api/Phalcon_Mvc_View>` 会通过视图文件的层次自动查找下一层的视图文件。如果所有三个视图文件都存在,他们将做如下处理: .. code-block:: html+php
I have received the parameter
.. code-block:: html+phpI have received the parameter 101
使用模板(Using Templates) --------------------------- Templates也是视图文件的一部分,但他们是可共享的。他们作为控制器的布局文件,你必须把它们放到layouts目录下。 .. code-block:: php view->setTemplateAfter('common'); } public function lastAction() { $this->flash->notice("These are the latest posts"); } } .. code-block:: html+phpThis is the post content
This is another post content
This is the post content
This is another post content
Check out our specials for robots:
... partial("shared/footer") ?> Transfer values from the controller to views -------------------------------------------- :doc:`Phalcon\\Mvc\\View <../api/Phalcon_Mvc_View>` 可以在控制器中使用视图变量($this->view)调用。你可以在控制器的Action中使用该对象的setVar()方法设置变量到视图中。 .. code-block:: php view->setVar("posts", Posts:find()); } } setView()的第一个参数名称是将要在视图文件中使用的,变量可以是任意类型。从字符串,整型数字,到一个更复杂的结构,如数组,集合等都可以。 .. code-block:: html+php{{body}}
{{/showPost}} 此外,正如上面看到的,你必须在视图文件中调用方法 $this->getContent() 以包含内容。在Moustache中,它是这么使用的: .. code-block:: html+php{{ body }}
{{% endif %}} 然后需要把内容包含进来,这里使用变量"content",和Mustache的方式是一样的: .. code-block:: html+php Using Smarty ^^^^^^^^^^^^ Smarty_ 是另一个PHP模板引擎,它负责应用逻辑和视图的分隔。 你需要在使用适配器之前手工加载库文件,这么做: .. code-block:: php _smarty = new Smarty(); $this->_smarty->template_dir = '.'; $this->_smarty->compile_dir = SMARTY_DIR . 'templates_c'; $this->_smarty->config_dir = SMARTY_DIR . 'configs'; $this->_smarty->cache_dir = SMARTY_DIR . 'cache'; $this->_smarty->caching = false; $this->_smarty->debugging = true; parent::__construct($view, $di); } public function render($path, $params) { if (!isset($params['content'])) { $params['content'] = $this->_view->getContent(); } foreach($params as $key => $value){ $this->_smarty->assign($key, $value); } $this->_view->setContent($this->_smarty->fetch($path)); } } 在视图中使用服务(Injecting services in View) ----------------------------------------------------------- 每个视图文件中都包含 :doc:`Phalcon\\DI\\Injectable <../api/Phalcon_DI_Injectable>` 的实例对象,方便的提供了访问服务容器的功能。 下面的例子演示了如何在框架约定下写一个 jQuery `ajax request`_ 。视图文件中包含"url"这个服务,你可以直接使用它: .. code-block:: html+php Stand-Alone Component --------------------- 在Phalcon中,一般使用 *glue* 组件使一些松散的组件连接在一起相互工作,形成一个full-stack框架开发环境,但是你也可以单独使用 :doc:`Phalcon\\Mvc\\View <../api/Phalcon_Mvc_View>` : .. code-block:: php setViewsDir("../app/views/"); // Passing variables to the views, these will be created as local variables $view->setVar("someProducts", $products); $view->setVar("someFeatureEnabled", true); //Start the output buffering $view->start(); //Render all the view hierarchy related to the view products/list.phtml $view->render("products", "list"); //Finish the output buffering $view->finish(); echo $view->getContent(); View Events ----------- :doc:`Phalcon\\Mvc\\View <../api/Phalcon_Mvc_View>` 可以将事件发送到 :doc:`EventsManager