为此修改添加到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文档中的Hook Index。
在每个钩子文件的函数名称必须是唯一的。请注意,如果您只想在特定页面上运行此钩子,则可以检查传入的$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()];
结尾,你可以在你的Smarty模板中使用{$currentTime}
来输出它的值。