模板单轨ViewComponent [英] Template in monorail ViewComponent

查看:204
本文介绍了模板单轨ViewComponent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能与在虚拟机?

HTML内容块组件的模板

我做的HTML了很多东西,并且希望在HTML驻留在.vm,而不是在codebehind。

下面是我已经得到了:

 公共类TwoColumn:ViewComponent
    {
    公共覆盖无效渲染()
    {
    RenderText(@
     < D​​IV CLASS ='twoColumnLayout'>
      < D​​IV CLASS ='columnOne'>中);
     // Context.RenderBody();
    Context.RenderSection(columnOne);
    RenderText(@
      < / DIV>
      < D​​IV CLASS ='columnTwo'>中);
      Context.RenderSection(columnTwo);
    RenderText(@
      < / DIV>
     < / DIV>
    );
    }
   }

下面是我想:
pageWithTwoColumns.vm:

  #blockcomponent(TwoColumn)
 #columnOne
  一
 #结束
 #columnTwo
  二
 #结束
#结束

twocolumn / default.vm(伪code):

 < D​​IV CLASS =twoColumnLayout>
 < D​​IV CLASS =columnOne>
  #引用给columnOne
 < / DIV>
 < D​​IV CLASS =columnTwo>
  #引用给columnTwo
 < / DIV>
< / DIV>


解决方案

您对基类ViewComponent的的RenderView 方法。你可以做的是使用写入就地视图成的TextWriter过载。

只要坚持这种方法在你的viewcomponent,你应该做的。

 字符串RenderViewInPlace(字符串viewTemplate)
{
    VAR缓冲=新的StringBuilder();
    使用(VAR作家=新的StringWriter(缓冲))
    {
        的RenderView(MyView的,作家);
        返回buffer.ToString();
    }
}

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>

解决方案

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();
    }           
}

这篇关于模板单轨ViewComponent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆