2016-02-05 53 views
2

我想獲得我的所有類別在我的頭(header.tpl),但似乎都不盡如人意......獲取類別在我的Prestashop主題的題材的Prestashop

我的代碼header.tpl

{$childCategories= Category::getChildren(0, 0, $active = true, $id_shop = false);} 
{printf($childCategories)} 

問題:錯誤500

+0

你檢查你的Apache的錯誤日誌文件?你正在使用哪種版本的Prestashop? –

+0

我使用的是最新版本,錯誤是'Category'不存在......但我可以在哪裏發送類別到模板? – tonymx227

回答

6

您寫的代碼對smarty無效。 Prestashop使用Smarty來呈現模板。如果你想避免這樣的麻煩,請看看規則。此外,Prestashop的默認主題中有很多示例,以瞭解更多關於Smarty編碼的內容。

正確的代碼如下:

{assign var='childCategories' value=Category::getChildren(1, 1, true, false)} 

要傳遞

  1. $ id_parent:父類ID。根標識類別爲1,家庭標識類別爲2.
  2. $ id_lang:id語言。您可以在本地化區域查看它以獲取語言的編號。如果您啓用了多種語言,則可以使用$ language變量來獲取該ID。 Prestashop中的List of global variables
  3. $活動:僅返回活動護理。
  4. $ id_shop:如果你有多個商店在instalation中的商店的ID。

打印變量調試

如果你想調試或看到的變量,你可以試試下面的代碼片段:

{* Print only the variable $childCategories *} 
{$childCategories|var_dump} 

或:

{* Print all variables *} 
{debug} 
+0

謝謝你完美的作品。 ;) – tonymx227

0

模板header.tpl來自FrontController.php功能displayHeader()

Category在那裏不存在,因爲header.tpl是在所有頁面中使用的綜合模板。

有幾個鉤子,你可以使用添加內容有:displayHeaderdisplayTopdisplayLeftColumndisplayRightColumndisplayFooter

您可以添加所有類別的任何模塊和這些鉤子像之一:

$category = new Category((int)Configuration::get('PS_HOME_CATEGORY'), $this->context->language->id); 
$sub_categories = $category->getSubCategories($this->context->language->id); 
// else code 
+0

這個解決方案很好,但我認爲它有一個缺點:你必須開發一個模塊。如果要修改結果的結果,此解決方案會更好,例如,創建類別的自定義過濾器。但是如果你沒有任何特殊功能,最簡單的方法就是通過靜態調用。 – Rufein

+0

是的,但是如果您想讓您的系統安全地進行主題升級,開發模塊是首選方式。 –