简体   繁体   中英

Is it ok to put $db = JFactory::getDBO(); in Joomla template itself?

Im new to Joomla and Im working on EasyBlog component and I want to create a list of categories as a menu and add a class to the current active menu ite. To do this i have to connect to the database and get the category id using $db = JFactory::getDBO();

My question is, is it ok to use $db = JFactory::getDBO(); on the index.php template of my website to get the ID?

This is my current script.

$view   = JRequest::getCmd('view');
$temp   = JRequest::getString('id');

if ($view == 'entry' or $view == 'tags' or $view == 'archive' or $view == 'blogger' or $view == 'teamblog') {

    $db = JFactory::getDBO();
    $option   = JRequest::getCmd('option');
    $temp   = explode(':', $temp);
    $id   = $temp[0];
        if ($option == 'com_easyblog' && $view == 'entry' && $id)
        { $db->setQuery('SELECT category_id FROM #__easyblog_post WHERE id='.$id);
        $category_id = $db->loadResult(); //this is current article’s category ID
        }
    //echo $category_id; //show it
     $cat_id = $category_id;

    }
    else{
     $cat_id = $temp;
    }

It's really not nice. But it should work if you use page-level caching:

Make sure the cache plugin is enabled, then switch to debug mode, and hit the page twice.

On the second load you should not see your query listed.

If this is the case, you can keep it.

If the query is still there, it means it's not cached and you are slowing down your site.

It's not proper solution ,but it actually solves the problem.

However you have other quite better solutions(in my opinion):

1: Use plugin: it's reccomended if you need to use this menu on every page and want to show it easilly in template. see http://docs.joomla.org/Triggering_content_plugins_in_your_extension for plugin triggering. Check the code of any simple plugin to see how to write this one. Plugins are basically function that you can use everywhere. you can add static variable in plugin and cache cat_id, you will be able then to use it from every place on the site and code will never execute two times. It will remove all the "controller and module" logic from template. You don't even need to use normal trigger you can just execute static function because class will be defined at programm start.

2: This solution is reccomended. Create module. In this case you can cache easilly cache all the resourcess, and you don't need to create logic for assinging it to options (if of course you can do it with itemid). Of course in this case you also need to add whole code for creating menu items, if not you have to push it to global scope which is also not really nice solution.

3: template overriding. I am not sure if it's suitable in this case, but you can always use template overriding http://docs.joomla.org/How_to_override_the_output_from_the_Joomla!_core . In this case you can get rid of whole code in main template and just leave it in component template.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM