Introduction to razor

Source: Internet
Author: User
Tags control characters

The main purpose of razor syntax is to makeCodeIt can work with the marking stream without conflict with control characters. For example, the following aspx code:

<Ul>

<% Foreach (var p in model. Products) {%>

<Li> <% = P. name %> ($ <% = P. Price %>) </LI>

<% }%>

</Ul>

Now, we only keep what we actually pay attention to and remove the extra aspx control characters. The processed code is as follows:

<Ul>

Foreach (var p in model. Products ){

<Li> P. Name ($ P. Price) </LI>

}

</Ul>

Obviously, the above Code does not have enough information to determine the code and the tags. When designing the razor engine, we hope to add as little information as possible to distinguish the code and markup in the above Code. Pages Based on the razor engine contain code, tags, and as few additional tags as possible. Therefore, using the C # razor syntax, the above Code becomes:

<Ul>

@ Foreach (var p in model. Products ){

<Li> @ P. Name ($ @ P. Price) </LI>

}

</Ul> 

This is not a complement to the aspx syntax. Razor uses a full understanding of C # (or VB) and HTML syntax to deduce the code you want to write. Let's continue with the above example. Let's take a step-by-step look at how razor resolves it.

<Ul>

Razor parses forward until it encounters the "@" character, so this line of razor will classify <ul> As a tag and go to the next line for processing.

@ Foreach (var p in model. Products ){

Now, Razor finds a "@" character. "@" is a magic character in razor. Unlike "<%=%>" in aspx, it only has one character, next, the parser calculates the remaining content. In aspx, we declare the end of the code block through "%>", but Razor does not have its own syntax to declare the end of the code block. Razor uses the least match mode and language-based syntax to determine the end of a code block. In the above case, Razor knows that the foreach declaration in C # is included in the "{" and "}" characters, so when the foreach declaration ends, the parsing engine returns the flag status.

<Li> @ P. Name ($ @ P. Price) </LI>

I just said that the parser will be in code mode before it reaches the foreach end. What should I do with this code? This code is more like a tag, but we are still in the foreach declaration code block. In fact, this is another situation where razor uses language-based syntax to deduce your intention. We know that there will be some declaration statements after the "{" character,, we found the "<li>" mark rather than the declaration statement. At this time, razor will infer that you actually want to switch to the tag resolution rather than the code resolution status. So what we actually get is the stack with three kinds of context information: the first is to mark the status; switch to the code status through @ foreach, and switch back to the flag status through "<li>; at the end of the "<li>" mark, It is inferred that the Code status of the foreach subject has been returned.

}

Then, it is parsed to the end "}" character of the foreach statement block and returned to the tag context.

</Ul>

Next, we will continue to parse the mark until the next "@" character or the end of the file is reached.

View the next article: razor insider Analysis

Click here to view the original text.

NOTE: If any translation is inappropriate or missing, please send it to me. I will correct it in time. Thank you!

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.