How to modify data in array before sending to view? - php

I currently send some data as an array from my CodeIgniter controller to a view:
//Send data to template
$this->load->view('generator/content', $data);
Where $data is:
$data = array_merge($page, $posts);
I wish to modify some of the data in $posts before sending it to the view, by calling a function/method inside my library:
if (!empty($posts)) {
$posts = $this->my_library->modifyPosts($posts, $page_ID);
}
The stuff inside the function/method includes str_replace, exploding strings, time formatting and generally turning the data from the array into a usable format. From what I understand, it's best to do this stuff using a library.
Is there a way I can rebuild and return the array to the controller from the library, so that when I pass the data to the view it is ready to be presented?

Well you should be calling the library function from the controller anyway. So in the controller you might have
$posts = $this->my_library->modify_something($posts);
and then your library can do it's thing and return $posts in the new "usable format"
Then it's just fine from there, add the data to the $data array and pass it to your view.

Related

Laravel: How can i pass data from controller to blade using yajra/laravel-datatables

I am using yajra/laravel-datatables. It's successfully installed. Now, how can i pass data Laravel Controller to blade??
$contacts = Contact::Where('is_deleted',0)->get();
This is the code. There are lots of data. For those reason, It takes lots of time to load in dataTable. I want to load and pass data to dataTable using yajra/laravel-datatables.
There are many option which you can use
compact method
$data = Contact::Where('is_deleted',0)->get();
return view('home',compact('data'))
with method
$data = Contact::Where('is_deleted',0)->get()
->with('data',$data);
For more information, read the documentation

Laravel equivalent or alternative for CodeIgniter

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

CodeIgniter $this->load->vars()

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.

How to get data from controller to view?

I need to pass data from controller to view. I used loop in controller which run more than 1 minute.
for($i=0;$i<$count;$i++){
$peicedata = getdata();
$ar=explode(",",$peicedata);
$data['firstname']=$ar[0];
$data['lastname']=$ar[1];
$data['email']=$ar[2];
$data['website']=$ar[3];
}
above function getdata() take around 10 second to get data back. when data get from getdata() i want to pass that data immidiate to view.
Are you passing the data to a view?
$this->load->view('view', $data);
http://codeigniter.com/user_guide/general/views.html
It seems you use CodeIgniter. You can pass data to template like this:
$this->load->view('show', $data);
show is the template. And please read User Guide for more details.
BTW, if your script runs too slow, you should check your code.

Returning Data with a Zend Framework View Helper

I am creating a view helper to deal with returning some data screen scraped from elsewhere and my question is, what is the best way to gather the collected data and return it for use in the view?
Assume the view helper goes out and successfully grabs a few divs full of data.
Should I return an array of data and iterate over that in the view?
$i = 0
foreach($this->myHelper as $noob) {
echo '<div><h3>'.$noob[$i][0].'</h3>'.$noob[$i][2].'</div>';
$i++;
}
or perhaps build the output in the view helper and return the whole lot as a string?
echo $this->myHelper;
/* giving me something like:
<div><h3>Foo1</h3>bar1</div>
<div><h3>Foo2</h3>bar2</div>
*/
or return an object rather than an array or some other way I am missing (or screwing up in the above examples)? Thanks in advance.
I'd add a view partial argument and pass the data to the nominated partial script, returning the result using the partial or partialLoop helpers (depending on your data), for example
<?php echo $this->myHelper('path/to/partial.phtml') ?>
and in your helper
return $this->partial($partialScript, $data);
Edit If you wanted to simplify things, you could make the partial argument optional and if it was missing, build your HTML string internally.
The basic ideas with view helpers is that they return strings or objects that implement __toString()

Categories