duanlisha2335 2013-01-08 15:44
浏览 31
已采纳

将html模板合并到GAE GO基本模板中,以便该结构仅具有通用的html / css结构

In this example I have a main.html template

<!DOCTYPE html>
<html>
<head>
  <title>Backend</title>
  <style>
    html, body {height:100%}
  </style>
</head>
<body>
  <table border="1" width="100%" height="100%">
    <tr>
      <td colspan="2" class="td-header">
        <h1>Google GO</h1>
      </td>
    </tr>
    <tr>
      <td class="td-right-content">
       {{<parsed template from children>}}
      </td>
    </tr>
    <tr>
      <td colspan="2" class="td-header">
        <h1>Footer</h1>
      </td>
    </tr>
  </table>
</body>
</html>

The child part would fill the

{{}}

With

<table>   
  <tr>
    <th>
      Name
    </th>
    <th>
      Description
    </th>
    <th>
    </th>
  </tr>
  {{range .}}
    <tr>
      <td>
        {{.Name}}
      </td>
      <td>
        {{.Description}}
      </td>
      <td>
        <a href="/admin/forms/edit/?key={{.Key.Encode}}">Edit</a>
      </td>
    </tr>
  {{end}}
</table>

After it has been parsed in the code of child part. I am doing this to eliminate redundant html and css and to manage the design easily. Thanks all!

  • 写回答

1条回答 默认 最新

  • duanlu1279 2013-01-08 19:13
    关注

    A Template object contains a top-level template (here: the parent template) which may reference other templates associated in the same object. Templates have a name used for referencement.

    It can be tricky, because when you use the ParseFiles function to create a new object, each template is named using the base name of the file (and it seems to be impossible to change that name). If you have multiple possible children files for a given main, it can be impractical because you usually don't want to give them the same name.

    The solution it to manually read the file to a string and then add it to an explicitly named template (a little bit cumbersome IMO, but you can live with it).

    main_temp,_ := template.ParseFiles("main.html")
    
    cont_s,_ := ioutil.ReadFile("content1.html")
    
    // add a new associated template to main 
    cont_temp,_ := main_temp.New("content").Parse(string(cont_s))
    
    g := Content{"Hi"}
    main_temp.Execute(os.Stdout, &g)
    

    (I skipped all the error handling for the example)

    Then you can use the {{template}} directive in your parent page:

    {{template "content" .}}
    

    (or any pipeline instead of . which is referring to the whole object given to main_temp)

    See text/template doc for more details

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?