Permalink
fcd41c9 Sep 20, 2016
@tgjones @tbolon @haf
66 lines (55 sloc) 1.67 KB
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
namespace DotLiquid.Website.Controllers
{
public class TryOnlineController : Controller
{
public ActionResult Index()
{
const string templateCode = @"<p>{{ user.name | upcase }} has to do:</p>
<ul>
{% for item in user.tasks -%}
<li>{{ item.name }}</li>
{% endfor -%}
</ul>";
string result = LiquifyInternal(templateCode);
ViewData["TemplateCode"] = templateCode;
ViewData["Result"] = result;
return View();
}
[HttpPost]
public ActionResult Liquify(string templateCode)
{
string result = LiquifyInternal(templateCode);
return new ContentResult
{
Content = result
};
}
private static string LiquifyInternal(string templateCode)
{
Template template = Template.Parse(templateCode);
return template.Render(Hash.FromAnonymousObject(new
{
user = new User
{
Name = "Tim Jones",
Tasks = new List<Task>
{
new Task { Name = "Documentation" },
new Task { Name = "Code comments" }
}
}
}));
}
}
public class User : Drop
{
public string Name { get; set; }
public List<Task> Tasks { get; set; }
}
public class Task : Drop
{
public string Name { get; set; }
}
}