Passing data to view from controller using templates - php

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()

Related

Passing more than one array to more than one view

I'm trying to load a controller wich loads two two different views, and passes a different array to each view.
public function index(){
$this->load->view('Head');
$data=$this->DAOPromotor->recuperarPromotor();
$eventos=$this->DAOEvento->recuperarPromotorEventos();
$this->load->view('MostrarPromotor', $data);
$this->load->view('MostrarPromotorEventos', $eventos);
$this->load->view('Footer');
}
This is my Controller. Now, the first view ("MostrarPromotor') loads just fine, but the second one keeps throwing an Undefined Variable warning. I've used the recuperarPromotorEventos() method on differnt controllers, and it works just fine. I treat the array the same way on both scripts, but it only works in one.
I'm wondering if maybe it's not possible to have two views reciving data at the same time.
Found a solution after looking around some more: when you pass an array to a view, it breaks down said array on its different components, using the index names of the array as references. So, if I had this array:
$eventos = array(
'data1' => 'someData',
'data2' => 'someMoreData'
);
When I pass that array to a view, the view can access the variables $data1 and $data2, but not the $eventos data. As my original array didn't have any index named eventos, there wasn't any variable called that.
As for my particular problem, the only thing I had to do to fix it was to change the code to this:
public function index(){
$this->load->view('Head');
$data=$this->DAOPromotor->recuperarPromotor();
$eventos["eventos"]=$this->DAOEvento->recuperarPromotorEventos();
$this->load->view('MostrarPromotor', $data);
$this->load->view('MostrarPromotorEventos', $eventos);
$this->load->view('Footer');
}

Best practise for passing data to subview codeigniter

I am looking for the best practise when it comes to passing data from the controller to a subview in Codeigniter, so far I have created a layout file which loads the subviews. For example, when I have been doing this, it does work but I am wondering if there is a cleaner way of doing things? Or are there any better options for templating my html and CSS without having to do this for every function?
Currently renaming each separate $data variable to $var1, $var2, $var3 etc.
Here is my code when loading pagination and passing this to the subview.
function index($start=0)
{
$var1['posts']=$this->post->get_posts(3,$start);
$this->load->library('pagination');
$config['base_url']=base_url().'posts/index/';
$config['total_rows']=$this->post->get_posts_count();
$config['per_page']=3;
$this->pagination->initialize($config);
$var2['pages']= $this->pagination->create_links();
$var3=array('subview' => 'post_index');
$data=array_merge($var1, $var2, $var3);
$this->load->view('layouts/layout',$data);
}
And in my template file I use this line of code in the body to load the subviews:
<?php $this->load->view($subview); ?>
Okay there indeed is a way to do that. Assuming you have a view called post_index in which you pass $var['posts'] and then you want to put that view in your master layout.
$var['posts'] = $this->post->get_posts(3, $start);
// setting the third parameter as true would return the view as
// data which you can pass to another view
$data['page'] = $this->load->view('post_index', $var, TRUE);
$this->load->view('layouts/layout', $data);
From codeigniter docs
Returning views as data
There is a third optional parameter lets you
change the behavior of the method so that it returns data as a string
rather than sending it to your browser. This can be useful if you want
to process the data in some way. If you set the parameter to TRUE
(boolean) it will return data. The default behavior is false, which
sends it to your browser. Remember to assign it to a variable if you
want the data returned.

how do we pass data from controller to view in zend?

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']

How does CodeIgniter send information from a Model to a View with the Controller?

When I call a controller and it calls the model, model returns information from my database assigned to something in the controller.
But how does it "send" it to the view for rendering? How for example, when I send $data array to my_view.php. how does it get to that page so that, I am guessing, I can do things like use extract to get my individual variables.
I'm really asking at the php level, how would you send that data (so I can learn). How does that view know what I sent it?
Thanks.
You have to "send" that $data array to the view as the second parameter when you load it.
$data['user'] = array(
'name' => 'Tom Jones',
'gender' => 'male'
);
$this->load->view('blogview', $data);
Then, the contents of the array are accessed within the view by their corresponding key values
<?php echo $user['name']; ?>
Checkout out the docs for more details: http://codeigniter.com/user_guide/general/views.html
The general pattern of all php views is this:
function render_view($__filename, $__data) {
extract($__data);
include $__filename;
}
This is basically how CodeIgniter does it, but it uses a loader to find the view filename and includes output buffering options.

storing a rendered element in a variable in CakePHP

I am having some trouble trying to "capture" the rendered html of an elmenet in cake php.
Say I have an element named "message.ctp"
I would like to do something like the following:
A making a $.getJSON request to an action in a controller say jsonAction(). Within this action I will perform some DB updates and return a json string. I would like to store the html is a part of the json object. Doable?
function jsonAction() {
//Do DB update
if(db update was ok) {
$response = array("completed" => true, "html" => $this->render("message"));
} else {
$response = array("completed" => false);
}
echo json_encode($response);
}
What seems to be happening right now is that the render method echos the rendered value instead of returning it.
Anyway I can achieve this?
Thanks
Regards
Gabriel
Forget elements for the time being.
First of all you have to separate everything that includes outputting stuff from the controller (either it is HTML or JSON or whatever).
For every controller action you have to have a corresponding view. So, for controller action jsonAction you should have a view names json_action.ctp (at the corresponding folder, e.g. if jsonAction is at MessagesController create a folder named /view/messages/json_action.ctp).
Set your variable from controller, display it at view and you are done. Do not forget to $this->layout = 'empty' from controller so that you display only what you have at the view.
Generally you should redo the CakePHP tutorials and reread the book it order to understand better the MVC (Model-View-Controller) pattern and CakePHP structure.
Do you mean this?
$myRenderedHtml = $this->element('message');
^^^^^^^

Categories