Page 1 of 1
noob to smarty, UDT writing needs a little coaching
Posted: Thu Apr 14, 2011 1:50 pm
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:
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!
Re: noob to smarty, UDT writing needs a little coaching
Posted: Thu Apr 14, 2011 3:53 pm
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:
I hope this answer is what you needed.
Re: noob to smarty, UDT writing needs a little coaching
Posted: Thu Apr 14, 2011 3:56 pm
by VesQ
Oh, and have you read this?
http://wiki.cmsmadesimple.org/index.php ... fined_Tags
It helps you making your own UDT's
Re: noob to smarty, UDT writing needs a little coaching
Posted: Thu Apr 14, 2011 5:36 pm
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.
Re: noob to smarty, UDT writing needs a little coaching
Posted: Fri Apr 15, 2011 3:44 pm
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