Adding automagic content-type-to-view-template mapping for use with Router::parseExtensions(). Also adding default XML template.

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@3411 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2006-08-18 08:05:00 +00:00
parent 2acff78b48
commit 866ea4bd5e
3 changed files with 91 additions and 20 deletions

View file

@ -1,6 +1,5 @@
<?php
/* SVN FILE: $Id$ */
/**
* Request object for handling alternative HTTP requests
*
@ -49,6 +48,8 @@ class RequestHandlerComponent extends Object{
var $__responseTypeSet = false;
var $params = array();
var $__requestContent = array(
'javascript' => 'text/javascript',
'js' => 'text/javascript',
@ -88,19 +89,51 @@ class RequestHandlerComponent extends Object{
parent::__construct();
}
/**
* Initialize
*
* @param object A reference to the controller
* @return void
*/
function initialize(&$controller) {
$this->params =& $controller->params;
if (isset($this->params['url']['extension'])) {
$ext = $this->params['url']['extension'];
if (isset($this->__requestContent[$ext])) {
$content = $this->__requestContent[$ext]['content'];
if (is_array($content)) {
$content = $content[0];
}
array_unshift($this->__acceptTypes, $content);
}
}
}
/**
* Startup
*
* @param object A reference to the controller
* @return null
* @return void
*/
function startup(&$controller) {
if ($this->disableStartup || !$this->enabled) {
return;
}
$this->setView($controller);
if (in_array('Ajax', $controller->helpers)) {
$controller->params['isAjax'] = $this->isAjax();
$controller->params['isAjax'] = $this->isAjax();
if (isset($this->params['url']['extension'])) {
$ext = $this->params['url']['extension'];
if (in_array($ext, array_keys($this->__requestContent))) {
if ($ext != 'html' && $ext != 'htm' && !empty($ext)) {
$controller->ext = '.ctp';
}
$controller->viewPath .= '/' . $ext;
$controller->layoutPath = $ext;
if (in_array($ext, array_keys($this->__requestContent))) {
$this->respondAs($ext);
}
}
}
}
/**

View file

@ -0,0 +1,2 @@
<?php e('<' . '?xml version="1.0" encoding="UTF-8" ?' . '>' . "\n"); ?>
<?php echo $content_for_layout; ?>

View file

@ -95,6 +95,13 @@ class View extends Object{
*/
var $viewPath;
/**
* Path to Layout.
*
* @var string Path to Layout
*/
var $layoutPath = '';
/**
* Variables for the view
*
@ -207,7 +214,27 @@ class View extends Object{
*/
var $plugin = null;
var $__passedVars = array('_viewVars', 'action', 'autoLayout', 'autoRender', 'base', 'webroot', 'helpers', 'here', 'layout', 'modelNames', 'name', 'pageTitle', 'viewPath', 'params', 'data', 'webservices', 'plugin', 'namedArgs', 'argSeparator');
/**
* Controller URL-generation data
*
* @var mixed
*/
var $namedArgs = null;
/**
* Controller URL-generation data
*
* @var string
*/
var $argSeparator = null;
/**
* List of variables to collect from the associated controller
*
* @var array
* @access protected
*/
var $__passedVars = array('_viewVars', 'action', 'autoLayout', 'autoRender', 'ext', 'base', 'webroot', 'helpers', 'here', 'layout', 'modelNames', 'name', 'pageTitle', 'layoutPath', 'viewPath', 'params', 'data', 'webservices', 'plugin', 'namedArgs', 'argSeparator');
/**
* Constructor
*
@ -247,7 +274,7 @@ class View extends Object{
}
if ($layout) {
$this->setLayout($layout);
$this->layout = $layout;
}
if ($file) {
@ -387,13 +414,12 @@ class View extends Object{
$pageTitle = Inflector::humanize($this->viewPath);
}
$data_for_layout = array_merge(
$this->_viewVars,
array(
'title_for_layout' => $pageTitle,
'content_for_layout' => $content_for_layout,
'cakeDebug' => $debug
)
$data_for_layout = array_merge($this->_viewVars,
array(
'title_for_layout' => $pageTitle,
'content_for_layout' => $content_for_layout,
'cakeDebug' => $debug
)
);
if (is_file($layout_fn)) {
@ -429,9 +455,7 @@ class View extends Object{
}
/**
* Sets layout to be used when rendering.
*
* @param string $layout Name of layout.
* @deprecated
*/
function setLayout($layout) {
$this->layout = $layout;
@ -554,11 +578,23 @@ class View extends Object{
}
}
if (file_exists(LAYOUTS . $this->subDir . $type . "{$this->layout}$this->ext")) {
$layoutFileName = LAYOUTS . $this->subDir . $type . "{$this->layout}$this->ext";
} elseif($layoutFileName = fileExistsInPath(LIBS . 'view' . DS . 'templates' . DS . "layouts" . DS . $type . "{$this->layout}.thtml")) {
if (!empty($this->layoutPath)) {
$path = $this->layoutPath . DS;
if (file_exists(LAYOUTS . $this->subDir . $this->layoutPath . $type . "{$this->layout}{$this->ext}")) {
$layoutFileName = LAYOUTS . $this->subDir . $path . $type . "{$this->layout}{$this->ext}";
} elseif (file_exists(LAYOUTS . $this->subDir . $path . $type . "default{$this->ext}")) {
$layoutFileName = LAYOUTS . $this->subDir . $path . $type . "default{$this->ext}";
} elseif ($layoutFileName = fileExistsInPath(LIBS . 'view' . DS . 'templates' . DS . 'layouts' . DS . $path . "default{$this->ext}")) {
} else {
$layoutFileName = LAYOUTS . $this->subDir . $path . $type . "default{$this->ext}";
}
} else {
$layoutFileName = LAYOUTS . $type . "{$this->layout}$this->ext";
if (file_exists(LAYOUTS . $this->subDir . $type . "{$this->layout}$this->ext")) {
$layoutFileName = LAYOUTS . $this->subDir . $type . "{$this->layout}$this->ext";
} elseif($layoutFileName = fileExistsInPath(LIBS . 'view' . DS . 'templates' . DS . 'layouts' . DS . $type . "{$this->layout}.thtml")) {
} else {
$layoutFileName = LAYOUTS . $type . "{$this->layout}$this->ext";
}
}
return $layoutFileName;