Smarty for WHMCS

7 May

Smarty for WHMCS

In MVC, we always use view to display html contents. If we are not using any template engine, then we have to use php tags in the html page to add dynamic contents.

For example, we can use below code to loop through an array without any template engine.

<?php foreach ($users as $user) : ?>
<div>
    <h2><?php echo $user['name']; ?></h2>
    <p><?php echo $user['company']; ?></p>
</div>
<?php endforeach; ?>

Here we are using php tags to loop through the array. Also we are using php function to print it. WHMCS uses Smarty template engine, and we have to do loop in a different manner with Smarty template engine.

{foreach from=$users item=user}
	<h2>{$user.name}</h2>
	<p>{$user.company}</p>
{/foreach}

This is just an example that shows the difference between Smarty and normal view files. For normal view files, we add .php as extension. But for Smarty files, by default .tpl is the extension of the view files.

Now let’s explore more smarty examples.
Let’s fist create a WHMCS page.

For any custom client area page, we have to add the below content at the top of the page.

<?php

//Let's include whmcs classes
use WHMCS\ClientArea;
use WHMCS\Database\Capsule;

define('CLIENTAREA', true);

require __DIR__ . '/init.php';

$ca = new ClientArea();
$ca->initPage();

Let’s name our custom page as custom_page.php and upload it to the root folder of whmcs.
Now , we have the content required for any client area page on custom_page.php.
Now let’s display some html using the php file.
For that we have to create a tpl file.

Before that, let’s discuss about client area theming.

With whmcs, there are many themes available. For each theme,
there will be different css, images etc. Templates are stored in the folder templates/.
WHMCS has a default client area template with the name ‘six’. So the client area .tpl files are stored in the folder templates/six.

Now, back to our custom_page.php. For this page, we have to create a tpl file to add html contents. Let’s name the tpl file as custom_page.tpl, then it should be uploaded to the folder templates/six.

So our tpl file full path is templates/six/custom_page.tpl

Let’s just add content as

<p>Hello world!!!! </p>

To use the above file as tpl file, we have to define that in custom_page.php

That can be one as

$ca->setTemplate('custom_page');  //it will load the templates/six/custom_page.tpl

if our tpl file name was test.tpl, then we could load that as

$ca->setTemplate('test');  //it will load templates/six/test.tpl

Then to print the content, we have to add below line to the custom_page.php

$ca->output();

So the full custom_page.php is

<?php

//Let's include whmcs classes
use WHMCS\ClientArea;
use WHMCS\Database\Capsule;

define('CLIENTAREA', true);

require __DIR__ . '/init.php';

$ca = new ClientArea();
$ca->initPage();

$ca->setTemplate('custom_page'); 
$ca->output();

And our templates/six/custom_page.tpl content is

<p>Hellow world!!!! </p>

Then browse the url
http://whmcssite.com/custom_page.php

And you will see the html content
Hello world!!!!

Now we know how to display static content using smarty.

Now let’s see how to display dynamic contents using smarty.
For that, now let’s just display the logged in user’s email in the tpl.

We have to add the below code to the php file after the line $ca->initPage();

//get the logged in user id
$loggedin_user_id =  $_SESSION['uid'];
$client = Capsule::table('tblclients')
        ->where('id', '=', $loggedin_user_id)
        ->first();
$ca->assign('client_email', $client->email);

And below code to the tpl

<p>
        Client email is: {$client_email}
</p>

So the trick is, we havee to fetch what ever data to be displayed using a query and then pass that to the
tpl as

$ca->assign('client_email', $client->email);

Then we can just access that from the tpl as {$client_email}

If we were passing the email as

$ca->assign('clientMail', $client->email);

Then we can access it from the tpl as

{$clientMail}

We can do a lot more with smarty, Some of them are explained in the below links.
https://www.smarty.net/docsv2/en/language.function.if.tpl
https://www.smarty.net/docs/en/language.function.foreach.tpl
https://www.smarty.net/docs/en/language.variables.tpl
https://www.smarty.net/docs/en/language.function.include.tpl
https://www.smarty.net/docs/en/language.syntax.variables.tpl

~

Leave a Reply

Your email address will not be published. Required fields are marked *