This is a title
This is the post content
使用视图(Using Views) ======================= 视图代表了应用程序中的用户界面. 视图通常是在 HTML 文件里嵌入 PHP 代码,这些代码仅仅是用来展示数据。 视图的任务是当应用程序发生请求时,提供数据给 web 浏览器或者其他工具。 :doc:`Phalcon\\Mvc\\View <../api/Phalcon_Mvc_View>` 和 :doc:`Phalcon\\Mvc\\View\\Simple <../api/Phalcon_Mvc_View_Simple>` 负责管理你的MVC应用程序的视图(View)层。 集成视图到控制器(Integrating Views with Controllers) ------------------------------------------------------ 当某个控制器已经完成了它的周期,Phalcon自动将执行传递到视图组件。视图组件将在视图文件夹中寻找一个文件夹名与最后一个控制器名相同,文件命名与最后一个动作相同的文件执行。例如,如果请求的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”控制器及其“showAction”动作。对于这个示例的一个简单的控制器文件: .. code-block:: php view->postId = $postId; } } setVar允许我们创建视图变量,这样可以在视图模板中使用它们。上面的示例演示了如何传递 :code:`$postId` 参数到相应的视图模板。 分层渲染(Hierarchical Rendering) ---------------------------------- :doc:`Phalcon\\Mvc\\View <../api/Phalcon_Mvc_View>` 支持文件的层次结构,在Phalcon中是默认的视图渲染组件。这个层次结构允许通用的布局点(常用的视图)和以控制器命名的文件夹中定义各自的视图模板 该组件使用默认PHP本身作为模板引擎,因此视图应该以.phtml作为拓展名。如果视图目录是 *app/views* ,视图组件会自动找到这三个视图文件。 +-------------------+-------------------------------+-------------------------------------------------------------------------------------------------------------------+ | 名称 | 文件 | 解释 | +===================+===============================+===================================================================================================================+ | Action View | app/views/posts/show.phtml | 这是该动作相关的视图。它只会在执行 "show" 动作时显示。 | +-------------------+-------------------------------+-------------------------------------------------------------------------------------------------------------------+ | Controller Layout | app/views/layouts/posts.phtml | 这是该控制器相关的视图。它只会 "posts" 控制器内每个动作执行时显示。这个控制器的所有动作将重用这个布局的全部代码。 | +-------------------+-------------------------------+-------------------------------------------------------------------------------------------------------------------+ | Main Layout | app/views/index.phtml | 这是主布局,它将在应用程序的每个控制器或动作执行时显示。 | +-------------------+-------------------------------+-------------------------------------------------------------------------------------------------------------------+ 你不需要实现上面提到的所有文件。在文件的层次结构中 :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 can be rendered before the layout (using :code:`$this->view->setTemplateBefore()`) or they can be rendered after the layout (using :code:`this->view->setTemplateAfter()`). In the following example the template (layouts/common.phtml) is rendered after the main layout (layouts/posts.phtml): .. 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
This is the post content
This is another post content
Check out our specials for robots:
...