News > Detail Template > Get Article ID via UDT

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Locked
eferrini
Forum Members
Forum Members
Posts: 17
Joined: Tue May 04, 2010 6:21 pm

News > Detail Template > Get Article ID via UDT

Post by eferrini »

CMSMS 1.9.3

Hi all,

I'm struggling a bit to create a UDT that gets an article ID. The tag is getting called in the detail template in the news module. I am trying to get the article ID for the current news entry. (looks like "news_id" in the database)

I think I'm close. Any suggestions?

$gCms = cmsms(); //global $gCms;
$smarty =& $gCms->GetSmarty();
$entry = $smarty->get_template_vars('id');

Thanks.
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

Re: News > Detail Template > Get Article ID via UDT

Post by Wishbone »

There is no smarty variable called 'id' in a News detail template. There is an $entry->id, however.

Code: Select all

$entry = $smarty->get_template_vars('entry');
echo($entry->id);
You don't need the cmsms() and getSmarty() stuff in your UDT.. $smarty is defined.

What are you using the News ID for?
eferrini
Forum Members
Forum Members
Posts: 17
Joined: Tue May 04, 2010 6:21 pm

Re: News > Detail Template > Get Article ID via UDT

Post by eferrini »

Wishbone, thanks for the help. I knew I was close, just not familiar with accessing the template variables.

I had a prior UDT that pulled the article ID from the "unfriendly" version of the URL, then queried the database for the category and displayed all the other news for the category on the side of the page. So I just needed to retrofit this UDT for use with friendly URLs.
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

Re: News > Detail Template > Get Article ID via UDT

Post by Wishbone »

You don't have to query the database to get the category name.. It is available in the detail template as $entry->category. To see what else is available, add {$entry|print_r} to your detail template.
eferrini
Forum Members
Forum Members
Posts: 17
Joined: Tue May 04, 2010 6:21 pm

Re: News > Detail Template > Get Article ID via UDT

Post by eferrini »

Thanks, wishbone. What I'm actually after is the other news items that are in the same category. So the query is really for getting all the other news items in the same category.
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

Re: News > Detail Template > Get Article ID via UDT

Post by Wishbone »

I still don't think that a UDT to query the database is necessary to get the news in the same category, since you can do {news category=$entry->category} .. Your method might be the easiest way, since you have it working already, but I find that it's best to use the interface provided us (smarty variables) if at all possible, since the database structure can change at any time in the future. The Smarty interface is less likely to change.

I was helping in another thread (http://forum.cmsmadesimple.org/viewtopi ... =8&t=51906) which had a semi-similar issue where the user wanted a gallery related to the news article in the sidebar. The solution used a similar technique.
eferrini
Forum Members
Forum Members
Posts: 17
Joined: Tue May 04, 2010 6:21 pm

Re: News > Detail Template > Get Article ID via UDT

Post by eferrini »

I think you are absolutely correct. Your method is preferable. I didn't think of doing it this way - partially just unaware. I probably fell back on a database query too quickly out of sheer habit, rather than considering an alternative within the system.

As you can probably tell, I'm still a bit new to working with the smarty variables. But I'm trying to learn more and improve in this area. This UDT and the resulting question to the community is my first step in this direction.

Thanks for taking the time to guide me in the right direction.
Locked

Return to “Modules/Add-Ons”