2013-12-13 28 views
0

我正在學習smarty。我發現它非常好。但是我遇到了一個錯誤,我不知道如何解決這個問題。因爲我有用PHP編寫的代碼,我將它轉換爲smarty模板。如何使用Smarty中的if語句找出數據是否爲空?

我有一個if語句,但驗證數據時,我得到一個錯誤

Fatal error: Smarty error: [in addons/maptool/views/maps/update.tpl line 59]: syntax error: unrecognized tag: if(!$locations.updated_lat ? $locations.updated_lat : $locations.geo_alt) (Smarty_Compiler.class.php, line 445) 

我的目標是簡單地驗證,如果變量不爲空或不等於0,我所做的是:

<div class="map_canvas" align="center" id="map_canvas_{$locations.company_id}" style="width: 800px; height: 300px; margin: 10px; border-style: solid; border-color: #0a9ccc" data-company-city="" data-company_id="{$locations.company_id}" data-label="{$locations.company}" data-lat="{if(!$locations.updated_lat ? $locations.updated_lat : $locations.geo_alt)}" data-long=" {if(!$locations.updated_long ? $locations.updated_long : $locations.geo_long)}"></div> 

這裏是智者的一部分,如果語句:

{if(!$locations.updated_lat ? $locations.updated_lat : $locations.geo_alt)} 

我沒有數據,但第一個問題e如果聲明是錯誤。好的,這是所有的感謝。

回答

1

您可以使用|default修飾符來測試變量是否被定義,您可能需要編寫自己的測試來測試是否爲空。

{if $locations.updated_lat|default:$locations.geo_alt}

+0

爲響應好,謝謝,我會盡力的 – Jerielle

+0

好吧,我有錯誤 致命錯誤:Smarty錯誤:[在插件/ maptool/views/maps/update.tpl行80]:語法錯誤:不匹配的標記{/ foreach}。預計{/ if}(打開第55行)。 (Smarty_Compiler.class.php,2299行)在1095行:C:\ wamp \ www \ www.hallohallotown.com \ lib \ templater \ Smarty.class.php – Jerielle

+0

檢查您的標記嵌套順序,這就是所有... – Scuzzy

0

您可以通過使用下面的代碼檢查

{if "" != $var) 

變量此檢查是不爲空或空