noob to smarty, UDT writing needs a little coaching

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Post Reply
npeg
New Member
New Member
Posts: 6
Joined: Thu Apr 14, 2011 1:33 pm

noob to smarty, UDT writing needs a little coaching

Post by npeg »

My end goal is to pass the page alias into my UDT which will then make related database queries.

Trying to get a handle on how to communicate between smarty/php.

I see lots of examples of UDT's where there's:

Code: Select all

global $gCms;
but no references as to what this global var is. I'm assuming it's a reference to the 'scope' of the CMS/smarty itself (?)

I know you can pass params in via the tag:

Code: Select all

 {content assign=pagecontent}
  {table_of_contents thepagecontent="$pagecontent"}
but i'm not sure where to look to find out (reference) what smarty variables are available to pass in ( i see that 'content' is assigned to a php variable and passed in directly to the UDT in the above example )

any help would be great!
VesQ
New Member
New Member
Posts: 2
Joined: Sat Apr 09, 2011 10:24 am

Re: noob to smarty, UDT writing needs a little coaching

Post by VesQ »

The $gCms is a global object for CmsObject class. You need it to get a reference to the ADODB-object when you're about to make queries to the database, e.g.

Code: Select all

global $gCms;
$db =& $gCms->GetDb(); // an ADODB-object reference. Notice that we're using only a reference and thus not creating a new object in memory.

// Now when we have $db available, we can do queries like this:
$query = "SELECT * FROM myspecialtable WHERE username = ?";
$result = array();
$rs = $db->Execute($query,$params['username']);
if($rs && $rs->RecordCount() > 0) {
    $result = $rs->GetArray();
}
echo '<pre>';
print_r( $result );
echo '</pre>';
Notice that there's the special array, called $params, I'm using. It hold's the variables you have inputted to your UDT. So if I wanted to get all the results from myspecialtable where field username has the value "Bob", I'd call my UDT like this:

Code: Select all

{myudt username='Bob'}
I hope this answer is what you needed.
VesQ
New Member
New Member
Posts: 2
Joined: Sat Apr 09, 2011 10:24 am

Re: noob to smarty, UDT writing needs a little coaching

Post by VesQ »

Oh, and have you read this? http://wiki.cmsmadesimple.org/index.php ... fined_Tags

It helps you making your own UDT's
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

Re: noob to smarty, UDT writing needs a little coaching

Post by Wishbone »

$gCms is an instance of the CmsObject class: http://www.cmsmadesimple.org/apidoc/CMS/CmsObject.html

You can use {get_template_vars} to see all smarty variables available to your template.
npeg
New Member
New Member
Posts: 6
Joined: Thu Apr 14, 2011 1:33 pm

Re: noob to smarty, UDT writing needs a little coaching

Post by npeg »

thanks for the excellent responses guys! I'm beginning to get a handle on the whole smarty/UDT tag thing.

If i wanted to pass in a template var (page_alias) to my UDT, how would i go about that? (I haven't found an example yet)

{myUDT myVar=page_alias}

Ultimately i want to query a separate/non-cms database using the page_alias/product name
Post Reply

Return to “CMSMS Core”