What is the difference between view and render? - codeigniter

What is the difference between view and render in codeigniter?

some template libraries use $this->template->render(); to output the rendered content based on your template. (you must have the template library installed obviously)
function index()
{
$this->template->set_template('main_template');
$data['content'] = 'hello this is my content';
$this->template->write_view('content', $data);
$this->template->render();
}
is in effect the same as
function index()
{
$data['content'] = 'hello this is my content';
$this->load->view('template/header');
$this->load->view('template/content', $data);
$this->load->view('template/footer');
}
template libraries save the need to load each partial view every time.

Render is not an out-of-the-box Codeigniter function for loading view files. Render is primarily used by Codeigniter template libraries like Collin William's Template Library or Phil Sturgeon's Template Library.
The following two methods for view files are supported by Codeigniter right in the core code without using third party libraries or core extensions.
$this->load->view()
The standard and most used way of loading a view file. Does not support any fancy syntax except alternative control syntax structured PHP code or standard PHP code.
$this->parser->parse()
Using the in-built Codeigniter parser which supports a Smarty like syntax only not as powerful. Also allows you to code view files using standard PHP and HTML as well.

Related

How to dynamically load/render Laravel Collective form?

I'm devising a small CMS for the in-house development team, where all have experience working with laravel.
The CMS requires a small feature where the services that are listed can have a quotation form attached. Instead of creating a completely separate module to add elements & other separately, I wanted to have the system in a manner that the developer adds the laravel collective form code, which we store in the database.
While retrieving, we render the form server side.
Here's my implementation
Controller
public function show($id)
{
$data['page'] = Service::where('slug', $id)->first();
if ($data['page']) {
....
$data['form'] = $data['page']->quoteform()->first();
....
And in the view
{!! $form['html'] !!}
But this is definitely won't help, so I tried this approach of rendering the collective form
$data['form'] = View::make('website.includes.render-form',['form'=>$data['page']->quoteform()->first()]);
But I'm not sure if this should work, as I couldn't make it work.
Looking forward to a solution if at all the approach I choose is possible, if yes or can be done, would like to know more on the same.
TIA
Edit 1:
I used the following blade command Blade::compileString('string here') which helped to a certain extent.
where I'm getting the following result
You can render the HTML of a view using render() method. So you can update to this :
$data['form'] = View::make('website.includes.render-form',
['form' => $data['page']->quoteform()->first()]
)->render();
Now the $data['form'] will have rendered HTML which you can use in blade using {!! $data['html'] !!} if you are passing $data to your main view.

Generating a PDF Report from my Web Application in CodeIgniter

I have developed a web application in CodeIgniter which contains a good number of Functionalities such as: drawing charts using the JavaScript library Amcharts, implementing an Offline Map for data presentation and many more. What I want now is to be able to generate a PDF report for my pages, which contains some charts and a description of the chart when a user clicks on a button. I have searched for many libraries such as HTML2PDF, jsPDF, but none of these libraries seem to meet my needs. A screenshot of my page is shown
here. I really need recommendations on libraries which could help do what I want, if anybody has been in such a situation.
Try Dompdf. Easy to use and works well with CodeIgniter. We've used this for a couple of our customers without any problems.
https://github.com/dompdf/dompdf
Make sure to download 0.6.x the 0.7 and higher won't work since it uses namespaces.
To use this make a helper function like so:
defined('BASEPATH') OR exit('No direct script access allowed');
function pdf_create($html, $filename = 'example', $stream = true)
{
require_once('dompdf/dompdf_config.inc.php');
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
if ($stream) {
$dompdf->stream("$filename.pdf");
} else {
return $dompdf->output();
}
}
and in your controller you would call it like so:
$this->load->helper('dompdf');
pdf_create('Hello!');

codeigniter - loading a library from a view?

I have some data that I have to display as a table.
I think I should pass the data from the controller as $data['events'] = array(.....
and then load the view to display them.
<?php
$this->load->library('table');
echo $this->table->generate($events);
?>
this doesn't work though - it gives a Fatal error: Call to a member function generate() on a non-object
If I paste the same code in the controller, obviously using ->generate($data['events'] the table gets displayed correctly.
Should I get that views can't load libraries, or I am doing something wrong?
Or maybe should I capture the output of the library in the controller and send that to the view?
If you need to call a library (and its functions) within a view, you can do this:
$CI =& get_instance();
$CI->load->library('library_name');
$CI->library_name->yourFunction();
You should run below code in the controller:
<?php
$this->load->library('table');
echo $this->table->generate($events);
?>
and store the data in variable and then send to the view.
To answer what you are doing wrong, you should know that the CodeIgniter class isn't declared in the view, and that this is the case for a reason - to abstract your PHP code from your HTML. Views should contain minimal PHP code (basic loops, conditions).
With this in mind, you should include your library as normal in the controller as follows;
controller
$this->load->library('table');
$data['events_table'] = $this->table->generate($events);
$this->load->view(....);
In the view, you simple echo the data. Although CodeIgniter allows short-hand tags, you should use standard PHP tags to keep to a convention that will work anywhere you keep your code.
view
<?php echo $events_table; ?>
You can auto-load the library in config/autoload.php
$autoload['libraries'] = array('database','session','table');
Then you can simply call the functions in your view .
echo $this->table->generate($events);
This was useful for me when i was creating a dynamic menu header . i auto-loaded the library which has the function for dynamic menu creation and then i simply called that function from the view , by the way it is bad practice .
That is one wrong approach to MVC. You don't need to load the library in the view, because all views are loaded from one CONTROLLER, so every external Helper or Library should be loaded from the controller and the used or send to the views
Regards,
Pedro
Note the shoulds: CodeIgniter is flexible in that it lets you do things the wrong way. It just makes it a little more difficult. You can do almost everything in the view, even when you shouldn't; but loading helpers, models, and views has to be done in the controller.
In controller or model use
$this->load->library('mylib');
$mylib=$this->mylib;
$data['mylib']=$mylib;
$this->load->view('myview',$data,false);
and than in view you can use library directly:
$mylib->myfunction();
It's not MVC like solution but works if you need.
Simply load library on controller, then use it view.
Controller:
$this->load->library('table');
$data = array(
array(1,2,3,4,5),
array(1,2,3,4,5),
array(1,2,3,4,5),
);
$this->load->view('the_view', compact('data'));
View:
echo $this->table->generate($data);

What is the best practice for displaying multiple views in Codeigniter?

I'm trying to determine the best practice for calling multiple views from the same method in a controller.
Is it preferable in the controller to make one view call, then have that view call all the views it needs, or call all the views you need in sequence in the controller?
Example:
function index(){
//set all data variables
//then send them to the view
$this->load->view($index_view, $data);
}
or
function index(){
//set variables
//then call each view
$this->load->view($header_view, $header_data);
$this->load->view($body_view, $body_data);
$this->load->view($footer_view, $footer_data);
The Codeigniter guide shows both ways, but does not seem to advise to the best practice...is there one?
I didn't like the way of including the header/footer within the view, and I didn't like loading the footer and header each time in every single Controller function.
To fix this, I extended the Controller class with my own view display function.
<?php
// in application/libraries/MY_Controller.php
class MY_Controller extends Controller {
function _displayPage($page, $data = array()) {
$this->view->load('header', $data);
$this->view->load($page, $data);
$this->view->load('footer', $data);
}
}
?>
// in application/controllers/home.php
<?php
class Home extends MY_Controller {
function index() {
$this->_displayPage('home/index', array('title' => 'Home'));
}
}
?>
Not sure if this is CodeIgniter "best practice" but it makes sense to me.
I don't think there is a definitive answer for that. Choose one and stick with it, it's important to be consistent.
Anyway, I'd prefer the second one.
I would say that the controller should only display one view. Then it's up to the view if it wants to show a header, footer, sidebar or whatever. The controller shouldn't have to care, its job is to get data from a model and hand it to a view. Not decide if the view should have a header and a footer.
Agree with Christian Davén: its view / display logic not data or business / logic. essentially its the same as using php includes for snippets like navigation, footer etc. you're just embedding markup.
This is expected behavior. Once variables are set they become available within the controller class and its view files. Sending an array in $this->load->view() is the same as sending an array directly to $this->load->vars() before calling the view file. This simplifies things for most people using multiple views in a controller. If you are using multiple view files in a single controller and want them to each have their own set of variables exclusively, you’ll need to manually clear out the $this->load->_ci_cached_vars array between view calls.
A code comment in the Loader class describes another situation showing why this is the desired default behavior:
You can either set variables using the dedicated $this->load_vars()
function or via the second parameter of this function. We'll merge
the two types and cache them so that views that are embedded within
other views can have access to these variables.

CI: Use view/controller in another included view?

I'm new to CI & PHP.
I have an auth library included, and works great stand-alone.
I simply want to have the login form load as a view inside another view...is that weird?:
I'm quasi-templating:
index:
$this->load->view('head_content');
$this->load->view('stuff');
$this->load->view('footer');
Inside the stuff view:
<stuff></>
$this->load->view('login_view');
<morestuff></>
I just want the login form to show up on the front page, and then tie into the auth system...
You have to load the login view in the controller, and then pass the data to the stuff view.
In the controller:
$this->load->view('head_content');
// the line below will save the output of the login view to $data['login']
// instead of outputting to the screen
$data['login'] = $this->load->view('login_view', '', TRUE);
$this->load->view('stuff');
$this->load->view('footer');
In the stuff view:
<stuff>
<?php echo $login; ?>
<morestuff>
In the page Views, at the bottom, check out the section Returning views as data
I've just recommended this but I'm going to recommend it again:
http://codeigniter.com/forums/viewthread/77279/
Django-like template inheritance helper for CodeIgniter.
I use this on ALL of my CI projects and it makes things like this stupidly easy.

Resources