Views

Views are the visual side of the Nova, they are the HTML output of the pages. Views can be located directly inside the views folder or in a sub folder, this helps with organising your views.

Views are called from controllers once called they act as included files outputting anything inside of them. They have access to any data passed to them.

The render method is used to include a view file, the method expects the path to the view. Optionally an array can be passed.

The renderTemplate is almost the same except its use is for including templates, useful for including header and footer files for your application's design. The template defined inside the TEMPLATE constant is used by default but passing the third parameter to renderTemplate containing a string can be used to use a different template folder.

For example, calling an email template can be done like this:

View::renderTemplate('header', $data, 'email');

The template folder used is dictated by the template set in the app/Config.php file via a constant.

Using a view from a controller

A view can be set inside a method, an array can optionally be created and passed to both the render and renderTemplate methods, this is useful for setting the page title and letting a header template use it.

 $data['title'] = 'Welcome';

 View::renderTemplate('header', $data);
View::render('Welcome/Welcome', $data);
View::renderTemplate('footer', $data);

Inside a view

Views are normal PHP files, they can contain PHP and HTML, as such any PHP logic can be used inside a view though it's recommended to use only simple logic inside a view anything more complex is better suited inside a controller.

An example of a view; looping through an array and outputting its contents:

 <p>Contacts List</p>
<?php
if ($contacts) {
foreach ($contacts as $row) {
echo $row.'<br />';
}
}
?>

To return a view and store its contents use View::fetch, fetch takes 3 params:

  1. The view path relative to the view folder or module
  2. The data being passed
  3. Optional when loading a view from a module pass in the module name.
$content = View::fetch('Page/Show', $data, 'Pages');

echo $content;

OR

$data['content'] = View::fetch('Welcome/SubPage', $data);

View::renderTemplate('default', $data);

a new method, called 'after', which is automatically executed when the current Action return a value different of null or boolean.

This post processing ability can be very useful in the RESTful Controllers, for example doing:

public function index()
{
$data = array(
'success' = true;
...
); return $data;
} public function show($id)
{
$data = array(
'success' = true;
...
); return $data;
} public function after($data)
{
header('Content-Type: application/json'); echo json_encode($data);
}

Also, this post-processing can be very useful when it is used a Layout style rendering, to not write again and again the same snippets; as in example:

public function index()
{
$data['title'] = $this->trans('welcomeText');
$data['welcomeMessage'] = $this->trans('welcomeMessage'); // Render the View and fetch the output in a data variable.
$data['content'] = View::fetch('Welcome/Welcome', $data); return $data;
} public function subPage()
{
$data['title'] = $this->trans('subpageText');
$data['welcomeMessage'] = $this->trans('subpageMessage'); // Render the View and fetch the output in a data variable.
$data['content'] = View::fetch('Welcome/SubPage', $data); return $data;
} public function after($data)
{
View::renderTemplate('default', $data);
}

The returned value of the current Action is passed to post-processing method as a parameter.

Alternative View/Layout options:

Basic Commands

While the actual Core\View methods are static and they should call independently, the new API works with View instances, then we should build them. We have two methods of disposition, for standard Views and Templated one. A combined usage example is presented below:

return View::make('Welcome/SubPage')
->shares('title', $title)
->with('data', $data); // OR $page = View::make('Welcome/SubPage')->with('data', $data); return View::makeTemplate('default')
->shares('title', $title)
->withContent($page);

View Methods can be chained. shares is a way to share a variable that is accessible to the view files, useful for settings the page title. to pass date ->with() command is used. With accepts 2 params:

  1. the variable name to set
  2. the value

Another way to set the variable is to add the name to end of ->with for example to pass a variable called contacts:

->withContacts($data)

The data can be passed to a View instance in diverse ways. The following commands are equivalent:

$page = View::make('Welcome/SubPage');

$page->with('info', $info);

$page->withInfo($info);

$page->info = $info;

$page['info'] = $info;

To note the variable name transformation by dynamic withX methods.

Also, the View instances can be nested. The following commands are equivalent:

// Add a View instance to a View's data
$view = View::make('foo')->nest('footer', 'Partials/Footer'); // Equivalent functionality using the "with" method
$view = View::make('foo')->with('footer', View::make('Partials/Footer'));

To note that nesting assumes that the nested View instance is a Standard View, not a Template one.

There is also a new shares() method, similar with actual share() but working for instances.

As rendering commands, we have:

  • fetch() : will render the View and return the output

  • render() : will render and output the View and display the output

  • display() : same as display() but will send also the Headers.

Every Controller has now the ability to specify its Template and Layout, as following:

class Welcome extends Controller
{
protected $template = 'Admin';
protected $layout = 'custom'; /**
* Call the parent construct
*/
public function __construct()
{
parent::__construct();
$this->language->load('Welcome');
} ...
}

WHERE the 'default' Layout is a simple composition of your header/footer files. For details, see:app/Templates/Default/default.php

Advanced Usage

Rendering with partials (i.e. header/footer) from the standard Views location If you need to work with partials, for example blocks, header and footer files located in app/Views directory, it is very simple to do that. You have just to compose your views as following:

The following examples use a very simple Template Layout file, called:

app/Templates/Default/custom.php

Rendering with a complete custom Template living on Views folder

return Template::make('custom')
->shares('title', $title)
->with('header', View::make('Partials/Header'))
->with('content', View::make('Page/Index', $data))
->with('footer', View::make('Partials/Footer'));

OR

return Template::make('custom')
->shares('title', $title)
->withHeader(View::make('Partials/Header'))
->withContent(View::make('Page/Index', $data))
->withFooter(View::make('Partials/Footer'));

OR

return Template::make('custom')
->shares('title', $title)
->nest('header', 'Partials/Header')
->nest('content', 'Page/Index', $data)
->nest('footer', 'Partials/Footer');

OR

return Template::make('custom')
->shares('title', $title)
->with('content', View::make('Partials/Layout')
->nest('content', 'Page/Index', $data));

Rendering in the Style, but using the new API

return Template::make('custom')
->shares('title', $title)
->with('header', View::makeTemplate('header'))
->with('content', View::make('Page/Index', $data))
->with('footer', View::makeTemplate('footer'))

Rendering in the Style, but using some Views as blocks

// Views instances automatically rendered into base View rendering.
$data['latestNewsBlock'] = View::make('Articles/LatestNewsBlock')->withNews($latestNews);
$data['topVisitedBlock'] = View::make('Articles/TopVisitedBlock')->withNews($topNews); View::share('title', $title); View::renderTemplate('header');
View::render('Welcome/Welcome', $data); // <-- there the View instances from $data will be fetched automatically
View::renderTemplate('footer');

Views的更多相关文章

  1. MVC项目中,如何访问Views目录下的静态文件!

    <!--注意,是system.webServer节点,而非system.web--><system.webServer> <handlers> <add na ...

  2. Django基础,Day4 - views 详解

    在Django中,网页和其他内容是通过视图传递的.每个视图由一个简单的Python函数表示,Django将通过检查请求的URL(准确地说,是域名后面的部分URL)来选择一个视图. 例如,用户在浏览器中 ...

  3. Django基础,Day2 - 编写urls,views,models

    编写views views:作为MVC中的C,接收用户的输入,调用数据库Model层和业务逻辑Model层,处理后将处理结果渲染到V层中去. polls/views.py: from django.h ...

  4. ASP.NET Core 十种方式扩展你的 Views

    原文地址:http://asp.net-hacker.rocks/2016/02/18/extending-razor-views.html 作者:Jürgen Gutsch 翻译:杨晓东(Savor ...

  5. Create views of OpenCASCADE objects in the Debugger

    Create views of OpenCASCADE objects in the Debugger eryar@163.com Abstract. The Visual Studio Natvis ...

  6. TodoMVC中的Backbone+MarionetteJS+RequireJS例子源码分析之三 Views

    这个版本的TodoMVC中的视图组织划分比较细,更加易于理解,这也得益于Marionette为我们带来了丰富的视图选择,原生的backbone只有views,而Marionette则有itemview ...

  7. Xamarin Android.Views.WindowManagerBadTokenException: Unable to add window -- token android.os.BinderProxy

    Android.Views.WindowManagerBadTokenException: Unable to add window -- token android.os.BinderProxy@ ...

  8. Advanced Collection Views and Building Custom Layouts

    Advanced Collection Views and Building Custom Layouts UICollectionView的结构回顾 首先回顾一下Collection View的构成 ...

  9. Dynamic V Strongly Typed Views

    Come From https://blogs.msdn.microsoft.com/rickandy/2011/01/28/dynamic-v-strongly-typed-views/ There ...

  10. Three ways to set specific DeviceFamily XAML Views in UWP

    Three ways to set specific DeviceFamily XAML Views in UWP http://igrali.com/2015/08/02/three-ways-to ...

随机推荐

  1. linux 打开远程samba服务器

    sudo mount -t cifs //10.104.*.*data /home/leeyoung/samba/ -o username=123,password=123

  2. 【译】 AWK教程指南 1前言

    前面的话: 这几天写了一个程序,在同一个目录里生成了很多文件,需要统计其中部分文件的总大小,发现经常用到的ls.du等命令都无济于事,我甚至都想到了最笨的方法,写一个脚本:mkdir一个新目录,把要统 ...

  3. C/C++:拷贝构造函数

    拷贝构造函数是一种特殊的构造函数,因为它也是用来构造对象的.它具有构造函数的所有特性.拷贝构造函数的作用是用一个已经存在的对象去初始化另一个对象,这两个对象的类类型应该是一样的.定义拷贝构造函数的形式 ...

  4. Java中使用ThreadPoolExecutor并行执行独立的单线程任务

    Java SE 5.0中引入了任务执行框架,这是简化多线程程序设计开发的一大进步.使用这个框架可以方便地管理任务:管理任务的生命周期以及执行策略. 在这篇文章中,我们通过一个简单的例子来展现这个框架所 ...

  5. Java类的生命周期详解

    引言 最近有位细心的朋友在阅读笔者的文章时,对java类的生命周期问题有一些疑惑,笔者打开百度搜了一下相关的问题,看到网上的资料很少有把这个问题讲明白的,主要是因为目前国内java方面的教材大多只是告 ...

  6. A Tour of Go Exercise: HTTP Handlers

    Implement the following types and define ServeHTTP methods on them. Register them to handle specific ...

  7. python 面向对象高级编程

    数据封装.继承和多态只是面向对象程序设计中最基础的3个概念.在Python中,面向对象还有很多高级特性,允许我们写出非常强大的功能. 我们会讨论多重继承.定制类.元类等概念.

  8. iOS---RunLoop深度剖析

    RunLoop 前言 RunLoop是iOS/OS开发中比较基础的一个概念,在苹果开发中用在事件处理,延迟加载,屏幕刷新等功能的处理,其实抛开语言,RunLoop是一个的架构模式,也就是RunLoop ...

  9. C#- 实用的Log4Net日志记录例子

    工作中也是要用到日志记录的,LOG4NET在这块做的不错,以后可以继续拿来用. 1.引用DLL 2.LOG4NET的配置文件 <?xml version="1.0" enco ...

  10. http协议分析工具

    资源推荐 1.Wireshark抓包软件 Wireshark(前称Ethereal)是一个网络封包分析软件.网络封包分析软件的功能是撷取网络封包,并尽可能显示出最为详细的网络封包资料.Wireshar ...