Page 1 of 1

Issue with UDT and Smarty Variables in Custom Module

Posted: Fri Mar 28, 2025 8:25 am
by lewiscook
Hi everyone, I'm new to CMS Made Simple and trying to create a custom solution using a User-Defined Tag (UDT) along with Smarty variables, but I’m running into issues.
I have a UDT that fetches data from a custom database table and assigns it to a Smarty variable like this:

Code: Select all

global $smarty, $db;
$query = "SELECT title, content FROM cms_my_custom_table WHERE id = ?";
$result = $db->GetRow($query, [1]);

if ($result) {
    $smarty->assign('my_custom_data', $result);
}
Then, in my template, I try to use:

Code: Select all

{$my_custom_data.title}
{$my_custom_data.content}
However, the variable does not seem to be available in the template. I’ve tried clearing the cache and debugging with `{$smarty.debug}` but don’t see `my_custom_data` in the list of assigned variables.
Am I missing something in how CMSMS handles UDTs and variable assignment? Any help would be greatly appreciated!

Re: Issue with UDT and Smarty Variables in Custom Module

Posted: Sat Mar 29, 2025 12:56 pm
by Jo Morg
You shouldn't use global for this purpose, in fact CMSMS core has deprecated (and I'm almost sure we have removed...) its use in 99% of it's code. IIRC $smarty and $db should already be available for UDTs, but if not use the core API functions such as $db = cms_utils::get_db(); and $smarty = cms_utils::get_smarty();.
One of the possible consequences of using global in this context is that even if $smarty is available there I would almost bet that it's not the template object that you are expecting it to be so the variables would not be where you are looking for.

HTH