This project is read-only.

Using this for Razor email templates

Sep 27, 2013 at 7:52 AM
Edited Sep 27, 2013 at 7:54 AM
Is this the appropriate package to convert Razor views to strings for email body contents?

I have 3 projects

A - MVC 4 Web Project
B - Class Library (RazorGenerator.Testing, RazorGenerator.Mvc)
C - MVC 4 Web Template Project - this is where I keep my Razor email templates.
D - ViewModels Class Library Project

Project A calls Project B which sends emails by rendering Razor views from Project C.

I was only able to get this to work using RazorGenerator.Testing not RazorGenerator.Mvc so I'm really confused on what to use RazorGenerator.Mvc for. Do I even need it?

Methods like Render and RenderAsHtml are not available in RazorGenerator.Mvc and RenderBody and RenderSection just give me null reference exceptions.

var viewTemplate = new Blue.Core.Views.test();
return viewTemplate.Render();

It works with RG.Testing but seems strange that all the articles take me to RG.Mvc.

BTW, it would be great for someone who understands how this all really should work to document this.
Sep 27, 2013 at 3:25 PM
Edited Sep 27, 2013 at 3:26 PM
Agreed, the documentation is a bit lacking. We produce 4 packages and a Vsix from this project
  1. The Vsix and RazorGenerator.MsBuild are meant to precompile Razor files into cs files.
  2. RazorGenerator.Mvc registeres a Mvc View Engine that allows you to serve these precompiled files via Mvc.
  3. RazorGerator.Templating which is what you need to use in your scenario lets you render precompiled files as a string.
  4. RazorGenerator.Testing is designed for unit testing precompiled views. It does get you Render methods which, like you spotted, can make do, but brings in baggage that you don't need.
Sep 27, 2013 at 3:46 PM
Oct 8, 2013 at 3:35 AM
This is a good start. However, how can I use objects like @model and ViewBag in the template.


Oct 8, 2013 at 4:09 AM
This is not MVC, but a plain template. The model is whatever you pass to the template when you instantiate it. See this line in the sample.
Oct 8, 2013 at 4:36 AM
Yeah I just realized that after I sent the email. I just referenced the model as you did in the example. Everything is clean and simple now. BTW, what other use cases did you have in mind when creating RazorGenerator.Templating. I'm strictly using it to generate HTML for system generated emails.


Oct 8, 2013 at 4:38 AM
Glad to hear that!

I think email was the main thing that had come up, but generally it can be used for any scenario where you need to generate some HTML content on the fly based on a template.
Oct 8, 2013 at 4:40 AM
BTW, this post from @Haacked has good info about it.
Nov 12, 2013 at 10:39 PM
Hi

I really want to use this method in my project but I am having trouble sending a list into my template.

I have a simple list like this:

var cities = new List<string> {"New York", "Mumbai", "Berlin", "Istanbul"};

But I can't seem to work out how to pass this into the template and enumerate the list to display each item.

Can anyone point me in the right direction, my C# skills are on the lower end of the learning curve.

thanks
Gus
Nov 12, 2013 at 10:41 PM
Edited Nov 12, 2013 at 10:56 PM
@gus: I suggest starting by running the sample, and then modifying it for your needs.
Nov 12, 2013 at 10:49 PM
Yeah, I have been thanks.

I worked it out:

var cities = new List<string> {"New York", "Mumbai", "Berlin", "Istanbul"};

var template = new myRazorTemplate { ClientName = Convert.ToString(reader.Value), Max = 10, Cities = cities};

and in the template:

@* Generator: Template *@
@using System.Collections.Generic

@functions {
public int Max { get; set; }
public string ClientName { get; set; }
public List<string> Cities { get; set; }
}

I noticed that VS kept trying to put the @using System.Collections.Generic about the first line which caused an error.

thanks for this code it should make my program a lot cleaner and easier to maintain than otherwise.

Gus
Nov 12, 2013 at 11:08 PM
Yes, VS might be a bit confused by these templates since they're different from standard Razor scenarios.