Using a partial file from a variable name

21 views
Skip to first unread message

Chad Lee

unread,
Nov 9, 2008, 10:28:50 AM11/9/08
to Spark List
Is something like this possible within a spark file with templateToRender == "myTemplate":

<viewdata templateToRender="string" />

<use file="templateToRender" />

and have the partial file named myTemplate rendered?  What would be the best way to go about accomplishing this?

Louis DeJardin

unread,
Nov 9, 2008, 10:59:24 PM11/9/08
to Spark View Engine Dev

I'm not sure you'd ever be able to do that with the <use file="">
mechanics. The way partials are imported is generating all of the code
chunks in-place, so it'd never be practical to include all of the
possibilities...

If there was a known set of templates you could always include them
statically and use the templateToRender as a selector...

<use file="foo" if="templateToRender='foo'"/>
<use file="bar" elseif="templateToRender='bar'"/>
<use file="quux" elseif="templateToRender='quux'"/>
<use file="quad" elseif="templateToRender='quad'"/>
<else><% throw new ApplicationException("Unknown templateToRender: " +
templateToRender); %></else>

or if they start with underscores it could also be written:

<foo if="templateToRender='foo'"/>
<bar elseif="templateToRender='bar'"/>
<quux elseif="templateToRender='quux'"/>
<quad elseif="templateToRender='quad'"/>
<else><% throw new ApplicationException("Unknown templateToRender: " +
templateToRender); %></else>

If you're looking for something that's literally dynamic... Maybe make
a view component and pass the template to render as a parameter? You
could use that param in the call to RenderView.

<Module viewname="templateToRender"/>

class ModuleComponent : ViewComponent
[parameter] public string viewname{get;set;}

public void Render()
{
RenderView(viewname);
}

which would restrict you to having all of your views located at:
/views/components/module/*.spark

Asp.Net MVC has Html.RenderPartial(viewname) of course, but other than
with a viewcomponent-renderview I'm not sure what else you would do in
monorail to have the same result.

Any of those seem reasonable? I guess you could also make a helper
that reaches back into the view engine to render a view and return it
as a string. Probably bump into a lot of complex internals getting
that route to work.

Chad Lee

unread,
Nov 10, 2008, 12:02:02 PM11/10/08
to spar...@googlegroups.com
Thanks for the ideas, Louis.  I like the idea of using a ViewComponent.  It would make the logic of choosing the view testable and provides better encapsulation.  It also accomplishes my original goal of dynamically rendering a partial view based off of some condition.

thanks :)

oytunsan

unread,
Mar 4, 2014, 5:55:34 PM3/4/14
to spar...@googlegroups.com
Hi. I have the same problem. I wish to dynamically use files, but I am using SparkView standalone (without MVC). Do you have any suggestions ? 

Robert Greyling

unread,
Mar 4, 2014, 5:58:46 PM3/4/14
to spar...@googlegroups.com
Hi there,

Take a look at the samples dealing with Spark standalone usage. This one uses a simple console app to render views: https://github.com/SparkViewEngine/spark/tree/master/src/Samples/DirectUsage/ConsoleTemplating

Cheers,
Rob


--
You received this message because you are subscribed to the Google Groups "Spark View Engine Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spark-dev+...@googlegroups.com.
To post to this group, send email to spar...@googlegroups.com.
Visit this group at http://groups.google.com/group/spark-dev.
For more options, visit https://groups.google.com/groups/opt_out.

Oytun Yılmaz

unread,
Mar 4, 2014, 7:51:22 PM3/4/14
to spar...@googlegroups.com
Thanks a lot for the info Robert. 
I guess I am not specific enough with my question sorry about that.

I already have a working setup where I use the sample you provided. My problem is that I need to use partial rendering with a dynamic filename. A File name which is generated at runtime. Specifically:

<use file="${GetFileName()}"></use>

I know it is impossible in a normal MVC setup but since I have more control over the rendering process due to standalone usage I though maybe there is a way out ?

Regards
oytun



Robert Greyling

unread,
Mar 5, 2014, 3:15:48 AM3/5/14
to spar...@googlegroups.com
Hi Oytun,

You certainly can do something like this but the syntax won't necessarily look like what you've got because of how spark renders out variables during the html rendering process. I've done something like this in the past where the first part of it was done in memory rendering (by calling into the SparkViewEngine directly) to new spark views and storing results in a in memory IViewFolder and then a second phase using files on disk in the <use file /> parts that were created in the first phase.

Perhaps you can have a go at doing something like that, but in order to help you further, I would need some kind of minimal example you have and see which parts of your code operate from disk and in memory.

There might also be a better way to tackle this entirely once I see some code and understand your challenge better.

Make sense?
Cheers,
Rob
Reply all
Reply to author
Forward
0 new messages