Issue with UDT and Smarty Variables in Custom Module

General project discussion. NOT for help questions.
Post Reply
lewiscook
New Member
New Member
Posts: 1
Joined: Fri Mar 28, 2025 8:21 am

Issue with UDT and Smarty Variables in Custom Module

Post 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!
Last edited by Jo Morg on Sat Mar 29, 2025 12:57 pm, edited 1 time in total.
Reason: Edited by moderator for a BBCode fix
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1964
Joined: Mon Jan 29, 2007 4:47 pm

Re: Issue with UDT and Smarty Variables in Custom Module

Post 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
"There are 10 types of people in this world, those who understand binary... and those who don't."
* by the way: English is NOT my native language (sorry for any mistakes...).
Code of Condut | CMSMS Docs | Help Support CMSMS
My developer Page on the Forge
GeekMoot 2015 in Ghent, Belgium: I was there!
GeekMoot 2016 in Leicester, UK: I was there!
DevMoot 2023 in Cynwyd, Wales: I was there!
Post Reply

Return to “General Discussion”