2010-06-26 41 views
0

我已經建立了一個變量(horaires_lundi)在該文件中:Magento:很好地顯示在JavaScript中的變量。如何在同一個文件中的PHP調用它?

應用程序/代碼/社區/ Unirgy/StoreLocator /控制器/ Adminhtml/Locationcontroller.php

->setTitle($this->getRequest()->getParam('title')) 
->setNotes($this->getRequest()->getParam('notes')) 
->setStreetAddress($this->getRequest()->getParam('street_address')) 
->setHorairesLundi($this->getRequest()->getParam('horaires_lundi')) 

所有變量都

應用程序/設計/前端/預設/默認/模板/ unirgy/storelocator/map.phtml

:在這個文件中也顯示在JavaScript在JavaScript
(function(){ 
    var storeLocator = new UnirgyStoreLocator({ 
     mapEl: $('map'), 
     sidebarEl: $('sidebar'), 
     searchUrl: '<?php echo $this->getUrl('ustorelocator/location/search')?>', 
     generateSidebarHtml: function(m) { 
      return '<span style="font-size:14px; color:#C68804;"><b>' + m.title + '</b></span><br/>(Distance : ' + parseFloat(m.distance).toFixed(1) + ' ' + m.units + ')<br/>' 
       + m.street_address + '&nbsp;' + m.test + '&nbsp;' + m.city + '<br/><b>' + m.notes + '</b><br/>'; 
     }, 
     generateMarkerHtml: function(m) { 
      var url = m.website_url.replace(/\s/,''); 
      var dataForm = new VarienForm('form-validate', true); 
      return '<span style="font-size:14px; color:#C68804;">' + m.title + '</span><br/>' 
       + '<?php if (trim($this->getHorairesLundi())!=""): ?>Lun&nbsp;'+ m.horaires_lundi +'<?php else : ?>No horaire lundi<br/><?php endif; ?>' 

可變m.horaires_lundi公顯示在前端。 但我不能在PHP中的狀態顯示m.horaires_lundi只有m.horaires_lundi存在......

上面我與

<?php if (trim($this->getHorairesLundi())!=""): ?> 

它是一個goog方法一試?我還需要做什麼? 是否有其他方法的東西,如:

<?php if (Mage::helper(... 

我發現一個關於Magento的教程,但找不到我的解決方案。

+0

下一次,請4位(使用'code'按鈕)縮進代碼;那麼你也不必使用這些點來顯示php標籤。 – 2010-06-26 17:48:28

回答

1

有PHP和JavaScript之間的區別顯著:前者是服務器端腳本語言,後者客戶端腳本語言。儘管您可以使用PHP來有條件地向瀏覽器發送echo JavaScript代碼(我不建議這樣做),但您無法訪問PHP中的JavaScript變量。只需在腳本中測試JavaScript變量的存在。

如果我正確理解你的目標,你可以在你的代碼的最後一行改寫爲

return '<span style="font-size:14px; color:#C68804;">' + m.title + '</span><br/>' + 
    ((m.horaires_lundi) ? 'Lun&nbsp;' + m.horaires_lundi : 'No horaire lundi') + '<br/>'; 
+0

非常感謝,你讓我的一天! 也感謝你「用4個空格縮進你的代碼」 – Bargalunan 2010-06-27 06:19:08

相關問題