在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>
我是否需要使用预编译的模板从代码中执行此操作,还是有一种我不知道的方式?
答案 0 :(得分:3)
我没有Google AdSense帐户,因此无法对此进行测试。但这里有几个主要问题:
<script>
标记。但为了简单起见,我将假设您可以直接与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");
}
});
至于允许Google抓取工具查看您的内容,Google有official advice for AJAX applications,但我don't know whether that works with the AdSense crawler。或者,如果您使用pushState
更新显示的网址,则需要确保在爬网程序请求时,服务器可以呈现每个网址。 (话语论坛软件does exactly this。)
如果它让你离得更近,请告诉我。