Jump to content
  • 0
nisompic

Declare custom Smarty modifier in WHMCS Server Module

Question

Hello,

I am trying to define a custom modifier for Smarty templates in my provisioning module. I have tried multiple methods but they all seem to fail.
My first try was using a Hook on ClientAreaPage where my function would register a Smarty modifier:

<?php
function hr_size($bytes) {
    $size   = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
    $factor = floor((strlen($bytes) - 1) / 3);
    $dec = 2;

    return sprintf("%.{$dec}f", $bytes / pow(1024, $factor)) . @$size[$factor];
}

add_hook('ClientAreaPage', 1, function ($vars) {
    global $smarty;
    $smarty->registerPlugin('modifier', 'humanReadable', 'hr_size');
});

Unfortunately, when using the modifier in the template file returned by module_ClientArea(), the template is not shown and I get the following error in the Activity Log: 

Syntax error in template "file:..." on line 5 "{$object.Size|humanReadable} |" unknown modifier 'humanReadable'

I have then tried this very same way by declaring global $smarty; in my module_ClientArea() function and registering the modifier there, but it did not work either.

I'm at a loss on what to try next, has anyone ever done something similar and made it work?
I'm using WHMCS 7.10 .

Share this post


Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 0

As far as I know you have to place your modifier (eg. modifier.humanReadable.php) in vendor/smarty/smarty/libs/plugins directory then you should be able to use {$somevar|humanReadable} in Smarty template. But keep in mind that last time I created a Smarty modifier was 2015 🤔

Share this post


Link to post
Share on other sites
  • 0

I initially brushed off the idea since I thought a WHMCS update would overwrite the file, but it does work as expected, which is great!
I think I'll keep this method for now, thank you very much @Kian!

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated