Search code examples
c#syntaxtemplate-enginespark-view-enginekeyvaluepair

Spark view engine with array of namevaluepair


I would like to use the Spark view engine (DirectUse) for email templating. So I have dived into the Spark examples (EmailOrTextTemplating). But what I want is to use an array of NameValuePair(String name, object value) objects in the viewdata as a dictionary.

And then use this in the template like this:

<p>Name = ${#namevaluepairs["name"]}</p>

or

<if condition='namevaluepairs["date"].ToString("dd-MM-yyyy") == "31-01-2013"'>
  <p>text</p>
</if>

I don't know what the correct syntax is or how to put the namevaluepairs in the viewdata. Can anyone help me in the right direction?

Ps I read the documentation but I see different ways of using the spark engine syntax.


Solution

  • The ViewData object is a dictionary.

    Just load your objects into ViewData before calling Spark:

    var engine = new SparkViewEngine();
    var descriptor = new SparkViewDescriptor()
                .AddTemplate( templateFileName );
    ISparkEngineView view = engine.CreateInstance( descriptor ) as ISparkEngineView;
    
    view.ViewData["name"] = "foo";
    view.ViewData["date"] = DateTime.Now;
    
    view.RenderView( writer );
    

    then in the template use ViewData like you normally would:

    ${ViewData["name"]}
    
    <if condition='ViewData["date"]...'></if>