📄 view.php
字号:
<?php
/**
* @version $Id: view.php 8682 2007-08-31 18:36:45Z jinx $
* @package Joomla.Framework
* @subpackage Application
* @copyright Copyright (C) 2005 Open Source Matters. All rights reserved.
* @license GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/
// Check to ensure this file is within the rest of the framework
defined('JPATH_BASE') or die();
/**
* Base class for a Joomla View
*
* Class holding methods for displaying presentation data.
*
* @abstract
* @package Joomla.Framework
* @subpackage Application
* @author Johan Janssens <johan.janssens@joomla.org>
* @author Louis Landry <louis.landry@joomla.org>
* @author Andrew Eddie
* @since 1.5
*/
class JView extends JObject
{
/**
* The name of the view
*
* @var array
* @access protected
*/
var $_name = null;
/**
* Registered models
*
* @var array
* @access protected
*/
var $_models = array();
/**
* The base path of the view
*
* @var string
* @access protected
*/
var $_basePath = null;
/**
* The default model
*
* @var string
* @access protected
*/
var $_defaultModel = null;
/**
* Layout name
*
* @var string
* @access protected
*/
var $_layout = 'default';
/**
* Layout extension
*
* @var string
* @access protected
*/
var $_layoutExt = 'php';
/**
* The set of search directories for resources (templates)
*
* @var array
* @access protected
*/
var $_path = array(
'template' => array(),
'helper' => array()
);
/**
* The name of the default template source file.
*
* @var string
* @access private
*/
var $_template = null;
/**
* The output of the template script.
*
* @var string
* @access private
*/
var $_output = null;
/**
* Callback for escaping.
*
* @var string
* @access private
*/
var $_escape = 'htmlspecialchars';
/**
* Charset to use in escaping mechanisms; defaults to urf8 (UTF-8)
*
* @var string
* @access private
*/
var $_charset = 'UTF-8';
/**
* Constructor
*
* @access protected
*/
function __construct($config = array())
{
//set the view name
if (empty( $this->_name ))
{
if (array_key_exists('name', $config)) {
$this->_name = $config['name'];
} else {
$this->_name = $this->getName();
}
}
// set the charset (used by the variable escaping functions)
if (array_key_exists('charset', $config)) {
$this->_charset = $config['charset'];
}
// user-defined escaping callback
if (array_key_exists('escape', $config)) {
$this->setEscape($config['escape']);
}
// 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']);
} else {
$this->_setPath('template', $this->_basePath.DS.'views'.DS.$this->_name.DS.'tmpl');
}
// 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.DS.'helpers');
}
// set the layout
if (array_key_exists('layout', $config)) {
$this->setLayout($config['layout']);
} else {
$this->setLayout('default');
}
}
/**
* Execute and display a template script.
*
* @param string $tpl The name of the template file to parse;
* automatically searches through the template paths.
*
* @throws object An JError object.
* @see fetch()
*/
function display($tpl = null)
{
$result = $this->loadTemplate($tpl);
if (JError::isError($result)) {
return $result;
}
echo $result;
}
/**
* Assigns variables to the view script via differing strategies.
*
* This method is overloaded; you can assign all the properties of
* an object, an associative array, or a single value by name.
*
* You are not allowed to set variables that begin with an underscore;
* these are either private properties for JView or private variables
* within the template script itself.
*
* <code>
* $view = new JView();
*
* // assign directly
* $view->var1 = 'something';
* $view->var2 = 'else';
*
* // assign by name and value
* $view->assign('var1', 'something');
* $view->assign('var2', 'else');
*
* // assign by assoc-array
* $ary = array('var1' => 'something', 'var2' => 'else');
* $view->assign($obj);
*
* // assign by object
* $obj = new stdClass;
* $obj->var1 = 'something';
* $obj->var2 = 'else';
* $view->assign($obj);
*
* </code>
*
* @access public
* @return bool True on success, false on failure.
*/
function assign()
{
// get the arguments; there may be 1 or 2.
$arg0 = @func_get_arg(0);
$arg1 = @func_get_arg(1);
// assign by object
if (is_object($arg0))
{
// assign public properties
foreach (get_object_vars($arg0) as $key => $val)
{
if (substr($key, 0, 1) != '_') {
$this->$key = $val;
}
}
return true;
}
// assign by associative array
if (is_array($arg0))
{
foreach ($arg0 as $key => $val)
{
if (substr($key, 0, 1) != '_') {
$this->$key = $val;
}
}
return true;
}
// assign by string name and mixed value.
// we use array_key_exists() instead of isset() becuase isset()
// fails if the value is set to null.
if (is_string($arg0) && substr($arg0, 0, 1) != '_' && func_num_args() > 1)
{
$this->$arg0 = $arg1;
return true;
}
// $arg0 was not object, array, or string.
return false;
}
/**
* Assign variable for the view (by reference).
*
* You are not allowed to set variables that begin with an underscore;
* these are either private properties for JView or private variables
* within the template script itself.
*
* <code>
* $view = new JView();
*
* // assign by name and value
* $view->assignRef('var1', $ref);
*
* // assign directly
* $view->ref =& $var1;
* </code>
*
* @access public
*
* @param string $key The name for the reference in the view.
* @param mixed &$val The referenced variable.
*
* @return bool True on success, false on failure.
*/
function assignRef($key, &$val)
{
if (is_string($key) && substr($key, 0, 1) != '_')
{
$this->$key =& $val;
return true;
}
return false;
}
/**
* Escapes a value for output in a view script.
*
* If escaping mechanism is one of htmlspecialchars or htmlentities, uses
* {@link $_encoding} setting.
*
* @param mixed $var The output to escape.
* @return mixed The escaped value.
*/
function escape($var)
{
if (in_array($this->_escape, array('htmlspecialchars', 'htmlentities'))) {
return call_user_func($this->_escape, $var, ENT_COMPAT, $this->_charset);
}
return call_user_func($this->_escape, $var);
}
/**
* Method to get data from a registered model
*
* @access public
* @param string The name of the method to call on the model
* @param string The name of the model to reference [optional]
* @return mixed The return value of the method
*/
function &get( $method, $model = null )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -