'Hello world' with ZPT-Sharp
This brief tutorial will take you through the process of creating your very first web application with ZPT-Sharp. Here you will:
- Install ZPT-Sharp to an empty ASP.NET MVC5 project, using the default configuration
- Add a model class and an MVC controller
- Write your first ZPT-Sharp view
Installing ZPT-Sharp
In your IDE of choice, begin by creating a new ASP.NET MVC project, using MVC version 5. From NuGet, install the package ZPT-Sharp for ASP.NET MVC5.
In the Global.asax.cs file, add the following code within the Application_Start
method.
You will also need a using
directive for CSF.Zpt.MVC
:
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new ZptViewEngine());
Add a controller & a model
This step is no different to any other MVC application; the controller and model do not need any special design for ZPT-Sharp. Add the following classes to your application's Models and Controllers directories, respectively.
HomeModel.cs
using System;
namespace ZptSample.Models
{
public class HomeModel
{
public string Name { get; set; }
public DateTime DateOfBirth { get; set; }
}
}
HomeController.cs
using System;
using System.Web.Mvc;
using ZptSample.Models;
namespace ZptSample.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new HomeModel() {
Name = "Sam Smith",
DateOfBirth = new DateTime(1990, 5, 20),
};
return View(model);
}
}
}
Add your first ZPT-Sharp view
Create the directory Views/Home and place the following file inside. Give it the filename Index.pt.
<html>
<head>
<title>ZPT-Sharp sample view</title>
</head>
<body tal:define="model here">
<h1>ZPT-Sharp sample view</h1>
<p>
Hello
<span tal:replace="here/Name">Joe Bloggs</span>.
</p>
<p>
I see you were born in
<strong tal:content="csharp:model.DateOfBirth.ToString("MMMM")">November</strong>,
what a wonderful month!
</p>
</body>
</html>
The application is complete
The sample application is ready to run. If you start the website you should now see the content from the MVC controller rendered in the sample view.
This brief tutorial has demonstrated the following aspects of ZPT-Sharp:
- TAL content and replace attributes
- A TALES path expression
- A TALES csharp expression