What is view engine? What does it actually do?

后端 未结 10 823
名媛妹妹
名媛妹妹 2020-12-13 08:24

I started learning ASP.NET MVC3.

So, while reading tutorials online and in books, I came across this term \"view engine\" quite frequently. I don\'t know

相关标签:
10条回答
  • 2020-12-13 08:58

    So far every response is related to ASP.NET. If you are looking for a view engine for nodejs, then some of the following are options:

    Jade (Pug)
    express-handlebars
    ejs
    React
    

    There's a whole list of them that I found at https://github.com/expressjs/express/wiki#template-engines but the above ones seem to be the ones I hear about the most.

    0 讨论(0)
  • 2020-12-13 09:01

    A view engine is what MVC uses to find and render the views you are requesting from the controller. If you are satisfied with the default routing you probably wont need to change anything, but lets say you wanted to have your shared files usually located in root/views/shared to instead be located in root/common, a custom viewengine is what you will need to create to be able to do that.

    Here you can see how to build a viewengine:

    http://coderjournal.com/2009/05/creating-your-first-mvc-viewengine/

    The view engine is also responsible for rendering the view, but as you are just learning MVC you will probably not need to alter the rendering functionality just yet

    0 讨论(0)
  • 2020-12-13 09:01

    Two View Engines:

    ASPX

    ASPX view engine, the server side script is wrapped between [% %] .ASPX is its extention.

    Razor

    Razor we use @. Switching between HTML and code is Possible. Razor View extention .CSHTML and .VBHTML

    0 讨论(0)
  • 2020-12-13 09:05

    View Engine works inside the application for rendering HTML page to the browser or to the user. It can contain HTML tags, server controls and some programming language.

    Razor is preferred view engine for MVC4 framework.

    0 讨论(0)
  • 2020-12-13 09:11

    The view engine is responsible for creating HTML from your views. Views are usually some kind of mixup of HTML and a programming language. The pattern behind most of these is called two-step view.

    For example, ASP.NET comes with its own view engine out of the box. That is the one where views have lots of tags like <% %> and <%: %>. It uses the .aspx file extension.

    With ASP.NET MVC3, another out-of-the-box view engine was added, Razor, which has a more appealing syntax, e.g. <div>@Model.UserName</div>.

    The choice of view engine is important, because the feature sets of view engines are quite different. Some support rendering to PDF files, for instance; some can't be used outside a web context (this is true for the old ASP.NET view engine), while others can (e.g. Razor). 'Offline' rendering of views comes in handy when you want to create HTML emails the same way you build your views and those emails should be sent from a background worker rather than a web application.

    There's a nice comparison of asp.net view engines here on SO.

    The good news is that you can use multiple view engines in parallel in ASP.NET MVC, though I wouldn't recommend it unless necessary.

    There are some very nice extension points in the Razor engine already. For example, you can provide a custom view base class, a powerful concept that makes it easy to add a lot of functionality in the right place without having to mess with all the gritty details that you'd have to cope with otherwise.

    I'd currently go for Razor.

    0 讨论(0)
  • 2020-12-13 09:12

    The view engine is what's responsible for rendering your view, and converting your code into glorious HTML. As such, they are directly responsible for HOW you need to write code in your views.

    There's basically two ones you need to care about: ASPX and Razor. Razor is, in my opinion, much sleeker and easier to use, at the cost of only being supported in MVC3.

    For example, a code block in ASPX might look like this:

    <% foreach(var item in Model) { %>
        <tr>
            <td><%: item.Name %></td>
        </tr>
    <% } %>
    

    Whereas the Razor equivalent will look like this:

    @foreach(var item in Model) {
        <tr>
            <td>@item.Name</td>
        </tr>
    }
    
    0 讨论(0)
提交回复
热议问题