ok, i think i've solved the problem. It was quite obscure, so if anyone has
similar problems i'll explain it here.
I have the following front controller plugin that loads the correct layout
depending on the request compared to a value in the layout.ini:
Layout.ini
[layout]
layoutPath = "../application/views/layouts/"
contentKey = "content"
viewSuffix = "phtml"
layout = "site"
[site : layout]
layout = "site"
[admin: layout]
layout = "admin"
controllers = "News, Staff, Products"
...and a front controller plugin that reads this and loads the correct
layout file depending on the request:
LayoutPlugin:
<?php
/**
* @package Controller
* @subpackage Plugin
* @uses Zend_Config
* @see Layout.ini
*
* Layout plugin selects the layout to be set for the current action
defined in the layout.ini
* Especially useful for admin or auth'ed areas, instead of eg:
* <code>
* //auth'ed controller
* public function postDispatch(){
* $this->getHelper('layout')->setLayout('adminLayout');
* }
* </code>
* ... the controllers are assigned in the layout.ini, under the key
'controllers':
*
* The plugin loads the Layout object with the configurations defined in
the section that a controller -> request match is made
*
*/
class My_Controller_Plugin_Layout extends
Zend_Controller_Plugin_Abstract{
/**
* @var Zend_Config
*/
protected $config;
/**
* @param Zend_Config $config
*/
public function __construct(Zend_Config $config){
$this->config = $config;
}
public function preDispatch(Zend_Controller_Request_Abstract
$request)
{
$this->isControllerDefinedInConfig($request->getControllerName());
}
/**
* Checks whether the action belongs to a controller defined with a
specific layout
*
* @param string $controllerName From request
* @return void
*/
protected function isControllerDefinedInConfig($controllerName){
foreach($this->config as $configSection){
foreach($configSection as $key => $val){
if(strtolower($key) == 'controllers'){
$controllers = explode(',',
$configSection->$key);
foreach($controllers as $controller){
if(strtolower(trim($controller)) ==
strtolower(trim(str_ireplace(array('-'), '', $controllerName)))){
$layout =
Zend_Layout::getMvcInstance();
$layout->setOptions($configSection);
}
}
}
}
}
}
}
?>
A careful look of the above and you'll see that this is executed on the
controllers PostDispatch method - i changed it to PreDispatch, and
everything is working as it should, with just the .ajax.phtml view script
being rendered without the layout.
It seems like it was resetting the layout or something since the layout was
being modified after the action? Unsure...but fixed!
As a pointer - if the layout is still being rendered from AjaxContext, go
through your code to see if you're modifying the layout anywhere.
tony stamp wrote:
>
> I have been having some trouble obtaining the html view rendered by an
> action from an AJAX request. Initially, in my ajax-ed controller action, i
> was checking if the request was XmlHttpRequest, and if so, disabling the
> layout so just the portion of the view is returned. This was not working,
> so a little research found pointers towards the AjaxContext helper. I have
> set it up as follows:
>
> Controller:
> public function init(){
> parent::init();
> $this->flashMessenger =
> $this->getHelper('FlashMessenger');
>
> $ajaxContext = $this->_helper->getHelper('AjaxContext');
> $ajaxContext->addActionContext('view', 'html')
> ->initContext();
> }
>
> public function viewAction(){
> if(!($id = $this->getRequest()->getParam('newsID'))){
> $this->_redirect('index');
> }
>
> $newsTable = new News_Table();
> $select = $newsTable->select();
> $select->setIntegrityCheck(false);
> $select->from('news')->joinLeft('news_approved',
>
> 'news.id = news_approved.news_id',
>
> array('approver_id' => 'staff_id', 'date_approved'));
> $select->where('id = ?',(int) $id);
>
> $rowset = $newsTable->fetchAll($select);
> $this->view->assign('news', $rowset->current());
> }
>
> The view script rendered for this is called view.phtml, so as per the
> documentation, i copied the content and named a new view script
> view.ajax.phtml.
>
> Although the correct action is being called (from jquery, with format=html
> appended to the query string, although it would be nice to assume that no
> format parameter = html by default, unless this can be set somewhere?) and
> loading the correct ajax view script, it is not disabling the layout, and
> i am getting a "page within a page".
>
> Any pointers on what i'm doing wrong?
>
--
View this message in context:
http://old.nabble.com/Returning-HTML-content-from-AjaxContext-tp26148891p26157777.html
Sent from the Zend Framework mailing list archive at Nabble.com.