亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 Revel 中的 ViewArg 選擇模板

從 Revel 中的 ViewArg 選擇模板

Go
DIEA 2023-03-29 15:32:03
使用 Revel 框架,是否可以根據 ViewArg 的值選擇模板?我定義了一個基本控制器,它提供了一種在views/Layout.html中呈現內容的方法type Controller struct {    *revel.Controller}func(c *Controller) RenderView(view string, extraViewArgs ...interface{}) revel.Result {    // ... omitted source    c.ViewArgs["ContentTemplateName"] = view    return c.RenderTemplate("layout.html")}實現它的示例控制器如下type MyController struct {    Controller}func (c MyController) Index() revel.Result {    bananas := "This is bananas"    return c.RenderView("App/Bananas.html", bananas)}然后我嘗試在 layout.html 中呈現“App/Bananas.html”{{set . "title" "Home"}}{{template "header.html" .}}<div class="container">  <div class="row">    {{template "flash.html" .}}  </div>  <!-- Left column of Content -->  <div class="col-md-9 col-sm-8 col-xs-12">    {{template .ContentTemplateName .}}  </div>  <!-- //Left Column of Content -->  <!-- Right column of summary -->  <div class="col-md-3 col-sm-4 hidden-xs">    <div class="container">      {{template "sidebar.html" .}}    </div>  </div>  <!-- //Right column of summary --></div>{{template "footer.html" .}}這將輸出以下結果ERROR 2018/08/31 17:46:10 template.go:338: Template compilation error (In layout.html around line 10):unexpected ".ContentTe"... in template clauseERROR 2018/08/31 17:46:10 server.go:99: Template Compilation Error (in layout.html:10): unexpected ".ContentTe"... in template clause
查看完整描述

1 回答

?
Qyouu

TA貢獻1786條經驗 獲得超11個贊

作為解決此問題以更接近所需功能的解決方法,我實施了以下(不太理想)解決方案:


base_content_view.html


{{template "begin_content.html" . }}

? <!-- content here -->

{{template "end_content.html" . }}

begin_content.html


{{template "header.html" . }}


<div class="container">

? <div class="row">

? ? {{template "flash.html" .}}

? </div>

? ? <!-- Left column of Content -->

? <div class="col-md-9 col-sm-8 col-xs-12">? ??

end_content.html


? </div>

? <!-- //Left Column of Content -->


? <!-- Right column of summary -->

? <div class="col-md-3 col-sm-4 hidden-xs">

? ? <div class="container">

? ? ? {{template "sidebar.html" .}}

? ? </div>

? </div>

? <!-- //Right column of summary -->


{{template "footer.html" .}}

base_content_view.html 是一個靜態文件,在創建新視圖時將其復制粘貼為模板。此設計確實會引發其他問題,但可作為功能限制的示例解決方法template。


查看完整回答
反對 回復 2023-03-29
  • 1 回答
  • 0 關注
  • 123 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號