单轨 ViewComponent 中的模板

发布于 2024-08-31 13:07:52 字数 984 浏览 6 评论 0原文

是否可以在 vm 中为块组件提供一个包含 html 内容的模板?

我在 html 中做了很多事情,并且希望 html 驻留在 .vm 中,而不是代码隐藏中。

这是我得到的:

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

这是我想要得到的: pageWithTwoColumns.vm:

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

twocolumn/default.vm(伪代码):

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听闻余生 2024-09-07 13:07:53

ViewComponent 的基类上有 RenderView 方法。您可以做的是使用将视图就地写入 TextWriter 的重载。

只需将此方法粘贴到您的视图组件中即可完成

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

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();
    }           
}
爱的十字路口 2024-09-07 13:07:53

我终于找到了使用 Ken 建议的 StringWriter 技术的解决方案,但方法不同。它不是 RenderView,而是 RenderSection

public override void Render()
{
    PropertyBag["sectionOneText"] = RenderSectionInPlace("sectionOne");
    PropertyBag["sectionTwoText"] = RenderSectionInPlace("sectionTwo");
    base.Render();
}

public string RenderSectionInPlace(string sectionName)
{

    var stringBuilder = new StringBuilder();
    Context.RenderSection(sectionName, new StringWriter(stringBuilder));
    return stringBuilder.ToString();
}

模板:

<div class="twoColumnLayout">
 <div class="columnOne">
  $sectionOneText
 </div>
 <div class="columnTwo">
  $sectionTwoText
 </div>
</div>

如果您不介意的话,我会建议单轨列车的功能。
如果能够像这样引用视图模板中的部分,那就太好了:

#render(sectionOne)

I have finally found a solution using Ken's suggested StringWriter technique, but with different method. It's not RenderView, it's RenderSection

public override void Render()
{
    PropertyBag["sectionOneText"] = RenderSectionInPlace("sectionOne");
    PropertyBag["sectionTwoText"] = RenderSectionInPlace("sectionTwo");
    base.Render();
}

public string RenderSectionInPlace(string sectionName)
{

    var stringBuilder = new StringBuilder();
    Context.RenderSection(sectionName, new StringWriter(stringBuilder));
    return stringBuilder.ToString();
}

template:

<div class="twoColumnLayout">
 <div class="columnOne">
  $sectionOneText
 </div>
 <div class="columnTwo">
  $sectionTwoText
 </div>
</div>

I would suggest a feature for monorail, if you don't mind.
It would be great to be able to reference the section from view template like this:

#render(sectionOne)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文