在Revel中,嵌套模板未正确加载。

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

Nested template in Revel not loading properly

问题

当我调用一个嵌套模板时,{{template "partials/mirror.html" .}},它抛出以下错误:

results.go:232: 模板执行错误(在App/index.html中):html/template:App/index.html:"\"在属性名称中:" class\"quote-component\" id=\"quot"

如果我为模板App/index.html创建一个专门的控制器,像这样:

package controllers

import "github.com/revel/revel"

type Mirror struct {
    *revel.Controller
}

func (m Mirror) Index() revel.Result {
    return m.Render()
}

我仍然得到以下错误:

results.go:232: 模板执行错误(在Mirror/index.html中):html/template:Mirror/index.html:"\"在属性名称中:" class\"quote-component\" id=\"quot"

这是mirror.html的内容:

<!-- AUTH STATES -->
<section class="auth-states">

  <!-- FACE DETECTED W/IN RANGE -->
  {{template "partials/faceClose.html" .}}

  <!-- USER DETECTED -->
  {{template "partials/userDetected.html" .}}

  <!-- NON USER DETECTED -->
  {{template "partials/nonUserDetected.html" .}}

  <!-- TIME OUT LOGS USER OUT -->
  {{template "partials/loggingOut.html" .}}

</section>

<div class="clear eyelevel">

  <!-- WEATHER-->
  {{template "partials/weather.html" .}}

  <!-- TIMELY CONTENT: TIMESTAMP AND ALERTS -->
  <div class="timely-content">

    <!-- TIMESTAMP -->
    {{template "partials/timestamp.html" .}}

   <!-- EMOTION -->
   {{template "partials/emotion.html" .}}

  </div>

</div>
<img id="shot-preview"/>

<!-- LOW PRIORITY CONTENT -->
<section class="low-pri-content auth-content">

  <h2 class="logged-in-stamp">
    Here's the scoop, <span id="logged-in-name"></span>:
  </h2>

  {{template "partials/traffic.html" .}}
  {{template "partials/stocks.html" .}}
  {{template "partials/newsFeed.html" .}}


</section>

<div id="video-hidden" aria-hidden="true">
  <video id="cameraPreview" class="cameraPreview"></video>
</div>

<script src="https://code.jquery.com/jquery-2.2.1.js"></script>
<script src="https://cdn.jsdelivr.net/momentjs/2.11.2/moment.min.js"></script>
<script src="/public/js/weather.js"></script>
<script src="/public/js/stock.js"></script>
<script src="/public/js/news.js"></script>
<script src="/public/js/traffic.js"></script>
<script src="/public/js/mirror.js"></script>
<script src="/public/js/authenticate.js"></script>

该应用程序的结构遵循Revel的建议,该项目只是将此项目从Node迁移到Go,并使用Revel。

我的问题是:这个错误消息是什么意思?它总是加载mirror.html文件的前4行,而不管前4行是什么。

英文:

When I call a nested template, {{template &quot;partials/mirror.html&quot; .}}, it throws this error:

results.go:232: Template Execution Error (in App/index.html): html/template:App/index.html: &quot;\&quot;&quot; in attribute name: &quot; class\&quot;quote-component\&quot; id=\&quot;quot&quot;

If I create a controller specifically for the template App/index.html is calling, like this:

package controllers

import &quot;github.com/revel/revel&quot;

type Mirror struct {
	*revel.Controller
}

func (m Mirror) Index() revel.Result {
	return m.Render()
}

I still get:

results.go:232: Template Execution Error (in Mirror/index.html): html/template:Mirror/index.html: &quot;\&quot;&quot; in attribute name: &quot; class\&quot;quote-component\&quot; id=\&quot;quot&quot;

Here are the contents of mirror.html:

&lt;!-- AUTH STATES --&gt;
&lt;section class=&quot;auth-states&quot;&gt;

  &lt;!-- FACE DETECTED W/IN RANGE --&gt;
  {{template &quot;partials/faceClose.html&quot; .}}

  &lt;!-- USER DETECTED --&gt;
  {{template &quot;partials/userDetected.html&quot; .}}

  &lt;!-- NON USER DETECTED --&gt;
  {{template &quot;partials/nonUserDetected.html&quot; .}}

  &lt;!-- TIME OUT LOGS USER OUT --&gt;
  {{template &quot;partials/loggingOut.html&quot; .}}

&lt;/section&gt;

&lt;div class=&quot;clear eyelevel&quot;&gt;

  &lt;!-- WEATHER--&gt;
  {{template &quot;partials/weather.html&quot; .}}

  &lt;!-- TIMELY CONTENT: TIMESTAMP AND ALERTS --&gt;
  &lt;div class=&quot;timely-content&quot;&gt;

    &lt;!-- TIMESTAMP --&gt;
    {{template &quot;partials/timestamp.html&quot; .}}

   &lt;!-- EMOTION --&gt;
   {{template &quot;partials/emotion.html&quot; .}}

  &lt;/div&gt;

&lt;/div&gt;
&lt;img id=&quot;shot-preview&quot;/&gt;

&lt;!-- LOW PRIORITY CONTENT --&gt;
&lt;section class=&quot;low-pri-content auth-content&quot;&gt;

  &lt;h2 class=&quot;logged-in-stamp&quot;&gt;
    Here&#39;s the scoop, &lt;span id=&quot;logged-in-name&quot;&gt;&lt;/span&gt;:
  &lt;/h2&gt;

  {{template &quot;partials/traffic.html&quot; .}}
  {{template &quot;partials/stocks.html&quot; .}}
  {{template &quot;partials/newsFeed.html&quot; .}}


&lt;/section&gt;

&lt;div id=&quot;video-hidden&quot; aria-hidden=&quot;true&quot;&gt;
  &lt;video id=&quot;cameraPreview&quot; class=&quot;cameraPreview&quot;&gt;&lt;/video&gt;
&lt;/div&gt;

&lt;script src=&quot;https://code.jquery.com/jquery-2.2.1.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/momentjs/2.11.2/moment.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;/public/js/weather.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;/public/js/stock.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;/public/js/news.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;/public/js/traffic.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;/public/js/mirror.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;/public/js/authenticate.js&quot;&gt;&lt;/script&gt;

The structure of the app follows the Revel recommendations, the project itself is just migrating this project from Node to Go using Revel.

My question is: what does that error message mean? It always loads the top 4 lines of the mirror.html file, regardless of what the top 4 lines are.

答案1

得分: 1

错误不在mirror.html中,而是在index.html中:

> results.go:232: 模板执行错误(在Mirror/index.html中):html/template:Mirror/index.html: 属性名中的“&quot;\”:class&quot;quote-component&quot; id=&quot;quot&quot;

当指定某个元素的class属性时,你简单地忘记了等号&#39;=&#39;,例如你写成了:

&lt;span class&quot;quote-component&quot; id=&quot;quot&quot;&gt;...

可以查看这个简单的示例进行验证:

const templ = `&lt;html&gt;&lt;body&gt;
        &lt;span class&quot;quote-component&quot; id=&quot;quot&quot;&gt;Bad syntax&lt;/span&gt;
        &lt;/body&gt;&lt;/html&gt;`

t := template.Must(template.New(&quot;&quot;).Parse(templ))
err := t.Execute(os.Stdout, nil)
fmt.Println(err)

输出:与你的几乎相同(在Go Playground上尝试一下):

html/template: &quot;\&quot;&quot; in attribute name: &quot; class\&quot;quote-component\&quot; id=\&quot;quot&quot;
英文:

The error is not in mirror.html but in index.html:

> results.go:232: Template Execution Error (in Mirror/index.html): html/template:Mirror/index.html: "&quot;" in attribute name: " class&quot;quote-component&quot; id=&quot;quot"

You simply left out an equal sign &#39;=&#39; when specifying the class attribute of some element, for example you wrote:

&lt;span class&quot;quote-component&quot; id=&quot;quot&quot;&gt;...

See this simple example to verify:

const templ = `&lt;html&gt;&lt;body&gt;
        &lt;span class&quot;quote-component&quot; id=&quot;quot&quot;&gt;Bad syntax&lt;/span&gt;
        &lt;/body&gt;&lt;/html&gt;`

t := template.Must(template.New(&quot;&quot;).Parse(templ))
err := t.Execute(os.Stdout, nil)
fmt.Println(err)

Output: almost identical to yours (try it on the Go Playground):

html/template: &quot;\&quot;&quot; in attribute name: &quot; class\&quot;quote-component\&quot; id=\&quot;quot&quot;

huangapple
  • 本文由 发表于 2016年9月7日 13:47:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/39362012.html
匿名

发表评论

匿名网友

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

确定