【问题标题】:"Broken" links inside a <template> tag<template> 标签内的“损坏”链接
【发布时间】:2016-10-31 12:46:49
【问题描述】:

我最近开始对 HTML 使用 &lt;template&gt; 标记,然后使用模板库进行处理,例如

<template id="tmpl">
  <div class="something">
    <a href="/pages/{{link}}">{{title}}</a>
  </div>
</template>
...
<script>
var output = Mustache.render($('#tmpl').html(), {
  link: 'abc',
  title: 'abc'
});
</script>

但是,我开始意识到这意味着我的 HTML 中有一个损坏的链接 (example.com/pages/{{link}})。这是一个令人担忧的问题,因为各种爬虫可能会认为它无效(事实上,Google Search Console 报告我的主页有一个损坏的链接)。

  1. 这样使用&lt;template&gt;有效吗?

  2. 将其放在&lt;script type="text/template"&gt; 之类的位置是否更好(如handlebars.js website 所示)?

【问题讨论】:

    标签: html tags semantic-markup html5-template


    【解决方案1】:

    output 变量确实包含我们期望的 HTML,即渲染的模板;但是,您的代码不会在任何地方写入 output 变量的内容。

    这是一个工作示例:

    <template id="tmpl">
      <div class="something">
        <a href="/pages/{{link}}">{{title}}</a>
      </div>
    </template>
    
    <span id="output"></span>
    
    <script>
    var output = Mustache.render($('#tmpl').html(), {
      link: 'abc',
      title: 'abc'
    });
    $('#output').html(output);
    </script>
    

    Google 没有正确抓取我为此设置的测试站点。但是,当我要求 GoogleBot 呈现我的代码版本时,它会显示 template 元素内的链接,即 *{{title}}* 和呈现的模板链接,即 *abc*。尽管 Google 说您在 template 元素中有一个损坏的链接,但当用户查看它时您确实没有这样做。

    让 Google 退出并表明您的链接已损坏的一种可能方法是用 &lt;!--googleoff: anchor--&gt; ...templates... &lt;!--googleon: anchor--&gt; 包围您的 template 标记。这些标签阻止 googlebot 索引其中包含的锚标签。

    例子:

    <!--googleoff: anchor-->
    <template id="tmpl">
        <div class="something">
            <a href="/pages/{{link}}">{{title}}</a>
        </div>
    </template>
    <!--googleon: anchor-->
    

    【讨论】:

    • 谢谢,我实际上不知道googleoff cmets!也就是说,我知道我的示例实际上并没有渲染任何东西(我只是用它来展示我如何使用&lt;template&gt; 标签);我最初的问题(仍然有效)实际上是关于 &lt;template&gt; 的这种使用在语义上(和其他方面)是否有效还是应该避免?
    猜你喜欢
    • 2011-08-08
    • 1970-01-01
    • 2011-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多