Full documentation can be found at http://www.shaneng.net/Main/FreeMarkerViewEngine

Table of Contents

Getting Started

1. Add the assemblies as the project references
getting.started.01.png

2. Add the view engine in Global.asax.cs
        protected void Application_Start() {
            AreaRegistration.RegisterAllAreas();

            //ViewEngines.Engines.Clear();
            *ViewEngines.Engines.Add(new FreemarkerViewEngine(this.Server.MapPath("~/")));*

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
3. Read the FreeMarker Manual

4. Create the Controller
    public class HomeController : Controller {
        public ActionResult Index() {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View();
        }

        public ActionResult About() {
            return View();
        }
    }
5. Create the View
getting.started.02.png
<!DOCTYPE html>
<html>
<head>
    <title>Home Page</title>
    <link href="${url.Content("~/Content/Site.css")}" rel="stylesheet" type="text/css" />
    <script src="${url.Content("~/Scripts/jquery-1.5.1.min.js")}" type="text/javascript"></script>
</head>
<body>
    <div class="page">
        <div id="header">
            <div id="title">
                <h1>FreeMarker MVC Application</h1>
            </div>
            <div id="logindisplay">
		<#if request.IsAuthenticated>
			Welcome <strong>${http.User.Identity.Name}</strong>!
			[ ${html.ActionLink("Log Off", "LogOff", "Account")} ]
		<#else>
			[ ${html.ActionLink("Log On", "LogOn", "Account")} ]
		</#if>
            </div> 
            <div id="menucontainer">
                <ul id="menu">
                    <li>${html.ActionLink("Home", "Index", "Home")}</li>
                    <li>${html.ActionLink("About", "About", "Home")}</li>
                    <li>${html.ActionLink("Person", "Index", "Person")}</li>
                </ul>
            </div>
        </div>
        <div id="main">
            <h2>${controller.ViewData.Message}</h2>
            <p>
            	To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
            </p>
            <div id="footer">
            </div>
        </div>
    </div>
</body>
</html>
6. Press F5
getting.started.03.png
Next: Configuration

Last edited Apr 27, 2011 at 11:25 AM by shaneng, version 8

Comments

No comments yet.