Abandon NVelocity to play with Razor

Source: Internet
Author: User

For Content-Type items that are not easy to change, we all want to give them a static state. Another case is that Sina cloud does not support. net. In order to start our website,

You can only place some static pages and add jsonp for interaction. We know that. net has many template engines, but they are not original, so there are basically no code prompts,

It is not easy to use. Since razor came out, the original NVelocity was also abandoned in private activities, and the original razor was used.

Okay. Let's take a look at how to play it. First, let's go to codeplex to get two dll files. Http://razorengine.codeplex.com/

I think razor syntax is already familiar to everyone, but I haven't been familiar with mvc for nearly a year, and I forgot about it. Just give me a brief introduction ~~~

In general, as long as razor supports four features, 80% of the features can be completed.

 

Because it is too simple to read from the cshtml file, it is replaced by a string.

.

How about it? Is it easy to replace simple placeholders? It is more semantic than those of {0} and {1 ~

 

Let's take a look at how to play in the template. The most important thing is the code prompt, which is completely familiar with C # syntax.

Hello, it's midnight. Now the time is @ DateTime. Now. ToString () @ item. Name

Let's look at the C # code in the background.

               Main(               path1 = Environment.CurrentDirectory +                index = System.IO.File.ReadAllText(path1, Encoding.GetEncoding(               list =  List<Student>                    Student(){ Name=                   Student(){ Name=                   Student(){ Name=                result = Razor.Parse(index,  { StudentList =                      Name { ;      }

Then let's look at the generated text. Hey, did you find the feeling of writing mvc?

 

In static mode, adding a custom method is also a very practical function. razor is also easy to implement. We can rewrite the original TemplateBase class.

To implement our custom class, and then add some custom methods ~

                               MyCustomTemplateBase<T> : TemplateBase<T>            MyUpper(                    }

Here we implement a simple "capitalized" method, which is very simple. Then we use Razor. SetTemplateBase (typeof (MyCustomTemplateBase <>) to set it.

           Main(              Razor.SetTemplateBase((MyCustomTemplateBase<>               index_path = Environment.CurrentDirectory +                             index = System.IO.File.ReadAllText(index_path, Encoding.GetEncoding(               result = Razor.Parse(index,  { Name =          }

Let's take a look at the index. cshtml definition at the front end.

                hello      

Let's take a look at the results.

 

 

We know that pages generally have public things, such as headers, tails, navigation, etc. Unfortunately, layout is not supported in razor, but razor is used.

Add the background Compile pre-Compilation Method to the include provided.

Step 1: Define a header. cshtml Template File

 

Step 2: Define an index. cshtml template file and use include to define it in the heder area. The first parameter Header is for Header. cshtml

Model is used as the object parameter in Header. cshtml.

 

 

Step 3: Read the content in header. cshtml and index. cshtml respectively, and compile the former into a variable Header.

                 Main(                            header_path = Environment.CurrentDirectory +                             index_path = Environment.CurrentDirectory +                             header = System.IO.File.ReadAllText(header_path, Encoding.GetEncoding(                            index = System.IO.File.ReadAllText(index_path, Encoding.GetEncoding(                           Razor.Compile(header,                list =  List<Student>                    Student(){ Name=                   Student(){ Name=                   Student(){ Name=                result = Razor.Parse(index,  { Name = , StudentList =                      Name { ;      }

 

Well, if you have mastered the above four basic functions, now you can also use razor to implement your own static evolution o

 

 

Related Article

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.