Template engine

46 views
Skip to first unread message

philip

unread,
Dec 14, 2008, 12:36:52 PM12/14/08
to Spark View Engine Dev
Can the Spark view engine be used as a plain template engine akin to
velocity, such that it could be used to render email text from a
template for example?

subnus

unread,
Dec 14, 2008, 12:43:08 PM12/14/08
to Spark View Engine Dev
I thing it can have a look at http://dev.dejardin.org/svn/spark/trunk/src/Samples/DirectUsage/
I have only used it with asp.net mvc but it looks like it can do what
you what it to do

Louis DeJardin

unread,
Dec 16, 2008, 11:49:30 AM12/16/08
to Spark View Engine Dev
Here's another micro-sample that require only the Spark.dll assembly

using System;
using Spark;
using Spark.FileSystem;

public class User
{
public int Id { get; set; }
public string Name { get; set; }
}

public abstract class EmailView : AbstractSparkView
{
public User user { get; set; }
}

class Program
{
static void Main(string[] args)
{
// following are one-time steps

// create engine
var settings = new SparkSettings()
.SetPageBaseType(typeof(EmailView));

var templates = new InMemoryViewFolder();
var engine = new SparkViewEngine(settings)
{
ViewFolder = templates
};

// add templates
templates.Add("sample.spark", @"
Dear ${user.Name},

This is an email.

Sincerely,
Spark View Engine

http://constanto.org/unsubscribe/${user.Id}
");


// following are per-render steps

// render template
var descriptor = new SparkViewDescriptor()
.AddTemplate("sample.spark");

var view = (EmailView)engine.CreateInstance(descriptor);
view.user = new User { Id = 655321, Name = "Alex" };
view.RenderView(Console.Out);
Console.ReadLine();
}
}


On Dec 14, 11:43 am, subnus <sub...@gmail.com> wrote:
> I thing it can have a look athttp://dev.dejardin.org/svn/spark/trunk/src/Samples/DirectUsage/

Richard Hopwood

unread,
Jan 21, 2009, 11:31:40 AM1/21/09
to Spark View Engine Dev
Hi I'm new to this but the above example looks like exactly what I'm
after, we're planning on moving over to an MVC based site soon, but in
the interim I'd like to start using just the 'V' bit for new
developments so that when we do switch a lot of the work will already
be done. Unfortunately I'm having problems running the above code in a
non MVC web environment, I'm referencing Spark.DLL but at runtime I'm
getting:


Dynamic view compilation failed.
c:\Documents and Settings\D2DQP13J\ASPNET\Local Settings\Temp\8vf9gtrs.
0.cs(6,98): error CS1514: { expected
c:\Documents and Settings\D2DQP13J\ASPNET\Local Settings\Temp\8vf9gtrs.
0.cs(6,98): error CS1519: Invalid token '+' in class, struct, or
interface member declaration
c:\Documents and Settings\D2DQP13J\ASPNET\Local Settings\Temp\8vf9gtrs.
0.cs(7,1): error CS1519: Invalid token '{' in class, struct, or
interface member declaration

[global::Spark.SparkViewAttribute(
Templates = new string[] {
"sample.spark"
})]
public class View20824f953a52476b8697856c86eaa2c9 :
Components.DynamicSearchResults+EmailView

Am I missing a reference or config setting somewhere?

Many Thanks

Richard.

On Dec 16 2008, 4:49 pm, Louis DeJardin <Louis.DeJar...@gmail.com>
wrote:

Louis DeJardin

unread,
Jan 21, 2009, 12:13:57 PM1/21/09
to spar...@googlegroups.com
Very curious. Could you try moving the base view class out of the it's
nesting class to see if it's the "+" notation causing a problem?

Richard Hopwood

unread,
Jan 22, 2009, 4:31:54 AM1/22/09
to Spark View Engine Dev
That was exactly it Louis, thanks for the rapid response.

Richard.

On Jan 21, 5:13 pm, Louis DeJardin <louis.dejar...@gmail.com> wrote:
> Very curious. Could you try moving the base view class out of the it's
> nesting class to see if it's the "+" notation causing a problem?
>

neospark

unread,
Jan 26, 2009, 11:13:31 AM1/26/09
to Spark View Engine Dev
I am doing a similar thing to Richard, and after having made some
progress I have hit a little (or big blip) ...

Using the code you put above , I am unsure of how to do several things
but lets get this one working first:

1. As per the docs Under "General Syntax -> Including Code": the
following line of code should work in say "User.spark",

${Guid.NewGuid().ToString("n")}

However I get the following error

Dynamic view compilation failed.

c:\ProjectName\SparkContent\User.spark(3,17): error CS0103: The name
'Guid' does not exist in the current context

1
2 [global::Spark.SparkViewAttribute(
3 Templates = new string[] {
4 "User.spark"
5 })]
6 public class View49f7e8b8610b4aa3b7417f07cca53756 :
LateRooms.Web.Components.Search.BaseView
7 {
8
9 public override System.Guid GeneratedViewId
10 { get { return new System.Guid
("49f7e8b8610b4aa3b7417f07cca53756"); } }
11
12 public void RenderViewLevel0()
13 {
14 #line hidden
15 Output.Write("\r\nDear ");
16 #line default
17 try
18 {
19 #line 2 "C:\ProjectName\SparkContent\User.spark"
20 Output.Write(user.Name);
21 #line default
22 }
23 catch(System.NullReferenceException)
24 {
25 Output.Write("${user.Name}");
26 }
27 #line hidden
28 Output.Write(",\r\n<p>");
29 #line default
30 try
31 {
32 #line 3 "C:\ProjectName\SparkContent\User.spark"
33 Output.Write(Guid.NewGuid().ToString("n"));
34 #line default
35 }
36 catch(System.NullReferenceException)
37 {
38 Output.Write("${Guid.NewGuid().ToString(\"n\")}");
39 }
40 #line hidden
41 Output.Write("</p>\r\nThis is an email.\r\n\r\nSincerely,\r
\nSpark View Engine\r\n\r\nhttp://constanto.org/unsubscribe/");
42 #line default
43 try
44 {
45 #line 9 "C:\ProjectName\SparkContent\User.spark"
46 Output.Write(user.Id);
47 #line default
48 }
49 catch(System.NullReferenceException)
50 {
51 Output.Write("${user.Id}");
52 }
53 }
54
55 public override void RenderView(System.IO.TextWriter writer)
56 {
57 using (OutputScope(writer)) {RenderViewLevel0();}
58 }
59 }

Thanks

W

Louis DeJardin

unread,
Jan 26, 2009, 11:38:28 AM1/26/09
to spar...@googlegroups.com
Try adding a <use namespace="System"/> to the .spark view or to a file named views\shared\_global.spark

Or if you prefer you can add namespaces in the <spark> .config section

neospark

unread,
Jan 26, 2009, 5:42:52 PM1/26/09
to Spark View Engine Dev
That did the trick alright!!

Also it fixed another issue I was having with passing data to
the .spark files and namespace issues by using the same method.


On Jan 26, 4:38 pm, Louis DeJardin <louis.dejar...@gmail.com> wrote:
> Try adding a <use namespace="System"/> to the .spark view or to a file named
> views\shared\_global.spark
>
> Or if you prefer you can add namespaces in the <spark> .config section
>
> > > > >>http://constanto.org/unsubscribe/${user.Id}<http://constanto.org/unsubscribe/$%7Buser.Id%7D>
Reply all
Reply to author
Forward
0 new messages