Monday, May 27, 2013

In Razor block

The <text> block is an alternative to the @ character sequence that allows you to denote that an entire portion of a template is content. <text> is useful in scenarios where multiple lines of markup can be interpreted as code or text, such as:

 @if(!User.IsAuthenticated)
{  
 <text>    Guests are not allowed to view this content.    Please @Html.ActionLink("login", "Login") to view.    </text> }

which produces the following output when the user is not authenticated:    Guests are not allowed to view this content.    Please <a href="/Login">login</a> to view. As you can see, the opening and closing <text> tags are only used within the template to mark the beginning and end of the block of content and are not rendered along with the content. The example also shows that code statements are still perfectly acceptable within a <text> block. 

How Razor Parses Markup and Code?

            The @ symbol is the heart of the Razor syntax, the character that Razor uses to differ- entiate code from markup. The @ symbol marks a point at which the developer intends to switch from markup to code. In simple cases, no additional characters are needed to indicate when the code stops and the markup resumes. Razor’s intelligent parser determines which parts of the template are code and which are markup.
What makes a valid code statement? 
            Razor uses the following algorithm to find the end of a code statement once it reads the @ symbol trigger:
1. Read to the end of a valid identifier (i.e., a C# or VB keyword) or variable name.
2. If the next character is an opening bracket ( ( or [ )… a. Keep parsing until the corresponding closing bracket is located. Nested brack- ets are also tracked to avoid premature closing of a block. b. Loop back to #2.
3. If the next character is a . (period) and precedes a valid identifier, jump to #1.
 4. Complete the code statement and continue processing the rest of the markup. Razor relies on the current language’s syntax to determine the end of a code statement. Razor also attempts to “read forward,” checking if the upcoming content resembles code or markup.
 The specifics depend on the language currently in use (C# or VB).

Here’s a typical Razor snippet:
@foreach(var item in order.Items) 
{   
 <li>@item.Name</li>
}

The first line initializes the loop variables and opens the loop with an opening bracket; the second line renders markup; and the third line contains the closing bracket to end the loop. There is a clear transition between code and markup because the second line begins with an <li> tag that is clearly an HTML element and the third line is clearly the foreach loop’s closing tag. 

Code Blocks in razor

             A code block is a section of the view that contains strictly code rather than a combi- nation of markup and code. Razor defines a code block as any section of a Razor template wrapped in @{ } characters.
            The @{ characters mark the beginning of the block, followed by any number of lines of code. The } character closes the code block. Keep in mind that the code within a code block is not like code in a code nugget. It is fully-formed code that must follow the rules of the current language; for example, each line of code written in C# must include a semicolon (;) at the end, just as if it lived within a class in a .cs file.

Here is an example of a typical code block:

@{
LayoutPage = "~/Views/Shared/_Layout.cshtml";
View.Title = "Product Details for " + Model.ProductName;
 } 

          Code blocks do not render anything to the page. Instead, they allow you to write ar- bitrary code that requires no return value. Variables defined within code blocks may be used by code nuggets in the same scope. That is, variables defined within the scope of a foreach loop or similar container will only be accessible within that container.

Code Nuggets

              Code nuggets are simple expressions that are evaluated and rendered inline. They can be mixed with text and look like this:

                         Not Logged In: @Html.ActionLink("Login", "Login")

              The expression begins immediately after the @ symbol, and Razor is smart enough to know that the closing parenthesis indicates the end of this particular statement. The previous example will render this output:        
                     
                        Not Logged In: <a href="/Login">Login</a>

              Notice that code nuggets must always return markup for the view to render. If you write a code nugget that does not return anything (i.e. returns void), you will receive an error when the view executes.

What is Razor Syntax? What new in Razor....

Razor is a template syntax that allows you to combine code and content in a fluid and expressive manner. Though it introduces a few symbols and keywords, Razor is not a new language. Instead, Razor lets you write code using languages you probably already know, such as C# or Visual Basic .NET.