2017-05-31 40 views
0

试图在A.tpl获取输出,但没有得到任何输出。我想我做错了在tpl文件中调用php函数。使用WHMCS的smarty模板引擎。需要使用PHP函数从外部php文件中.tpl文件

A.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函数输出。

在此先感谢。对不起,作为noob。

回答

0

为此修改添加到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}来输出它的值。