来自document:
state
您看到,该示例给出了变量定义,但没有给出函数定义。
我想在php文件中定义一个<?php
use WHMCS\ClientArea;
use WHMCS\Database\Capsule;
define('CLIENTAREA', true);
require __DIR__ . '/init.php';
$ca = new ClientArea();
$ca->setPageTitle('Your Page Title Goes Here');
$ca->addToBreadCrumb('index.php', Lang::trans('globalsystemname'));
$ca->addToBreadCrumb('mypage.php', 'Your Custom Page Name');
$ca->initPage();
//$ca->requireLogin(); // Uncomment this line to require a login to access this page
// To assign variables to the template system use the following syntax.
// These can then be referenced using {$variablename} in the template.
//$ca->assign('variablename', $value);
// Check login status
if ($ca->isLoggedIn()) {
$clientName = Capsule::table('tblclients')
->where('id', '=', $ca->getUserID())->pluck('firstname');
// 'pluck' was renamed within WHMCS 7.0. Replace it with 'value' instead.
// ->where('id', '=', $ca->getUserID())->value('firstname');
$ca->assign('clientname', $clientName);
} else {
$ca->assign('clientname', 'Random User');
}
Menu::addContext();
Menu::primarySidebar('announcementList');
Menu::secondarySidebar('announcementList');
$ca->setTemplate('mypage');
$ca->output();
,然后在模板中可以调用它。
但是我尝试使用function
或其他方法,所有方法均无效。
答案 0 :(得分:0)
有两种实现目标的方法:
,打开Allow Smarty PHP tag
。那么您可以使用
{php}
your_function();
{/php}
2。推荐的方法是,允许在WHMCS模板中使用其他自定义PHP逻辑,您可能需要结合使用钩子并在页面加载之前定义变量,例如
```
<?php
/**
* Hook sample for defining additional template variables
*
* @param array $vars Existing defined template variables
*
* @return array
*/
function hook_template_variables_example($vars)
{
$extraTemplateVariables = array();
// set a fixed value
$extraTemplateVariables['fixedValue'] = 'abc';
// fetch clients data if available
$clientsData = isset($vars['clientsdetails']) ? $vars['clientsdetails'] : null;
// determine if client is logged in
if (is_array($clientsData) && isset($clientsData['id'])) {
$userId = $clientsData['id'];
// perform calculation here
$extraTemplateVariables['userSpecificValue'] = '123';
$extraTemplateVariables['anotherUserOnlyValue'] = '456';
}
// return array of template variables to define
return $extraTemplateVariables;
}
add_hook('ClientAreaPageViewTicket', 1, 'hook_template_variables_example');
```