Search code examples
phptemplateswhmcs

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


I have a php script that select from the database and display every row.

I want to display that information in homepage.tpl all that is happening in WHMCS.

I am assuming there is an index file that is already doing that but I have an encrypted version. Wondering whether I can have another php file that will assign my smarty variables and be able to use them in my homepage.tpl?


Solution

  • WHMCS encourages not to include php code in the tpl files, and recommends adding any php related code to hooks.

    For your question:

    1 - Add file includes/hooks/my_list.php, and add the following code to it:

    <?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 - In homepage.tpl, access the passed variables as following:

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

    References:

    WHMCS Hooks

    Interacting With The Database