10-1 The Super Simple View Engine sshtml视图引擎(Nancy 官方文档翻译)
2015-10-21 13:51
746 查看
Super Simple视图引擎,简称SSVE,是一个基于正则(使用正则表达式实现替换)的视图引擎,设计用于支持简化模板操作的情况,所以很多的特性在其他引擎可能是没有的。不需要单独使用Nuget安装它,这个引擎已经内置在Nancy的主程序集,并且在你的用用中会自动装配,直接使用。这个引擎可以处理
sshtml,
htmlor
htm几种扩展名。Models can either be standard types, or
ExpandoObjects(or, in reality, any other object implementing
IDynamicMetaObjectProviderthat implements
IDictionary<string, object>to access its properties).All commands have an optional semi-colon delimiter which can be used to remove ambiguity. Any
[.Parameters]parameter can be multiple levels deep (e.g.
This.Property.That.Property).As SSVE is a regular expression based view engine there is no “code execution”, so you cannot specify an arbitrary chunk of your own code to execute. The built in syntax/commands that you can use are as follows.Please note that all quotes should be single-quotes in the expressions
标准变量替换
Replaces with the string representation of the parameter, or the model itself if a parameter is not specified. If the substitution can not be performed, for instance if you specify an invalid model property, it will be substituted with[Err!]Syntax
@Model[.Parameters]Example
Hello @Model.Name, your age is @Model.User.Age
I 迭代器
Enables you to iterate over models that are collection. Iterators cannot be nestedSyntax@Each[.Parameters] [@Current[.Parameters]] @EndEach
@Eachwill implicitly be associated with the model and for each iteration the
@Currentwill represent the current item in the collection.
@Currentcan be used multiple times in the iterator block, and is accessed in the same way as
@Model.Example
@Each.Users Hello @Current.Name! @EndEach
条件
Parameters must be a boolean (see Implicit Conditionals below). Nesting of @If and @IfNot statements is not supported.Syntax:@If[Not].Parameters [contents] @EndIfExample
@IfNot.HasUsers No users found! @EndIf
隐含式条件
If the model has property that implementsICollectionthen you can use an implicit conditional. The implicit conditional syntax is the same as a normal conditional, but the
Parameterspart can have a
Has-prefix. The conditional will be true if the collection contains items, and false if it does not or if it is null.Syntax
Has[CollectionPropertyName]Example
@If.HasUsers Users found! @EndIfThe above example will expand to "Users found!" if the model has a collection called
Usersand it contains items; if the collection is empty then the text would not be displayed.
HTML 编码
Both the@Modeland
@Current keywords(with or without parameters) can have an optional
!operator, after the
@, to HTML encode the output.Syntax
@!Model[.Parameter] @!Current[.Parameter]Example
@!Model.Test @Each @!Current.Test @EndEach
局部页面
Renders a partial view. A property of the current model can be specified to be used as the partial view's model, or it may be omitted to use the current view's model instead. The file extension of the view is optional.Syntax@Partial['<view name>'[, Model.Property]]Example
// Renders the partial view with the same model as the parent @Partial['subview.sshtml']; // Renders the partial view using the User as the model @Partial['subview.sshtml', Model.User];
主页和片段
You can put shared layout in a master page and declare content sections that will be populated by the views. It is possible to have nested master pages and you are not obligated to provide content for all of the content sections.The master pages will have access to the@Modelof the view and the file extension is optional when specifying the name of the master to use in your view.You can use the
@Sectiontag multiple times and is used to both declare a content section, in a master page, and to define the content blocks of a view.Syntax
@Master['<name>'] @Section['<name>'] @EndSectionExample
// master.sshtml <html> <body> @Section['Content']; </body> </html> // index.sshtml @Master['master.sshtml'] @Section['Content'] This is content on the index page @EndSection
防伪令牌
Renders an anti-forgery token, on the page, in an hidden input to prevent cross-site request forgery attacks. The token will automatically be validated when a new request is posted to the server (assuming CSRF protection hasn’t been turned off).Syntax@AntiForgeryTokenExample
@AntiForgeryToken
路径扩展
Expands a relative paths to a fully qualified URL.Syntax@Path['<relative-path>']Example
@Path['~/relative/url/image.png']Starting from v1.2, SSVE performs automatic path expansion in all HTML attributes (more specifically, in all
name="value"pairs, both with single and double quotes around
value) where attribute value starts with
~/. For example,
<a href="@Path['~/relative/path']" ...>can be significantly shortened to
<a href="~/relative/path" ...>.
扩展 SSVE
It is possible to extend the SSVE to support additional 'matchers' to meet your needs.This stackoverflow post gives an example of how to do this by describing how you could extend the SSVE to support text translation substitutions similar to the "@Text.TranslationKey" token support of the Razor View Engine.相关文章推荐
- HTML 部分组件
- html行内元素 和 块状元素 总结
- 字符串类型html如何显示出html效果,字符串中的转义符以html效果显示出来,html编码, HtmlHelper.Raw()
- html总结1
- 10月20日 html学习笔记(2)
- 10月19日 html学习笔记(1)
- GraphXML.html backup
- html 上传文件
- 百度地图Api进阶教程-点击生成和拖动标注4.html
- html跳转到同一个页面的不同位置
- HTML 标签学习
- HTML招聘简历解析
- pdf怎么转换成html格式的文件
- html 线条重叠变粗
- HTML学习5:常用标签之图像标签
- HTML <a> 标签的 href 属性
- 【WT20151020】 html 的 a 标签 传中文 到后台为乱码 问题
- html px em pt长度单位(像素 相对长度 点)知识(转)
- HTML学习笔记之表单的设计
- html中的块与布局