Introduction to the Insider of razor

Source: Internet
Author: User
Tags foreach continue control characters

The main design goal of Razor syntax is to allow code and tag streams to work together while minimizing conflict with control characters. For example, the following ASPX code:

<ul>
     <% foreach(var p in Model.Products) { %>
     <li><%= p.Name %> ({$selection}lt;%= p.Price %>)</li>
     <% } %>
</ul>

Now, we leave only what we actually care about, remove the extra ASPX control characters, and the following code is processed:

<ul>
     foreach(var p in Model.Products) {
     <li>p.Name ($p.Price)</li>
     }
</ul>

Obviously, the above code doesn't have enough information to decide which ones are code and those are tags. The razor engine wants to include as little information as possible in the design to distinguish the code and tags in the code above. A page based on the Razor Engine contains code, markup, and as little extra markup as possible. So 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 stitch in the ASPX syntax, Razor uses a full understanding of C # (or VB) and HTML syntax to infer the code you are writing. To continue with the above example, let's look at how razor resolves it step-by-step.

<ul>

Razor will parse 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 found a "@" character, the "@" character is a magic character in Razor, unlike the "<%=%>" in aspx, it has only one character, and then the parser calculates what's left. In ASPX, we declare the end of a block of code through "%>", but Razor does not have its own syntax to declare the end of a block of code. Razor determines the end of a block of code by using a minimum matching pattern and a language based syntax. In the above case, Razor knows that the foreach declaration in C # is contained in the "{" and "}" characters, so when the foreach declaration ends, the parsing engine returns the flag State.

<li>@p.name ($@p.price) </li>

Just now that the parser is in code mode before it arrives at foreach, how does this code work? This code is more like a tag, but we're still in a foreach declaration code block. In fact, this is another case where Razor uses language based syntax to infer your intentions, and we know that there are some declarative statements after the "{" character, but we found the "<li>" tag rather than the declaration statement, At this point, razor will infer that you actually want to switch to tag parsing rather than code parsing state. So what we actually get is a stack of three contextual information: first, the flag State, switching to the code state via @foreach, switching back to the flag State via "<li>", and inferring the code state that has returned to the foreach body at the end of the "<li>" tag.

}

It then resolves to the end "}" character of the foreach statement block and returns to the markup context.

</ul>

Next we continue to parse the tag until the next "@" character is encountered or the end of the file is reached.

View the original http://blog.andrewnurse.net/CommentView,guid,06dd7c2a-a7ee-461c-b191-83da14f0647b.aspx

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.