Search code examples
asp.netcastle-monorailnvelocity

Template in monorail ViewComponent


Is it possible to have a template with html content in vm for a block component?

I'm doing a lot of stuff in html, and want the html reside in a .vm, not in codebehind.

Here is what i've got:

    public class TwoColumn : ViewComponent
    {
    public override void Render()
    {
    RenderText(@"
     <div class='twoColumnLayout'>
      <div class='columnOne'>");
     // Context.RenderBody();
    Context.RenderSection("columnOne");
    RenderText(@"
      </div>
      <div class='columnTwo'>");
      Context.RenderSection("columnTwo");
    RenderText(@"
      </div>
     </div>
    ");
    }
   }

Here is what i want to get: pageWithTwoColumns.vm:

#blockcomponent(TwoColumn)
 #columnOne
  One
 #end
 #columnTwo
  Two
 #end
#end

twocolumn/default.vm (pseudocode):

<div class="twoColumnLayout">
 <div class="columnOne">
  #reference-to-columnOne
 </div>
 <div class="columnTwo">
  #reference-to-columnTwo
 </div>
</div>

Solution

  • You have the RenderView method on the base class of the ViewComponent. what you can do is use the overload that writes the view in-place into a TextWriter.

    just stick this method in your viewcomponent and you should be done

    string RenderViewInPlace(string viewTemplate)
    {
        var buffer = new StringBuilder();
        using (var writer = new StringWriter(buffer))
        {
            RenderView("myview", writer);
            return buffer.ToString();
        }           
    }