Html View

The function loads a template file. It first looks in the templates folder for an override.

  • string $tpl - The name of the template source file; automatically searches the template paths and compiles as needed.
  • return string - The output of the the template script.
public function loadTemplate($tpl = null)
{
// Clear prior output
$this->_output = null;

$template = \JFactory::getApplication()->getTemplate();
$layout = $this->getLayout();
$layoutTemplate = $this->getLayoutTemplate();

// Create the template file name based on the layout
$file = isset($tpl) ? $layout . '_' . $tpl : $layout;

// Clean the file name
$file = preg_replace('/[^A-Z0-9_\.-]/i', '', $file);
$tpl = isset($tpl) ? preg_replace('/[^A-Z0-9_\.-]/i', '', $tpl) : $tpl;

// Load the language file for the template
$lang = \JFactory::getLanguage();
$lang->load('tpl_' . $template, JPATH_BASE, null, false, true)
|| $lang->load('tpl_' . $template, JPATH_THEMES . "/$template", null, false, true);

// Change the template folder if alternative layout is in different template
if (isset($layoutTemplate) && $layoutTemplate !== '_' && $layoutTemplate != $template)
{
$this->_path['template'] = str_replace($template, $layoutTemplate, $this->_path['template']);
}

// Load the template script
jimport('joomla.filesystem.path');
$filetofind = $this->_createFileName('template', array('name' => $file));
$this->_template = \JPath::find($this->_path['template'], $filetofind);

// If alternate layout can't be found, fall back to default layout
if ($this->_template == false)
{
$filetofind = $this->_createFileName('', array('name' => 'default' . (isset($tpl) ? '_' . $tpl : $tpl)));
$this->_template = \JPath::find($this->_path['template'], $filetofind);
}

if ($this->_template != false)
{
// Unset so as not to introduce into template scope
unset($tpl, $file);

// Never allow a 'this' property
if (isset($this->this))
{
unset($this->this);
}

// Start capturing output into a buffer
ob_start();

// Include the requested template filename in the local scope
// (this will execute the view logic).
include $this->_template;

// Done with the requested template; get the buffer and
// clear it.
$this->_output = ob_get_contents();
ob_end_clean();

return $this->_output;
}
else
{
throw new \Exception(\JText::sprintf('JLIB_APPLICATION_ERROR_LAYOUTFILE_NOT_FOUND', $file), 500);
}
}