To load parts of currently opened view I use default template part loading function $this->loadTemplate. $this->loadTemplate('default_cover');

But for easier view management I want to put different view parts in different folders.

In this case I would like to know if it's possible to select folder from which to load required view with $this->loadTemplate function

up vote 2 down vote accepted

There's view's addTemplatePath() to add additional lookup paths:

$this->addTemplatePath(JPATH_BASE . '/components/com_example/templates/example_view');

With this, the custom path is prioritized, meaning it would override template overrides in standard folders. To reverse that, reverse the array order of $_path['template']:

$this->_path['template'] = array_reverse($this->_path['template']);

Or, instead of using addTemplatePath(), manually add the path:

$this->_path['template'][] = JPATH_BASE . '/components/com_example/templates/example_view';

Also, if you want to allow custom structure in template overrides, add template folder too (also mind the lookup order):

$this->addTemplatePath(JPATH_THEMES . '/' . JFactory::getTemplate() . '/html/com_example/example_view');

p.s. I don't endorse using custom folder structure. This only leads to confusion for users trying to make overrides. Perhaps look into using layouts instead?

  • Wow. That's great explanation. Thank you @Sharky Yeah. It seems a bit too confusing – user1876234 Aug 10 at 9:35

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.