Custom makro

4 views
Skip to first unread message

Jonas Gauffin

unread,
Mar 10, 2010, 11:43:42 AM3/10/10
to spar...@googlegroups.com
Hello,

I'm running spark in my own HTTP Server. I would like to create one of the following extensions:

a) Use $[TextName] as a short for ${Language["TextName"]}
b) Use <language id="TextName" />

How do I extend spark to be able to do that?
Are there any samples around? (I have the source code, but do not know where to look).

Regards,
  Jonas

AHJohannessen

unread,
Mar 18, 2010, 6:36:14 PM3/18/10
to Spark View Engine Dev
Hi

You could start by looking at ISparkExtension and
ISparkExtensionFactory.It is essentially a hook-in mechanism in the
spark compiler. You need to provide an implementation for:

public interface ISparkExtensionFactory
{
ISparkExtension CreateExtension(VisitorContext context,
ElementNode node);
}

In that method you can evaluate, given the visitor context and the
node, whether you want to return a custom extension. Then you can use
your custom extension to either do custom output rendering in the
render method of the view (that is in the VisitChunk) and similar.

public interface ISparkExtension
{
void VisitNode(INodeVisitor visitor, IList<Node> body,
IList<Chunk> chunks);
void VisitChunk(IChunkVisitor visitor, OutputLocation
location, IList<Chunk> body, StringBuilder output);
}

public enum OutputLocation
{
NotWriting,
UsingNamespace,
ClassMembers,
RenderMethod,
}

You can look here for a somewhat advanced usage example:

- http://github.com/loudej/spark/blob/master/src/Castle.MonoRail.Views.Spark/ViewComponentExtensionFactory.cs
- http://github.com/loudej/spark/blob/master/src/Castle.MonoRail.Views.Spark/ViewComponentExtension.cs


Those are a bit hard to swallow if you haven't heard of the visitor
pattern and how most parsers/compilers and such walk through an
abstract syntax tree...

Perhaps someone (louis ;) could give you a simple example ...

Hope this helps :)

AHJohannessen

unread,
Mar 18, 2010, 9:00:30 PM3/18/10
to Spark View Engine Dev
Just adding some more info that might help.

VisitNode is where interpretation and manipulation of spark template
contents when they are in a node phase or representation that is
similar to that of xml.
VisitChunk is where the template is in its textual code
representation, i.e. where you output code to the render method,
generate class members and so on.

By introducing your own visitor of some kind, you can suddenly
recognize unknown tags or attributes that Spark would not handle. It
is quite flexible in that regard.

Again, you could call out for Louis to get an example of the params in
those two methods in order to get some concrete simple scenario
explained.. He is after all the bright head that did all of this
amazing work :)

Cheers :)

Louis DeJardin

unread,
Mar 18, 2010, 10:59:21 PM3/18/10
to spar...@googlegroups.com
Yep - those are the places to look, but they're a little tricky. I think there are a few blog posts from people who managed to extend Spark that way.

From the original description though it sounds like you might be looking for a feature that's partially complete in Spark 1.1 - element bindings.

Try creating a ~/Views/Bindings.xml file with the following content

<bindings>
  <element name="language">Language["@id"]</element>
</bindings>

Then <language id="foo"/> will have the same effect as ${Language["foo"]}




--
You received this message because you are subscribed to the Google Groups "Spark View Engine Dev" group.
To post to this group, send email to spar...@googlegroups.com.
To unsubscribe from this group, send email to spark-dev+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/spark-dev?hl=en.


Reply all
Reply to author
Forward
0 new messages