Whmcs 使用 smarty 变量调用 tpl 文件中的 php 脚本

Whmcs call a php script in a tpl file using smarty variables

我有一个 php 脚本,它 select 来自数据库并显示每一行。

我想在 homepage.tpl 中显示 WHMCS 中发生的所有信息。

我假设有一个索引文件已经这样做了,但我有一个加密版本。想知道我是否可以有另一个 php 文件来分配我的 smarty 变量并能够在我的 homepage.tpl 中使用它们?

WHMCS 不鼓励在 tpl 文件中包含 php 代码,并建议将任何 php 相关代码添加到挂钩中。

针对您的问题:

1 - 添加文件 includes/hooks/my_list.php,并向其中添加以下代码:

<?php

if (!defined("WHMCS"))
    die("This file cannot be accessed directly");

use Illuminate\Database\Capsule\Manager as Capsule;

add_hook('ClientAreaPage', 1, function ($vars) 
{
    //Make sure code executed only in homepage.tpl (index.php)
    if (strpos($vars['SCRIPT_NAME'], 'index.php') !== false) {
        //Read products
        $products = Capsule::table('tblproducts')
                    ->where('hidden', '0')
                    ->orderBy('gid', 'name')
                    ->get();

        //pass variables to the template
        $extra = array("products" => $products, "prdouctsLabel" => "Our Products");

        return $extra;
    }


});

2 - 在 homepage.tpl 中,访问传递的变量如下:

<h4>{$prdouctsLabel}</h4>
<ul>
    {foreach item=product from=$products }
    <li>{$product->name}</li>
    {/foreach}

</ul> 

参考文献:

WHMCS Hooks

Interacting With The Database

认为您无法在此处查看该代码,这是一个更好的版本'

      if (!defined("WHMCS"))
      die("This file cannot be accessed directly");
      require("new.php");
      use Illuminate\Database\Capsule\Manager as Capsule;

      add_hook('ClientAreaPage', 1, function ($vars) 
      {
        //Make sure code executed only in homepage.tpl (index.php)
        if (strpos($vars['SCRIPT_NAME'], 'index.php') !== false) {

        $extra = array("products" => $car, "prdouctsLabel" => "Our Products");

return $extra;
}

 });'