文字中的Smarty和javascript

时间:2011-08-11 15:19:46

标签: php smarty

我希望能够将Google Analytics分析代码移动到该数据库中。

最初我会在模板中手动执行此操作:

<script type="text/javascript"><!--
{literal}
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-19807844-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
{/literal}
--></script>

如何执行以下操作,允许用户将自己的分析数据输入数据库

<script type="text/javascript"><!--
{literal}
{$portal.google_analytics}
{/literal}
--></script>

2 个答案:

答案 0 :(得分:2)

<script type="text/javascript"><!--

{$portal.google_analytics}

--></script>

或者如果您只是想让他们输入他们的帐户:

<script type="text/javascript"><!--

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', {$portal.google_analytics_account}]);
  _gaq.push(['_trackPageview']);

{literal}
  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
{/literal}
--></script>

<script type="text/javascript"><!--
{literal}
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', {/literal}{$portal.google_analytics_account}{literal}]);
  _gaq.push(['_trackPageview']);


  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
{/literal}
--></script>

答案 1 :(得分:1)

 <?php
   // assuming your Smarty object is $smarty
   $smarty->assign('ga_id','UA-xxxxxx-x');
   $smarty->display('ga.tpl');
 ?>

ga.tpl(请注意,两个 literal块,中间有ga_id

<script type="text/javascript"><!--
{literal}
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', '{/literal}{$ga_id}{literal}']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript';
    ga.async = true;
    ga.src = ('https:' == document.location.protocol ?
                 'https://ssl' : 'http://www') +
      '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(ga, s);
  })();
{/literal}
--></script>