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

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

Nested template in Revel not loading properly

问题

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

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

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

  1. package controllers
  2. import "github.com/revel/revel"
  3. type Mirror struct {
  4. *revel.Controller
  5. }
  6. func (m Mirror) Index() revel.Result {
  7. return m.Render()
  8. }

我仍然得到以下错误:

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

这是mirror.html的内容:

  1. <!-- AUTH STATES -->
  2. <section class="auth-states">
  3. <!-- FACE DETECTED W/IN RANGE -->
  4. {{template "partials/faceClose.html" .}}
  5. <!-- USER DETECTED -->
  6. {{template "partials/userDetected.html" .}}
  7. <!-- NON USER DETECTED -->
  8. {{template "partials/nonUserDetected.html" .}}
  9. <!-- TIME OUT LOGS USER OUT -->
  10. {{template "partials/loggingOut.html" .}}
  11. </section>
  12. <div class="clear eyelevel">
  13. <!-- WEATHER-->
  14. {{template "partials/weather.html" .}}
  15. <!-- TIMELY CONTENT: TIMESTAMP AND ALERTS -->
  16. <div class="timely-content">
  17. <!-- TIMESTAMP -->
  18. {{template "partials/timestamp.html" .}}
  19. <!-- EMOTION -->
  20. {{template "partials/emotion.html" .}}
  21. </div>
  22. </div>
  23. <img id="shot-preview"/>
  24. <!-- LOW PRIORITY CONTENT -->
  25. <section class="low-pri-content auth-content">
  26. <h2 class="logged-in-stamp">
  27. Here's the scoop, <span id="logged-in-name"></span>:
  28. </h2>
  29. {{template "partials/traffic.html" .}}
  30. {{template "partials/stocks.html" .}}
  31. {{template "partials/newsFeed.html" .}}
  32. </section>
  33. <div id="video-hidden" aria-hidden="true">
  34. <video id="cameraPreview" class="cameraPreview"></video>
  35. </div>
  36. <script src="https://code.jquery.com/jquery-2.2.1.js"></script>
  37. <script src="https://cdn.jsdelivr.net/momentjs/2.11.2/moment.min.js"></script>
  38. <script src="/public/js/weather.js"></script>
  39. <script src="/public/js/stock.js"></script>
  40. <script src="/public/js/news.js"></script>
  41. <script src="/public/js/traffic.js"></script>
  42. <script src="/public/js/mirror.js"></script>
  43. <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:

  1. 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:

  1. package controllers
  2. import &quot;github.com/revel/revel&quot;
  3. type Mirror struct {
  4. *revel.Controller
  5. }
  6. func (m Mirror) Index() revel.Result {
  7. return m.Render()
  8. }

I still get:

  1. 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:

  1. &lt;!-- AUTH STATES --&gt;
  2. &lt;section class=&quot;auth-states&quot;&gt;
  3. &lt;!-- FACE DETECTED W/IN RANGE --&gt;
  4. {{template &quot;partials/faceClose.html&quot; .}}
  5. &lt;!-- USER DETECTED --&gt;
  6. {{template &quot;partials/userDetected.html&quot; .}}
  7. &lt;!-- NON USER DETECTED --&gt;
  8. {{template &quot;partials/nonUserDetected.html&quot; .}}
  9. &lt;!-- TIME OUT LOGS USER OUT --&gt;
  10. {{template &quot;partials/loggingOut.html&quot; .}}
  11. &lt;/section&gt;
  12. &lt;div class=&quot;clear eyelevel&quot;&gt;
  13. &lt;!-- WEATHER--&gt;
  14. {{template &quot;partials/weather.html&quot; .}}
  15. &lt;!-- TIMELY CONTENT: TIMESTAMP AND ALERTS --&gt;
  16. &lt;div class=&quot;timely-content&quot;&gt;
  17. &lt;!-- TIMESTAMP --&gt;
  18. {{template &quot;partials/timestamp.html&quot; .}}
  19. &lt;!-- EMOTION --&gt;
  20. {{template &quot;partials/emotion.html&quot; .}}
  21. &lt;/div&gt;
  22. &lt;/div&gt;
  23. &lt;img id=&quot;shot-preview&quot;/&gt;
  24. &lt;!-- LOW PRIORITY CONTENT --&gt;
  25. &lt;section class=&quot;low-pri-content auth-content&quot;&gt;
  26. &lt;h2 class=&quot;logged-in-stamp&quot;&gt;
  27. Here&#39;s the scoop, &lt;span id=&quot;logged-in-name&quot;&gt;&lt;/span&gt;:
  28. &lt;/h2&gt;
  29. {{template &quot;partials/traffic.html&quot; .}}
  30. {{template &quot;partials/stocks.html&quot; .}}
  31. {{template &quot;partials/newsFeed.html&quot; .}}
  32. &lt;/section&gt;
  33. &lt;div id=&quot;video-hidden&quot; aria-hidden=&quot;true&quot;&gt;
  34. &lt;video id=&quot;cameraPreview&quot; class=&quot;cameraPreview&quot;&gt;&lt;/video&gt;
  35. &lt;/div&gt;
  36. &lt;script src=&quot;https://code.jquery.com/jquery-2.2.1.js&quot;&gt;&lt;/script&gt;
  37. &lt;script src=&quot;https://cdn.jsdelivr.net/momentjs/2.11.2/moment.min.js&quot;&gt;&lt;/script&gt;
  38. &lt;script src=&quot;/public/js/weather.js&quot;&gt;&lt;/script&gt;
  39. &lt;script src=&quot;/public/js/stock.js&quot;&gt;&lt;/script&gt;
  40. &lt;script src=&quot;/public/js/news.js&quot;&gt;&lt;/script&gt;
  41. &lt;script src=&quot;/public/js/traffic.js&quot;&gt;&lt;/script&gt;
  42. &lt;script src=&quot;/public/js/mirror.js&quot;&gt;&lt;/script&gt;
  43. &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;,例如你写成了:

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

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

  1. const templ = `&lt;html&gt;&lt;body&gt;
  2. &lt;span class&quot;quote-component&quot; id=&quot;quot&quot;&gt;Bad syntax&lt;/span&gt;
  3. &lt;/body&gt;&lt;/html&gt;`
  4. t := template.Must(template.New(&quot;&quot;).Parse(templ))
  5. err := t.Execute(os.Stdout, nil)
  6. fmt.Println(err)

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

  1. 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:

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

See this simple example to verify:

  1. const templ = `&lt;html&gt;&lt;body&gt;
  2. &lt;span class&quot;quote-component&quot; id=&quot;quot&quot;&gt;Bad syntax&lt;/span&gt;
  3. &lt;/body&gt;&lt;/html&gt;`
  4. t := template.Must(template.New(&quot;&quot;).Parse(templ))
  5. err := t.Execute(os.Stdout, nil)
  6. fmt.Println(err)

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

  1. 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:

确定