Back to HtmlView class

Method loadTemplate

public string
loadTemplate
(mixed $tpl = null)
Load a template file -- first look in the templates folder for an override
Parameters
  • string $tpl The name of the template source file; automatically searches the template paths and compiles as needed.
Returns
  • string The output of the the template script.
Since
  • 3.0
-
  • \Exception
Class: HtmlView
Project: Joomla

Method loadTemplate - Source code

/**
 * Load a template file -- first look in the templates folder for an override
 *
 * @param   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.
 *
 * @since   3.0
 * @throws  \Exception
 */
public function loadTemplate($tpl = null)
{
    // Clear prior output
    $this->_output = null;
    $template = Factory::getApplication()->getTemplate(true);
    $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 = Factory::getLanguage();
    $lang->load('tpl_' . $template->template, JPATH_BASE) || $lang->load('tpl_' . $template->parent, JPATH_THEMES . '/' . $template->parent) || $lang->load('tpl_' . $template->template, JPATH_THEMES . '/' . $template->template);
    // Change the template folder if alternative layout is in different template
    if (isset($layoutTemplate) && $layoutTemplate !== '_' && $layoutTemplate != $template->template) {
        $this->_path['template'] = str_replace(JPATH_THEMES . DIRECTORY_SEPARATOR . $template->template, JPATH_THEMES . DIRECTORY_SEPARATOR . $layoutTemplate, $this->_path['template']);
    }
    // Load the template script
    $filetofind = $this->_createFileName('template', array('name' => $file));
    $this->_template = Path::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 = Path::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;
    }
    throw new \Exception(Text::sprintf('JLIB_APPLICATION_ERROR_LAYOUTFILE_NOT_FOUND', $file), 500);
}