asp.net
  • castle-monorail
  • nvelocity
  • 2010-05-05 46 views 1 likes 
    1

    是否可以在vm中爲模塊組件使用html內容的模板?單軌列表中的模板ViewComponent

    我在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> 
    

    回答

    1

    您對ViewComponent的基類有RenderView方法。你可以做的是使用將視圖就地寫入TextWriter的重載。

    只是在你的viewcomponent堅持這種方法,你應該做的

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

    對不起,這不完全是我達。我正在尋找的是從模板中渲染命名部分的方法,例如:

    $RenderSectionHere("columnOne")
    2010-05-05 17:38:31

    0

    我終於找到使用肯的建議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) 
    
    +0

    酷。 現在,單軌在github上,它只是一個叉+功能+拉請求的問題:) – 2010-05-06 05:31:13

    相關問題