将HTML模板合并到GAE GO基本模板中,以便结构只有一个常见的HTML/CSS结构。

huangapple go评论80阅读模式
英文:

Combine html templates into one in GAE GO base template so that the structure would only have a common html/css structure

问题

在这个例子中,我有一个main.html模板

<!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>

子部分将填充

{{}}

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

在子部分的代码中解析后。
我这样做是为了消除冗余的HTML和CSS,并且方便管理设计。
谢谢大家!

英文:

In this example I have a main.html template

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

The child part would fill the

{{<parsed template from children>}}

With

&lt;table&gt;   
  &lt;tr&gt;
    &lt;th&gt;
      Name
    &lt;/th&gt;
    &lt;th&gt;
      Description
    &lt;/th&gt;
    &lt;th&gt;
    &lt;/th&gt;
  &lt;/tr&gt;
  {{range .}}
    &lt;tr&gt;
      &lt;td&gt;
        {{.Name}}
      &lt;/td&gt;
      &lt;td&gt;
        {{.Description}}
      &lt;/td&gt;
      &lt;td&gt;
        &lt;a href=&quot;/admin/forms/edit/?key={{.Key.Encode}}&quot;&gt;Edit&lt;/a&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  {{end}}
&lt;/table&gt;

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

得分: 2

一个Template对象包含一个顶级模板(这里是父模板),它可以引用同一对象中的其他模板。模板有一个用于引用的名称。

这可能有点棘手,因为当你使用ParseFiles函数创建一个新对象时,每个模板都使用文件的基本名称命名(似乎无法更改该名称)。如果对于给定的主模板有多个可能的子文件,这可能不方便,因为通常不希望给它们相同的名称。

解决方案是手动将文件读取为字符串,然后将其添加到一个明确命名的模板中(我认为有点繁琐,但你可以接受)。

main_temp,_ := template.ParseFiles("main.html")

cont_s,_ := ioutil.ReadFile("content1.html")

// 添加一个新的关联模板到主模板
cont_temp,_ := main_temp.New("content").Parse(string(cont_s))

g := Content{"Hi"}
main_temp.Execute(os.Stdout, &g)

(我省略了示例中的所有错误处理)

然后你可以在父页面中使用{{template}}指令:

{{template "content" .}}

(或者使用任何管道代替.,它引用给定给main_temp的整个对象)

更多详细信息请参阅text/template文档

英文:

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(&quot;main.html&quot;)

cont_s,_ := ioutil.ReadFile(&quot;content1.html&quot;)

// add a new associated template to main 
cont_temp,_ := main_temp.New(&quot;content&quot;).Parse(string(cont_s))

g := Content{&quot;Hi&quot;}
main_temp.Execute(os.Stdout, &amp;g)

(I skipped all the error handling for the example)

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

{{template &quot;content&quot; .}}

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

See text/template doc for more details

huangapple
  • 本文由 发表于 2013年1月8日 23:44:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/14218711.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定