Zend Framework: View variable in layout script is always null - model-view-controller

I set a view variable in someAction function like this:
$this->view->type = "some type";
When I access this variable inside layout script like this:
<?php echo $this->type ?>
it prints nothing. What's wrong?
My application.ini settings related to layout
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.layout.layout = "layout" ; changed 'default' to 'layout'
Edit
This thread suggests the alternate solution, but looking for solution to above problem. And this was working in Zend 1.6.2. I just upgraded to 1.10 and it stopped working.
Edit
If I set this view var inside any _init Bootstrap function, it works.

If you want to assign something to your layout you have to go an other way:
// get the layout instance
$layout = Zend_Layout::getMvcInstance();
// assign fooBar as Name to the layout
$layout->name = 'fooBar';

I believe the layout view object and the action view object are separate instances of the Zend_View class.
I think this is the correct way to pass variables from the controller to the layout:
/**
* Controller action
*/
public function indexAction()
{
$this->_helper->layout()->assign('myName', 'John Doe');
}
and then in your layout script you can access the variables by referencing the layout object like this:
<html>
<body>
<?php echo $this->layout()->myName; ?>
</body>
</html>

Do you have the following entry in your application.ini file?
resources.view[] =
So, you can initialize the view with no options and use it through:
<?php echo $this->type ?>

Related

Laravel disable controller action layout

Is there a way to disable layout for certain controller method?
Im using something like $this->layout = null ,yet it still render the layout
The view im rendering obviously have a layout associate with it, i just wonder is it possbile to disable the layout from within controller method, without need to modify the blade file itself
Here is the controller:
class PurchaserController extends \BaseController
{
public function index()
{
$this->layout = null;
return View::make('purchasers.index');
}
}
The view:
#extends('layouts.master')
#section('content')
Content
#stop
Im using Laravel 4
Just remove
#extends('layouts.master')
from your view. That will prevent the view from loading.
Also - if you are using the #extends - then you dont actually need $this->layout() in your controller at all
Edit:
" i just wonder is it possbile to disable the layout from within controller method, without need to modify the blade file itself"
The idea is you do it either entirely from the controller, or entirely from the blade file. Not both together.

zend 2: Automatically inject a template for all actions of a module without modifying site wide configuration

I am using the Zend skeleton application.
I want to have a sub menu bar (via a template) injected underneath the default site wide menu bar, but I don't want to modify the site wide app settings, I'd like to just have it in the module.
Looking at examples it seems I would have to manually inject the menu bar template in each of my controller's actions and in every template where I want it to appear like this:
public function indexAction() {
$view = new ViewModel();
$subNavView = new ViewModel();
$subNavView->setTemplate('helpdesk/helpdesk/subNav');
$view->addChild($subNavView, 'subNav');
return $view;
}
public function someAction() {
$view = new ViewModel();
$subNavView = new ViewModel();
$subNavView->setTemplate('helpdesk/helpdesk/subNav');
$view->addChild($subNavView, 'subNav');
....do something add variables to $view....
return $view;
}
public function someOtherAction() {
$view = new ViewModel();
$subNavView = new ViewModel();
$subNavView->setTemplate('helpdesk/helpdesk/subNav');
$view->addChild($subNavView, 'subNav');
....do something add variables to $view....
return $view;
}
...etc
And the "echo $this->subNav" in every template.
Is this the right way to do this or is there a way to have my module automatically include this template for every page (without modifying anything outside of the individual module)?
I read the docs, but I'm still confused on how to achieve this or if this is even possible.
If you want to update the view model directly, you could also do that in onBootstratp of your Module class:
public function onBootstrap($e) {
$view = $e->getApplication()->getMvcEvent()->getViewModel();
$subNavView = new ViewModel();
$subNavView->setTemplate('helpdesk/helpdesk/subNav');
$view->addChild($subNavView, 'subNav');
}
And of course you already have something like this in your layout:
<?php echo $this->subNav; ?>
You could also consider using the standard partial view helper and setting the template path as a variable in your model from your Module.php like this:
public function onBootstrap($e) {
$viewModel = $e->getApplication()->getMvcEvent()->getViewModel();
$viewModel->subNav = 'application/navigation/subNav.phtml';
}
Then you modify your /module/Application/view/layout/layout.phtml something like this
<?php if ($this->subNav) {
echo $this->partial($this->subNav);
} ?>
The drawback to this idea is that then you have a view model variable which will show up in all your models. This can be annoying, for example, in json results.
Last idea, you might want to consider a navigation view helper. You could implement https://github.com/spiffyjr/spiffy-navigation if you don't want to build one from scratch.
If you solve it with a view helper, either custom or pre-existing package such as Spiffy Jr's, then you'd modify your layout so it uses the helper something like this, and all the logic is provided by the helper class:
<?php echo $this->navigationMenu(); ?>
All three ideas will unclutter your controllers and let your Module set up the subNav in a way that is relevant to it, such as which routes it is valid for, etc.

Can't see variable inside content view in Laravel 4 with Blade

I have a problem understanding how variables work inside the Laravel templating system, Blade.
I set the variables in the controller, I can see them in the first view I make, but not inside the next one.
I'm using a master.blade.php file that holds the header and footer of the page, yielding the content in the middle. I can use the variable inside the master.blade.php file, but not inside the content blade file (variable undefined).
For example, for the contact page:
CONTROLLER FUNCTION:
$this->data['page'] = "contact";
$this->layout->content = View::make('pages.contact');
$this->layout->with('data', $this->data);
MASTER.BLADE.PHP:
if ($data['page'] == 'contact')
{ //do something, it works }
#yield('content')
CONTACT.BLADE.PHP:
if ($data['page'] == 'contact')
{// do something. ErrorException Undefined variable: data}
Am I missing something or is it a known restriction?
Thanks!
The problem was that I was passing the variables only to the layout:
Instead of:
$this->layout->content = View::make('pages.contact');
$this->layout->with('data', $this->data);
I fixed it using:
$this->layout->content = View::make('pages.contact', array('data' => $this->data));
$this->layout->with('data', $this->data);
This way passing the variables to the layout, but also to that particular view.
Hope it helps someone.

codeigniter :load view inside div of a view

lets say that i hv this view (main)
<body>
lorem epsim
<div table></div>
lorem epsim
</body>
in controller control1.php i do
$this->load->view('header');
$this->load->view('main',$data);
$this->load->view('footer');
Now i need to load content of div=table from another view (tbl.php),which is called from another controller
control2.php
function load_table(){
$data['x']=1;
$this->load->view('tbl.php',$data);
}
tbl.php view
<ul>$x</ul>
how can i do that ?
i tried to load controler 2 from controller 1 and assign the function load_table to variable and pass that to main view, but it didnt work cuz load->view is executed instead of saving output to variable..
Reason:
i need to do this is that tbl.php view is a complex table that i need to refresh and load via ajax calls, so i need it to be on different view alone
so can some one explain to me how can i work this out ?
You can't call one controller method from another, separate controller. You can, however, get the output of the table view and use that:
// main.php
<body>
lorem epsim
<div table><?php echo $table_content; ?></div>
lorem epsim
</body>
.
// control1.php
$table_data['x'] = 1;
$data['table_content'] = $this->load->view('tbl.php', $table_data, TRUE);
$this->load->view('header');
$this->load->view('main',$data);
$this->load->view('footer');
So, you get the data to pass to the tbl.php view and pass that to the load->view method - also passing TRUE as the third parameter - which will return the contents of that view as a string (instead of outputting that to the browser). Now, you have a $data variable to pass to the main view with the table html included and you can just echo that out in the main view.
How you get the $data['table_content'] data from the view is up to you. You can create another controller method inside control1.php, you can create a helper file that can load the view into a string and return that, etc.
Maybe you can create a view with the table code within and then you can do inside your div for ajax
<div id="for_ajax">
<?php $this->load->view('table'); ?>
</div>
I've similar needs but mine its like a comments wall for issues.
Inside a view use following in a php block
$CI = &get_instance();
$CI->load->view('view_name');

how to load view into another view codeigniter 2.1?

Ive been working with CI and I saw on the website of CI you can load a view as a variable part of the data you send to the "main" view, so, according the site (that says a lot of things, and many are not like they say ...ej pagination and others) i did something like this
$data['menu'] = $this->load->view('menu');
$this->load->view ('home',data);
the result of this is that I get an echo of the menu in the top of the site (before starts my body and all) and where should be its nothing, like if were printed before everything... I have no idea honestly of this problem, did anybody had the same problem before?
Two ways of doing this:
Load it in advance (like you're doing) and pass to the other view
<?php
// the "TRUE" argument tells it to return the content, rather than display it immediately
$data['menu'] = $this->load->view('menu', NULL, TRUE);
$this->load->view ('home', $data);
Load a view "from within" a view:
<?php
// put this in the controller
$this->load->view('home');
// put this in /application/views/home.php
$this->view('menu');
echo 'Other home content';
Create a helper function
function loadView($view,$data = null){
$CI = get_instance();
return $CI->load->view($view,$data);
}
Load the helper in the controller, then use the function in your view to load another one.
<?php
...
echo loadView('secondView',$data); // $data array
...
?>

Resources