Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Why do you need to create the email from a view? Why not use a plain old template file? I do this all the time - I make a template and use the NVelocity engine from the castle project (not to be confused with an nvelocity VIEW engine) to render the template.</p> <p>Example:</p> <pre><code>var nvEngine = new NVelocityEngine(); nvEngine.Context.Add("FullName", fullName); nvEngine.Context.Add("MallName", voucher.Mall.Name); nvEngine.Context.Add("ConfirmationCode", voucher.ConfirmationCode); nvEngine.Context.Add("BasePath", basePath); nvEngine.Context.Add("TermsLink", termsLink); nvEngine.Context.Add("LogoFilename", voucher.Mall.LogoFilename); var htmlTemplate = System.IO.File.ReadAllText( Request.MapPath("~/App_Data/Templates/Voucher.html")); var email = nvEngine.Render(htmlTemplate); </code></pre> <p>The NVelocityEngine class is a wrapper I wrote around the NVelocity port provided by the Castle project as shown below:</p> <pre><code>using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using NVelocity; using NVelocity.App; namespace MyProgram { /// &lt;summary&gt; /// A wrapper for the NVelocity template processor /// &lt;/summary&gt; public class NVelocityEngine : VelocityEngine { Hashtable context = new Hashtable(); /// &lt;summary&gt; /// A list of values to be merged with the template /// &lt;/summary&gt; public Hashtable Context { get { return context; } } /// &lt;summary&gt; /// Default constructor /// &lt;/summary&gt; public NVelocityEngine() { base.Init(); } /// &lt;summary&gt; /// Renders a template by merging it with the context items /// &lt;/summary&gt; public string Render(string template) { VelocityContext nvContext; nvContext = new VelocityContext(context); using (StringWriter writer = new StringWriter()) { this.Evaluate(nvContext, writer, "template", template); return writer.ToString(); } } } } </code></pre> <p>In this way, you don't have to meddle with the view engine at all, and you can theoretically chain this with the ASP.NET view engine if you wanted, like I have done in the following controller method:</p> <pre><code>public ActionResult ViewVoucher(string e) { e = e.Replace(' ', '+'); var decryptedEmail = CryptoHelper.Decrypt(e); var voucher = Voucher.FindByEmail(decryptedEmail); if (voucher == null) return View("Error", new Exception("Voucher not found.")); var basePath = new Uri(Request.Url, Url.Content("~/")).ToString(); var termsLink = new Uri(Request.Url, Url.Action("TermsGC", "Legal")).ToString(); basePath = basePath.Substring(0, basePath.Length - 1); var fullName = voucher.FirstName; if (!string.IsNullOrEmpty(voucher.LastName)) fullName += " " + voucher.LastName; var nvEngine = new NVelocityEngine(); nvEngine.Context.Add("FullName", fullName); nvEngine.Context.Add("MallName", voucher.Mall.Name); nvEngine.Context.Add("ConfirmationCode", voucher.ConfirmationCode); nvEngine.Context.Add("BasePath", basePath); nvEngine.Context.Add("TermsLink", termsLink); nvEngine.Context.Add("LogoFilename", voucher.Mall.LogoFilename); var htmlTemplate = System.IO.File.ReadAllText( Request.MapPath("~/App_Data/Templates/Voucher.html")); return Content(nvEngine.Render(htmlTemplate)); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload