I am starting in zend framework 1.11. How do we pass different $data value in view from controller to view like in codeigniter we pass like this.
$data['pass_one_thing'] = $this->model1->pass_all_mangoes();
$data['pass_another_thing'] = $this->model2->pass_all_oranges();
$this->load->view('viewfile', $data);
then in views we get values of $pass_one_thing and $pass_another_thing with foreach loops in same view file.
how do i pass from different model function in a same view ?
How do we get such thing in zend ? I am new to zend and bit confused.
You set it in your controller as:
$this->view->myVar = "something";
And then access it from the view:
echo $this->myVar;
Or using assign like Wesley said.
That can be done pretty much the same:
$this->view->data = $data;
Or use the assign function:
$this->view->assign('data', $data);
edit:
How do I pass from different model function in a same view
Not exactly sure but taking your exact example:
$this->view->data['pass_one_thing'] = $this->model1->pass_all_mangoes();
$this->view->data['pass_another_thing'] = $this->model2->pass_all_oranges();
$this->load->view('viewfile', $data);
Then in your view you would access these trough:
$this->data['pass_one_thing']
$this->data['pass_another_thing']
Related
Controller:
public function home(){
$data['page_title'] = 'Home';
$data['navbar_content'] = $this->model->get($var);
$data['page_content'] = $this->model->get($var1);
$this->load->view('template/header');
$this->load->view('template/navbar');
$this->load->view('pages/home');
$this->load->view('template/footer');
}
Using the sample code above, Since codeigniter only allows the passing of variables/data through an array or object from controller to the view then what is the 'right' way to pass my data?
I could simply pass the data when loading the header and things will still work out fine.
$this->load->view('template/header', $data);
$this->load->view('template/navbar');
$this->load->view('pages/home');
$this->load->view('template/footer');
But that doesn't seem 'right' to me, i guess i wanted things to be clear in my code for example:
$this->load->view('template/header', $title);
$this->load->view('template/navbar', $navbar_content);
$this->load->view('pages/home', $page_content);
$this->load->view('template/footer');
The above is clear cut and there is a distinction to the data that are being passed.
But since i can't do that, is there a 'correct' way of doing this? or am i thinking too much on it?
I neglected to mention, I could simply make 2 arrays to pass to the view. I thought this is wrong in the first place since i'm going out of my way to make 2 arrays to pass just so i can pass a single string variable to my header page.
Ofcourse when i pass more data to my navbar then it would make sense to me.
This is the correct way according to the documentation:
https://codeigniter.com/userguide3/general/views.html
Data is passed from the controller to the view by way of an array or an object in the second parameter of the view loading method.
You should also look on:
Codeigniter: Best way to structure partial views
You should structure you views hierarchically.
$this->load->view('template/header', ["title" => $title]);
$this->load->view('template/navbar', ["navbar_content" => $navbar_content]);
$this->load->view('pages/home', ["page_content" => $page_content]);
$this->load->view('template/footer');
CodeIgniter passes data from controllers to views.
You need to pass $data to views.
You will pass an array with its keys be strings or even arrays (multi-dimensional arrays also).
So, you need to pass it to every view you are using $data.
$this->load->view('template/header', $data);
$this->load->view('template/navbar', $data);
$this->load->view('pages/home', $data);
$this->load->view('template/footer', $data);
And in your view, you can access $data with keys.
For example:
if your data is
$data['header_banner'] = 'Header Banner';
In your view, you can access this value by:
echo $header_banner;
Observe that to the key in $data, you just need to add a $ and you can access that variable.
Its something line PHP's in built function extract()
I am trying to figure out how to do the equivalent of the following in Laravel that I would do in CodeIgniter all the time to build views:
$section = $this->load->view('pages/about', $data, TRUE);
This would allow me to echo $section in another view file and then when that view was called the normal way, it would render it. I am not sure how to do something like this in Laravel.
UPDATE
I figured it out. What I was needing was Laravel's HtmlString class to take a string and convert it to html markup to the view file.
You would need to use the View Facade, so make sure to include it with an "Use" statement in your Controller, but basically is this:
$html = View::make('pages/about', $data)->render();
The render() method will just render the view in HTML, instead of returning it as a Response object like the view() helper function does.
There are several ways to do so, try this:
return view('admin.profile', $data);
Read through this doc:
https://laravel.com/docs/5.5/views
I have a piece of data I want passed to every view. I am using CodeIgniter 3 and have PHP 7 available to me. The current way I do it is using something like this in every function.
$data['foobar'] = $this->general_model->foobar();
// More code
$this->load->view('homepage', $data);
I'd prefer not to have to call $data['foobar'] = $this->general_model->foobar(); on every single function.
I've tried many approaches to fix this without resorting to anything that makes the code too goofy. I've tried constructors, autoload, and hooks. The problem in each case boils down to the fact that $data is local to each function. The best I've gotten is usually something like this.
$data['foobar'] = $this->foobar;
// More code
$this->load->view('homepage', $data);
This is slightly nicer, but it still results in me placing this line in every function.
I'd like my functions to in someway inherit $data with the index foobar already set. I'd prefer to avoid a solution that requires every function receiving $data as a parameter. How can I accomplish this?
Option 1:
Not sure if you have tried this but you could set $data as a property of your class
protected $data = [];
Then in your constructor set it.
$this->data['foobar'] = $this->general_model->foobar();
This would mean your $data becomes accessible to all your methods in your controller and you would need to refer to them as $this->data['data_name'] and use it in a view like
$this->load->view('homepage', $this->data);
Option 2:
A second way is to create a method like render() which is common to all your methods that load views and replaces your existing view calls.
So you would have something like...
public function one_of_my_methods(){
$data['content'] = 'This is content 1';
$this->render('test_view',$data); // Call the new view handler
}
// All methods using views now call this to load the final view
public function render($view,$data){
$data['foobar'] = 'I am common'; // DRY
$this->load->view($view, $data);
}
I'm wondering how $this->load->vars() works in CodeIgniter. The documentation is fairly vague about it.
I have the following code:
$init = $this->init->set();
$this->load->view('include/header', $init);
$this->load->view('include/nav');
$dates = $this->planner_model->create_date_list();
$this->load->view('planner/dates_content', $dates);
$detail = $this->planner_model->create_detail_list();
$this->load->view('planner/detail_content', $detail);
$this->load->view('include/footer');
However, I also need the $datesarray in my detail_content view. I was trying to load it with $this->load->vars() and hoping it would append to the $detail array, because the CI documentation states as follows:
You can have multiple calls to this function. The data get cached and merged into one array for conversion to variables.
Would it work if I do $detail['dates'] = $dates; ? Will it append the $dates array to $detail['dates'] then?
Thanks in advance.
$this->load->vars() is perfect for this purpose. Try this:
$init = $this->init->set();// Won't be passed to the next 2 views
$this->load->view('include/header', $init);
$this->load->view('include/nav');
$dates = $this->planner_model->create_date_list();
$this->load->vars($dates);
$this->load->view('planner/dates_content');
$detail = $this->planner_model->create_detail_list();
$this->load->vars($detail);
$this->load->view('planner/detail_content');
What looks strange to me is that normally you pass an associative array as data, like $data['my_var_name'] = $var_value, so I assume your model calls are returning the data already structured with the variable names (array keys) that you'll use in your view which I do find odd, but then I know nothing of your application.
Here's a more "conventional" version:
$data['dates'] = $this->planner_model->create_date_list();
$this->load->view('planner/dates_content', $data);
$data['detail'] = $this->planner_model->create_detail_list();
// receives both dates and detail
$this->load->view('planner/detail_content', $data);
Have you tried just just building an array that you pass to the different views? I find $this->load->vars() behaves unexpectedly.
As is stated in other answers, and in the user guide, using $this->load->vars() is the same as including the second argument in $this->load->view().
But from the user guide:
The reason you might want to use this function independently is if you would like to set some global variables in the constructor of your controller and have them become available in any view file loaded from any function.
This to me, is the only reason you'd use $this->load->vars(). As #madmartigan says, it's more convenient to use the view loader with the second argument.
Using PHP, If I have a model (a class) where I various queries, whatever I need, and in my controller, I use myModel = new CustomerModel(); and later in the controller, say I call myMyodel in the controller (I know looks like codeigniter but I am not using a framework) to:
$data['query'] = myModel.OrderByLastName();
how do I pass that $data['query'] to a view, a separate .php page?
I don't wan to echo anything from my controller.
Also, was hoping this design, the way I explained it makes sense. Or am I wasting time with the model class?
Typically, you'd instantiate a view object:
$view = new View();
Pass it the info it needs():
$view->set($name1, $value1);
$view->set($name2, $value2);
...
Then invoke the view's renderer:
$view->render();
The way Django works is the controller basically renders a template using a templating system. It passes the data in Contexts, like this:
data['query'] = myModel.OrderByLastName();
context = {'data': data['query']}
page = loader.get_template('folder/template.phtml')
return render_to_page(page, context)
roughly.
Obviously, you're writing your own system so you've got some room on exactly how you implement it. I don't know if that's exactly what you want, but it might give you a workable idea.