How include the same form in multiples views? - php

I have a newsletter register FORM, I want include the same FORM for all the views in my CodeIgniter project without repeat code in each controller for each view, must be only one controller for handle the form.
Load the view or include in another views is not the problem, the problem is not repeating the handling (js validation and record data in the database).

You can include more than one View per function, as well as call views within views.
$this->load->view('template');
and within template...
$this->load->view('header');
$this->load->view('form');
$this->load->view('footer');

Have the form be a independent view all by itself.. and on a per view basis where you want to include it in.. you can either include() it in or load it like a view within the view $this->view->load() (may be wrong with that syntax off hand but yea) generally thats how you would/could do it.

Related

codeigniter, views are never called directly, can someone explain

I just started to use codeigniter. I need to know the explanation of
Views are never called directly.
Is it mean that i can not use $this->load->view('My_view') into another view?
I created a project in core php and decided to convert into codeigniter. In my project a page has different section so i created a main file recipes.php in views. I also created a folder where i put different section files to include in recipes.php. In my controller i loaded recipe view, it showed the recipe page then inside recipes view i used $this->load->('categorymenu'). It worked fine. I didnt pass any data since this section contains simple html. But im confused that it is not a right way of loading views.
So please can someone explain in detail. Am i doing the right thing or is there another way of doing it.
I also loaded the view in controller and passed to main view in controller which also worked perfect. But as i mentioned im not sure which is the right approach. My apology if this is a stupid question.
As you are naive to Codeigniter no question asked by you is stupid.
Now the answer of your question is that,
You can load a view inside another view and it is absolutely acceptable as there will be some cases when you have to put header in all pages of the web site, that time you have to load the view in another view.
Second is that the correct way to load the view is in the controller. As the controller is the mediator between the model and view. if you want some data of database to be displayed in your page you have to get that data through the function created in the model and then after you can load the view in controller and can pass the data in that.
This is code of controller
public function temp()
{
$data['recs']=$this->my_model->my_function(); //getting data to pass in view
$this->load->view('my_view',$data); //load the view
}

How to render a template in a controller?

I have a blade template called 'main' and I wonder how I can render a sub template by calling a controllers method in my main template. Lets say I have a Controller WidgetsController with a method getSubView. The method returns a specific view with some data from (for instance) a database.
I already tried to #include a template but this will not call the controller which sets some necessary data to the view.
Thanks.
views don't call services, they only take variables and put them on screen for presentation.
you're on the good side with #include(). you only need to gather the infos for that sub view beforehand in the controller, and pass it to the View::make('main')->with($vars).
you may also consider using another <?= View::make('subview')->with($vars->sub);?> within the the view. or just use the #extend functionality.
I don't entirely understand your question.
I think what you are looking for is a View Composer
It allows you to get data for a subview without having to create the data in every controller.

how to call action from a view of another action?

how I can call an action from the view of another action, knowing that the 2 actions are belong to the same controller ?
the controller is named FilesController, and the requested action is named Subscribing.
Actions shouldn't really be called from within the view. The view should only have data fed into it that will be represented by the view code itself. Views can, however, call other views. You may want to look at how your logic is structured to see if there's a different way to go about doing this.
There is one way, though, and that's via ajax. You can make an ajax call via Javascript/jQuery to a controller, and then have that controller send back a view, which is then placed into a specific area of the original view file.

Include A View Of Another Controller Within Views/site/index

I can't figure out how to solve this problem i'm facing. Alright, so basically i have a default view/site/index.php view file which is generating the home page. Now i have built two separated Models and their associated Controllers and views. I want to include those view of the controllers in the default home page view i.e. views/site/index.php. Does it make any sense ?
So my home page is divided into several sections. The header and footer parts are the same throughout the whole site, which is why i've written them in the /themes/brushed/layouts folder under separate files. Those are being loaded fine.
The two controller i was talking about are named as FrontPageCategoriesController.php and TopProductsController.php. They are generating their separate view files i.e. index.php in their respective view folders.
How will i include those view files (index.php) into the main site view file (views/site/index.php). I just can't include those files using include_once() because the data is being generated yet by its controller. So whats the way to achieve this ?
Can anyone please help me out with this. Thanks in advance.
First you should move data generation from controller to model. Controller should only wire your input parameters to model and pass it to view. The reason is that you want to reuse models, not controllers. Remember that controller is bound to page request, while model is not.
Then just use your model to fetch the data and renderPartial to render the data. For example:
$categories = FrontPageCategories::model()->findAll();
$this->renderPartial('/frontPageCategories/index', array(
'categories' => $categories,
));
In fact it is possible to fetch categories from model directly in view, if they are not dependant on some parameters in the current page.
Actually, you should use renderPartial for your header and footer too.

Get only one section Laravel4

How can I get the contents of een section in blade (Laravel 4). This is ideal for using AJAX requests, so I can return just the contents of an section. There are several methods that are related to sections, but they all return nothing.
My question: How can i get the content of an section of an view in my controller?
I think I would tackle that by having a view dedicated for the section, then either import that view into another view for your regular page loads, or for AJAX route to a function that just uses the view by itself.

Categories