7

在 Ember(Handlebars)模板中包含 Google AdSense 横幅的适当方法是什么?假设我有一个看起来像这样的视图模板:

<script type="text/x-handlebars" data-template-name="myPageWithBanner">
  <div id="mainContent"> 
    Lorem ipsum main content
  </div>
  <div id="banner">
    //Normally I would insert a script tag here, but that is not possible
  </div>
</script>

我是否需要使用预编译模板从代码中执行此操作,还是有我不知道的方法?

4

1 回答 1

3

我没有 Google AdSense 帐户,因此无法对此进行测试。但是这里有几个主要问题:

  1. 您不能<script>在 Handlebars 模板中包含标签,即使您使用 CDATA。
  2. Google AdSense 要求 AdSense JavaScript 逐字显示在您的网页中,否则违反了 TOS。
  3. 根据这个 StackOverflow 答案,目前 AJAX 网站上的 AdSense 支持很差。
  4. Google AdSense 抓取工具将无法看到您网页上的任何内容,因此我怀疑广告定位是否会起作用。但是请参阅下面的一些可能对爬虫有帮助的东西。

但为了简单起见,我假设您可以直接与 Google 讨论 TOS 问题,我将尝试解决技术问题。首先,基于这个 StackOverflow 答案,这里有一个可能的解决方案,可以让您逐字提供 Google 的脚本:

<script type="text/x-handlebars">
    <h1>Ember AdSense</h1>
    {{outlet}}
    <div id="ads"></div>
</script>

<script type="text/x-handlebars" data-template-name="index">
    <p>Hello, world!</p>
</script>

<div id="ads-load" style="display: none">

<!--
  Apparently this needs to appear verbatim, exactly as Google gave it to
  you, or it's a TOS violation.
-->

<script type="text/javascript"><!--
google_ad_client = "ca-pub-XXXXXXXXXX";
/* Test Ad */
google_ad_slot = "XXXXXX";
google_ad_width = 250;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

</div>

然后当我们的主模板加载时,我们使用 jQuery 将广告移动到我们的应用程序模板中:

window.App = Ember.Application.create();

// Explicitly declare the view class for our application view.
App.ApplicationView = Ember.View.extend({
    // Called once the view is rendered.
    didInsertElement: function () {
        $('#ads-load').appendTo("#ads").css("display", "block");
    }
});

至于允许谷歌爬虫看到你的内容,谷歌官方对 AJAX 应用有建议,但不知道是否适用于 AdSense 爬虫。或者,如果您pushState用于更新显示的 URL,那么您需要确保当爬虫请求时,这些 URL 中的每一个都可以由您的服务器呈现。(Discourse 论坛软件正是这样做的。)

如果它让你更接近,请告诉我。

于 2013-04-09T11:28:29.037 回答