How to setup multiple Layouts in Zend Framework. Eg. Public/Logged in/Various combinations of modules - zend-framework

I know & used the very basic Zend Framework's Layouts where I used 1 layout throughout the whole site. But now I need a more intermediate/organized setup.
The public site layout will have the div#mainContent taking up the whole 12 columns (using 960gs)
The logged in site will have div#mainContent taking up 9 columns + side bar with 3 columns
In the sidebar of the logged in site, various pages may contain various modules (not Zend Framework's modules, more like "boxes/widgets")
They will have different nav menus too
I am thinking of using 1 base layout where the 2 sub layouts will "extend". The base layout will just contain the <html> declarations headScripts etc till the <body> then the sublayouts will contain definations for the wrapping divs div.grid_12, grid_9, grid_3. How can I implement this "extending", basically, I just want to reuse code
Also whats a good way to render sidebar boxes/widgets

I'm switching between layouts depending on the subdomain of my website.
Here's the layout plugin I'm using...
class App_Layout_Controller_Plugin_Layout extends Zend_Layout_Controller_Plugin_Layout
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$layout = $this->getLayout();
$filename = $layout->getLayoutPath() . '/' . $request->getModuleName() . '.' . $layout->getViewSuffix();
//check if the layout template exists, if not use the default layout set in application.ini
if (file_exists($filename))
{
$this->getLayout()->setLayout($request->getModuleName());
}
}
}
Of course you can modify this for your own needs.
Make sure you set up you application.ini correctly too including elements like the following...
resources.layout.layout = "default"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
resources.layout.pluginClass = "App_Layout_Controller_Plugin_Layout"
In my case I have:
default.phtml, admin.phtml, clients.phtml
I hope this helps...
Angel

The way we are doing it, which I'm not sure if it's the best approach, is to set the current layout within the init() method available in each controllers.
So for example in case we have this URL: www.mysite.com/social/
class SocialController extends BaseController
{
public function init(){
$layout = $this->_helper->layout();
$layout->setLayout('social');
}
}
Then within the config.ini:
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
And there is a socia.phtml defined within the resources.layout.layoutPath

Related

How can I render preview of FE plugin diplayed in Page module

I have developed TYPO3 (6.2) extensions with some FE plugins.
I need to change informations about plugin, which is displayed in the backend on page view.
Now only Title and name of plugin is displayed ...
I have used flexforms for configure the plugin and I would like to show some of configuration on the plugin "placeholder" in the backend.
I remember, I read some documentation how to do it a few years ago, but I can't find it anymore...
Does anyone know the right way to do it?
If I understood well you are asking for ContentElement preview. You need to use cms/layout/class.tx_cms_layout.php hook for this, here's quite nice gist
just two additions:
don't use t3lib_extMgm class it's removed since 7.x you can register this hook just with:
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/class.tx_cms_layout.php']['tt_content_drawItem'][$_EXTKEY]
= 'EXT:your_ext/Classes/Hooks/PageLayoutView.php:\Vendor\YourExt\Hooks\PageLayoutView';
Depending on how did you register the plugin (didn't mention) you can also need to check the $row['list_type'] as your $row['CType'] may be just generic list.
Sample class with value from FlexForm field
<?php
namespace Vendor\YourExt\Hooks;
class PageLayoutView implements \TYPO3\CMS\Backend\View\PageLayoutViewDrawItemHookInterface {
public function preProcess(\TYPO3\CMS\Backend\View\PageLayoutView &$parentObject, &$drawItem, &$headerContent, &$itemContent, array &$row) {
if ($row['CType'] == 'list' && $row['list_type'] == 'yourext_yourplugin') {
$drawItem = false;
$linkStart = '<a href="#" onclick="window.location.href=\'../../../alt_doc.php?returnUrl=%2Ftypo3%2Fsysext%2Fcms%2Flayout%2Fdb_layout.php%3Fid%3D' . $row['pid'] . '&edit[tt_content][' . $row['uid'] . ']=edit\'; return false;" title="Edit">';
$linkEnd = '</a>';
$headerContent =
$linkStart .
"<strong>Selected slides</strong>" .
$linkEnd;
$ffXml = \TYPO3\CMS\Core\Utility\GeneralUtility::xml2array($row['pi_flexform']);
$itemContent =
$linkStart .
$ffXml['data']['sDEF']['lDEF']['settings.myFlexField']['vDEF'] .
$linkEnd;
}
}
}

Croogo : Different layout for different Locate

How to set different layout for different locate with "Translate Plugin"
ex:
with link: [http://example.com/eng] it should render default.ctp
for English
and with link: [http://example.com/ja] it should render
default.ctp for Japanese
One way you can do this is by reading the $this->request->params['locale'] variable, which is available in the Controller/AppController.php file, and then change the layout according to it. It would be something like this:
class AppController extends CroogoAppController {
public function beforeRender() {
// Some code...
// First, checks if the locale parameter is not empty
if(!empty($this->request->params['locale']))
// Then, sets the layout for each case.
// In this example, we user eng and ja
switch($this->request->params['locale']) {
case 'eng':
$this->layout = 'Croogo.eng';
break;
case 'esp':
$this->layout = 'Croogo.ja';
break;
}
// If it is empty, then loads the default locale layout
else
$this->layout = 'Croogo.default';
}
// The rest of the AppController code...
}
Note that I used the Croogo prefix for the layout files. I did this because I wanted the layout files to be loaded from within the croogo folder. In my installation, they are located in the path Vendor/croogo/croogo/Croogo/View/Layouts.
Hope it helps!

login page using zend framework custom layout

i will try to integrate css template in my zend project.i had integrate it with layout resources.it is working file..but now i want different layout when user render log in page and different layout after log in. i am confuse how to achieve different layout for different view.
this my bootstarp.php file which add layout to pages when render..how can i render diffrent layout for login page
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
function _initViewHelpers()
{
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
$view->doctype('XHTML1_STRICT');
$view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
$view->headTitle()->setSeparator(' - ');
$view->headTitle('Zend Framework Tutorial');
}
}
?>
You probably dont want to do it in the boo
Layout quickstart

How to disable layout and view renderer in ZF2?

How can i disable layout and view renderer in Zend Framework 2.x? I read documentation and can't get any answers looking in google i found answer to Zend 1.x and it's
$this->_helper->viewRenderer->setNoRender(true);
$this->_helper->layout->disableLayout();
But it's not working any more in Zend Framework 2.x. I need to disable both view renderer and layout for Ajax requests.
Any help would be great.
Just use setTerminal(true) in your controller to disable layout.
This behaviour documented here: Zend View Quick Start :: Dealing With Layouts
Example:
<?php
namespace YourApp\Controller;
use Zend\View\Model\ViewModel;
class FooController extends AbstractActionController
{
public function fooAction()
{
$viewModel = new ViewModel();
$viewModel->setVariables(array('key' => 'value'))
->setTerminal(true);
return $viewModel;
}
}
If you want to send JSON response instead of rendering a .phtml file, try to use JsonRenderer:
Add this line to the top of the class:
use Zend\View\Model\JsonModel;
and here an action example which returns JSON:
public function jsonAction()
{
$data = ['Foo' => 'Bar', 'Baz' => 'Test'];
return new JsonModel($data);
}
EDIT:
Don't forget to add ViewJsonStrategy to your module.config.php file to allow controllers to return JSON. Thanks #Remi!
'view_manager' => [
'strategies' => [
'ViewJsonStrategy'
],
],
You can add this to the end of your action:
return $this->getResponse();
Slightly more info on the above answer... I use this often when outputting different types of files dynamically: json, xml, pdf, etc... This is the example of outputting an XML file.
// In the controller
$r = $this->getResponse();
$r->setContent(file_get_contents($filePath)); //
$r->getHeaders()->addHeaders(
array('Content-Type'=>'application/xml; charset=utf-8'));
return $r;
The view is not rendered, and only the specified content and headers are sent.

Way to define others layout() parts such layout()->content?

Ive done some search but no success.. i am trying to figure out how to define others layout() parts such layout()->content variable.. i would love to get int layout()->navigation (a custom one) which display the navigation..
Any ideas ?
Thanks.
Not sure if this is what you want, but you can create additional 'parts' of layout simply by assigning a value to your new part. ZF will take care of the rest. For example, in a bootstrap.php you could do:
public function _initNewLayoutPart() {
$view = $this->bootstrap('view')->getResource('view');
$view->layout()->newpart = 'some new part';
}
Then in your layout.phtml you could just echo the new part:
<?php echo $this->layout()->newpart; ?>
It is possible by just creating a new variable in layout, you can define it in your controller (preferably in init or postDispatch). Just like this:
public function init()
{
$this->view->layout()->motd = '<b>Message of the day.</b>';
}
Then in your actual view where you want to see the message, all you have to do is:
<?php echo $this->layout()->motd; ?>
If you want something fancier, such as rendering a whole page or sidebar, try the following:
public function init()
{
$this->view->layout()->sidebar = $this->view->action('render', 'sidebar');
}
With render being the action (including render.phtml) and sidebar being the controller.