簡體   English   中英

使用WHMCS的smarty模板引擎。 需要從.tpl文件中的外部php文件使用php函數

[英]Working with smarty template engine for WHMCS. Need to use php function from external php file in .tpl file

嘗試在A.tpl中獲取輸出,但未獲得任何輸出。 我認為我在tpl文件中調用php函數時做錯了。

tpl

{myModifier}

B.php

class Geolocation{
    public function sm_loc($params, Smarty_Internal_Template $template)
    {
        return "100.70";
    }
}

$smarty1 = new Smarty();

$smarty1->registerPlugin('modifier', 'myModifier', array('Geolocation', 'sm_loc'));

我已經使用過此代碼。 而且這似乎不起作用。 這樣做也會破壞我在A.tpl中現有的工作代碼。

我的需要是從外部php文件獲取A.tpl中php函數的輸出。

提前致謝。 很抱歉成為菜鳥。

要將此修飾符添加到Smarty並在模板中使用它,最好使用WHMCS掛鈎。

如果您在“〜/ includes / hooks /”目錄下創建一個新的PHP文件(您可以命名任何東西-在這種情況下,請使用“ myhook.php”),WHMCS將在每個請求時自動插入此鈎子。

為此,您將要使用ClientAreaPage掛鈎。 在鈎子內部,然后可以訪問全局$smarty變量。

例:

function MySmartyModifierHook(array $vars) {
    global $smarty;

    // I recommend putting your Geolocation class in a separate PHP file,
    // and using 'include()' here instead.
    class Geolocation{
        public function sm_loc($params, Smarty_Internal_Template $template) {
            return "100.70";
        }
    }

    // Register the Smarty plugin
    $smarty->registerPlugin('modifier', 'myModifier', array('Geolocation', 'sm_loc'));
}

// Assign the hook
add_hook('ClientAreaPage', 1, 'MySmartyModifierHook');

這應該夠了吧。 如果要使用其他掛鈎,可以查看WHMCS文檔中的掛鈎索引

每個掛鈎文件中的函數名稱必須唯一。

附帶說明一下,如果您只想在特定頁面上運行此鈎子,則可以檢查傳遞的$vars數組中的templatefile鍵。 例如,假設您只希望此掛鈎在訂單的“查看購物車”頁面上運行:

function MySmartyModifierHook(array $vars) {
    global $smarty;

    // If the current template is not 'viewcart', then return
    if ($vars['templatefile'] != 'viewcart')
        return;

    // ... your code here ...
}

另外,請注意,對於類似“ ClientAreaPage”鈎子的鈎子,返回鍵和值的數組將自動將它們添加為Smarty變量。 因此,如果您的鈎子函數以return ['currentTime' => time()];結尾return ['currentTime' => time()]; ,然后可以在Smarty模板中使用{$currentTime}來輸出其值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM