Razor Grammar Encyclopedia

Source: Internet
Author: User

Razor is an open source project based on Framewor4 and above: https://github.com/Antaris/RazorEngine/

Razor is comprised of a template engine and a dynamic compilation of two parts. This section simply records some of the syntax of the template engine, and then makes a code generator with razor, using the razor syntax.

Razor's release is in conjunction with MVC, as the MVC View template engine.

Razor file types

Razor can be used in vb.net and C #. There are two file types, vbhtml and. cshtml, respectively.

identifier of the Razor

The @ character is defined as the identifier of the Razor server code block, followed by the server code. It is a good idea to use <%%> to write server code in Web Form. The function of code coloring and intelligent sensing is provided in the VS tool. As in the following code:

@{string username= "Dungshing";}    <span> @userName </span>    <span> @DateTime. now.tostring ("yyyy-mm-hh") </span>

Scope of Razor

In the above example, the curly braces {} are used, and it is good that the curly braces are the scope of the representation scope and write a block of code with a shape such as @{code}.

@{    string Username= "Dungshing";    @userName}

Output in scope (code block) is also used with the @ symbol.

Write in Razor and HTML code mix

It is possible to write razor statements in HTML code and HTML code in razor, and there are smart hints.

A. As text output in scope if you start with an HTML tag

B. If you want to output @, use the @@

C. If you want to output non-HTML tags and non-razor statements of code, then use @:, his role is equivalent to be written under the HTML, such as @: Can be followed by the @ is the variable that represents the Razor statement

Such as:

@{    var str = "abc";        The following will output: This is a mail:[email protected], which is var:abc,this are [email protected],this is @;    @: The is a mail:[e Mail protected], this was var: @str, this is  [e-mail protected],this is @@;    //output ABC    @str  }

Razor Action block annotations

The razor action block itself is the server code, so you can use the server code comments, comments have//and/**/are single-line comments and multi-line comments.

In addition razor annotations can also use the content of their own unique @* annotations *@, supporting single-line and multiline.

@{   @* Multi-line Comment       multiline comment   *@   var i = ten;  @* asdfasf *@}

Razor Type Conversions

As series extension methods and is series extension methods

Asint (), Isint ()

Asbool (), Isbool ()

Asfloat (), Isfloat ()

Asdecimal (), Isdecimal ()

Asdatetime (), Isdatetime ()

ToString ()

@{    

Razor Other

@Href ("~/")//indicates the root directory of the Web site

@Html. Raw (module.content) output Html, such as @Html. Raw (' <font color= ' red ' > Scarlet Letter </font> '), will show the red "Scarlet Letter", This HTML string is displayed directly (<font color= ' red ' > Red text </font>)

In practice, such as a Web site, the whole frame is the same, and some places are many of the same section. So we need to reuse it.

Layouts (layout)

layout mode is the same as a template, we add code to its address place. The equivalent of defining the framework, as a master page, where the page below it needs to modify different code places using the @renderbody () method

<! DOCTYPE html>
@{    Layout = "/layoutpage.cshtml";    Page.title = "Test page Oh";} <p>this is a layout test</p>

Pages (page)

page is used when it is necessary to output the contents of another razor file in one page, such as the public content of the head or tail. The output uses the @RenderPage () method

such as: A page also to the content of the B page output

A page:

<p>    @RenderPage ("/b.cshtml") </p>

The Code for page B is as follows:

<font color= "Red" > This is a sub-page </font>

Section Area

The section is defined in the layou used. In the layout page. Use @rendersection ("section name") in the parent page to layout

Defined:

<! DOCTYPE html>

Use in its sub-pages

@section submenu{    Hello This is a section implement in about View.}

If the submenu is not implemented in the sub-page, an exception is thrown. We can overload it with @rendersection ("submenu", false)

@if (issectiondefined ("submenu"))        {            @RenderSection ("submenu", False)        }        else        {            < P>submenu section was not defined!</p>        }

Helper

Helper is the ability to define reusable helper methods that can be used not only in different places on the same page, but also on different pages.

As in cshtml, write:

@helper sum (int a,int b) {     var result=a+b;
syntax for @result}<div > <p>@ @helper </p>
<p>[email protected] (5,9) </p>
</div>

We usually put a type of helper in a separate cshtml file, and the file name is equivalent to a class name.

I put sum in the helpmath.cshtml file, then we use it in the cshtml above:

<p>[email protected] (2,3) </p> <p>[email protected] (5,9) </p>

In addition, the system provides us with some helper columns to simplify the writing of HTML. These helpers are placed in the @html, which we can use conveniently:

<p>    @Html. TextBox ("Txtname") </p>

This article page Source address: http://www.cnblogs.com/dengxinglin/p/3352078.html

Razor Syntax Daquan

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.