* @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\View; use Joomla\CMS\Application\ApplicationHelper; use Joomla\CMS\Event\AbstractEvent; use Joomla\CMS\Factory; use Joomla\CMS\Filesystem\Path; use Joomla\CMS\Language\Text; use Joomla\CMS\Uri\Uri; use Joomla\CMS\User\CurrentUserInterface; use Joomla\CMS\User\CurrentUserTrait; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Base class for a Joomla Html View * * Class holding methods for displaying presentation data. * * @since 2.5.5 */ class HtmlView extends AbstractView implements CurrentUserInterface { use CurrentUserTrait; /** * The base path of the view * * @var string * @since 3.0 */ protected $_basePath = null; /** * Layout name * * @var string * @since 3.0 */ protected $_layout = 'default'; /** * Layout extension * * @var string * @since 3.0 */ protected $_layoutExt = 'php'; /** * Layout template * * @var string * @since 3.0 */ protected $_layoutTemplate = '_'; /** * The set of search directories for resources (templates) * * @var array * @since 3.0 */ protected $_path = array('template' => array(), 'helper' => array()); /** * The name of the default template source file. * * @var string * @since 3.0 */ protected $_template = null; /** * The output of the template script. * * @var string * @since 3.0 */ protected $_output = null; /** * Charset to use in escaping mechanisms; defaults to urf8 (UTF-8) * * @var string * @since 3.0 */ protected $_charset = 'UTF-8'; /** * Constructor * * @param array $config A named configuration array for object construction. * name: the name (optional) of the view (defaults to the view class name suffix). * charset: the character set to use for display * escape: the name (optional) of the function to use for escaping strings * base_path: the parent path (optional) of the views directory (defaults to the component folder) * template_plath: the path (optional) of the layout directory (defaults to base_path + /views/ + view name * helper_path: the path (optional) of the helper files (defaults to base_path + /helpers/) * layout: the layout (optional) to use to display the view * * @since 3.0 */ public function __construct($config = array()) { parent::__construct($config); // Set the charset (used by the variable escaping functions) if (\array_key_exists('charset', $config)) { @trigger_error( 'Setting a custom charset for escaping is deprecated. Override \JViewLegacy::escape() instead.', E_USER_DEPRECATED ); $this->_charset = $config['charset']; } // Set a base path for use by the view if (\array_key_exists('base_path', $config)) { $this->_basePath = $config['base_path']; } else { $this->_basePath = JPATH_COMPONENT; } // Set the default template search path if (\array_key_exists('template_path', $config)) { // User-defined dirs $this->_setPath('template', $config['template_path']); } elseif (is_dir($this->_basePath . '/tmpl/' . $this->getName())) { $this->_setPath('template', $this->_basePath . '/tmpl/' . $this->getName()); } elseif (is_dir($this->_basePath . '/View/' . $this->getName() . '/tmpl')) { $this->_setPath('template', $this->_basePath . '/View/' . $this->getName() . '/tmpl'); } elseif (is_dir($this->_basePath . '/view/' . $this->getName() . '/tmpl')) { $this->_setPath('template', $this->_basePath . '/view/' . $this->getName() . '/tmpl'); } elseif (is_dir($this->_basePath . '/views/' . $this->getName() . '/tmpl')) { $this->_setPath('template', $this->_basePath . '/views/' . $this->getName() . '/tmpl'); } else { $this->_setPath('template', $this->_basePath . '/views/' . $this->getName()); } // Set the default helper search path if (\array_key_exists('helper_path', $config)) { // User-defined dirs $this->_setPath('helper', $config['helper_path']); } else { $this->_setPath('helper', $this->_basePath . '/helpers'); } // Set the layout if (\array_key_exists('layout', $config)) { $this->setLayout($config['layout']); } else { $this->setLayout('default'); } $this->baseurl = Uri::base(true); } /** * Execute and display a template script. * * @param string $tpl The name of the template file to parse; automatically searches through the template paths. * * @return void * * @throws \Exception * @see \JViewLegacy::loadTemplate() * @since 3.0 */ public function display($tpl = null) { $app = Factory::getApplication(); if ($this->option) { $component = $this->option; } else { $component = ApplicationHelper::getComponentName(); } $context = $component . '.' . $this->getName(); $app->getDispatcher()->dispatch( 'onBeforeDisplay', AbstractEvent::create( 'onBeforeDisplay', [ 'eventClass' => 'Joomla\CMS\Event\View\DisplayEvent', 'subject' => $this, 'extension' => $context ] ) ); $result = $this->loadTemplate($tpl); $eventResult = $app->getDispatcher()->dispatch( 'onAfterDisplay', AbstractEvent::create( 'onAfterDisplay', [ 'eventClass' => 'Joomla\CMS\Event\View\DisplayEvent', 'subject' => $this, 'extension' => $context, 'source' => $result ] ) ); $eventResult->getArgument('used', false); echo $result; } /** * Escapes a value for output in a view script. * * If escaping mechanism is htmlspecialchars, use * {@link $_charset} setting. * * @param mixed $var The output to escape. * * @return mixed The escaped value. * * @note the ENT_COMPAT flag was replaced by ENT_QUOTES in Joomla 4.0 to also escape single quotes * * @since 3.0 */ public function escape($var) { if ($var === null) { return ''; } return htmlspecialchars($var, ENT_QUOTES, $this->_charset); } /** * Get the layout. * * @return string The layout name * * @since 3.0 */ public function getLayout() { return $this->_layout; } /** * Get the layout template. * * @return string The layout template name * * @since 3.0 */ public function getLayoutTemplate() { return $this->_layoutTemplate; } /** * Sets the layout name to use * * @param string $layout The layout name or a string in format