在 Ember (Handlebars) 模板中包含 Google AdSense 横幅的合适方法是什么?
假设我有一个 View 模板,如下所示:
<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>
我是否需要使用预编译模板从代码中执行此操作,还是有一种我不知道的方法?
最佳答案
我没有 Google AdSense 帐户,因此无法对此进行测试。但是这里有几个主要问题:
<script>
Handlebars 模板中的标签,即使您使用 CDATA 也不行。 但为了简单起见,我将假设您可以直接与 Google 讨论 TOS 问题,我将尝试解决技术问题。一、基于this StackOverflow answer ,这是一种可能的解决方案,它允许您逐字提供 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");
}
});
至于允许谷歌爬虫看到你的内容,谷歌有official advice for AJAX applications , 但我 don't know whether that works with the AdSense crawler .或者,如果您使用的是
pushState
要更新您显示的 URL,那么您需要确保当爬虫请求时,您的服务器可以呈现这些 URL 中的每一个。 (话语论坛软件does exactly this。)如果它让你更接近,请告诉我。
关于ember.js - Ember 模板和 Google AdSense,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15897597/