How do you enabled layouts for non-HTML templates in Symfony?
My Example
Relatively simple layout.xml.php:
<?xml version="1.0" encoding="<?php echo sfConfig::get('sf_charset', 'UTF-8') ?>"?>开发者_如何学Go
<example>
<?php echo $sf_content ?>
</example>
Simply isn't being used by any XML templates e.g. indexSuccess.xml.php
The Symfony documentation states:
The layout is automatically disabled for XML HTTP requests and non-HTML content types, unless explicitly set for the view.
Yet gives no documentation on how to set explcitly? Elsewhere obviously leads to:
all:
layout: layout
has_layout: true
But this seems to make no difference for XML templates?
Other sources mention sfAction having a hasLayout method, which clearly has been deprecated.
Evidently seems this isn't something that can be set globally via YAML (which is sad).
You can set it as stated in the documentation per view, i.e. in view.yml:
indexSuccess:
layout: layout
has_layout
But this is pretty laborious if you have many actions and against DRY concepts.
Note: Setting the values for all
takes no effect.
$this->setLayout('layout')
Does work within an action, but again in my scenario this would need to be set in every action, again not particularly DRY.
Thus, I chose to extend sfActions and bind it into the preExecute method.
class myActions extends sfActions {
public function execute($request) {
if($request->getRequestFormat() == 'xml') {
$this->setLayout('layout');
}
return parent::execute($request);
}
}
Sorts the problem globally if you make sure all your actions extend myActions
instead of sfActions
, if you want to do it for all formats you could make use of the preExecute
method instead, but I wanted to make use of the sfWebRequest
to ensure I don't try and force layouts onto prospective other formats I may add such as JSON.
Might this be part of "setting it explicitly for the view"?
$response = $this->getResponse();
$response->setContentType('text/xml');
http://www.symfony-project.org/gentle-introduction/1_4/en/07-Inside-the-View-Layer
精彩评论