2011-08-11 41 views
1

我想能夠將谷歌分析代碼移入該數據庫。Smarty和JavaScript裏面的文字

本來我想手動執行此模板的內部:

<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> 
+1

這將打印出*文字* '{$ portal.google_analytics}'。放下'literal'標籤並嘗試。 – Piskvor

回答

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
<?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>