Get only one section Laravel4 - php

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.

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
}

Update Laravel View dynamically

I am loading data from an API through JS send the data to the Laravel Controller and save them into the Database. After loading all the Ajax I want to display the data in a subview/section of the master.blade - Is it possible to render a View dynamically after the page finish loading, - also for later I want to update database rows and display the new data in the view dynamically.
//afater Ajax loading - update / display the data in the view
public function loadcomments() {
$comments = Comment::all();
$childcomment = Childcomment::all();
return View::make('partial')
->with(compact('comments'))
->with(compact('childcomments'));
}
in user.blade.php (main site) I am defining
#section('comments')
#include('partial')
#stop
and in the master.blade.php I am defining the yields:
#yield('content')
#yield('comments')
any idea how to render the site with the updated content?
Once the page has finished loading, with out making further AJAX calls to the Laravel app its self it will have no further part in the request.
You could just update the front end markup with JS or you can make a call back to your Laravel application using AJAX/jQuery to pull the data back out after it has added it to the database.
Use a resource controller or similar implementation to allow insertion and reading of the comments (CRUD) so you can pull data when you need using AJAX.
Edit
There are different ways to make a page dynamic on the front end all of these will usually include Javascript or another front end scripting language.
In the past I have used jQuery to handle updates of the page content using either JSON or XML/HTML but lately I have started to use AngularJS.
Another library is EmberJS which I am currently learning to use but I feel front end languages are out of scope for the question.
There are many tutorials on updating the HTML after a page has loaded by making a call back to a controller or other resourceful route.
Say the posts have been saved to the database, if this is done AFTER the view has been returned to the browser you WILL have to use javascript to pull out the data and most likely have a piece of js code to tick over that "polls" your resource controller for new comments.
If a new comment has been detected a second request is made to pull the comments out OR the comments are returned from the polling requests using AJAX.
On the laravel side, a partial view could be returned or JSON, for simplicity we'll replace the view. You would make a jQuery selector for the current existing partial on the browser and then replace it with the one pulled from Laravel with AJAX.
$('#some-page .my-view-to-update').html(somedata);
My jQuery is VERY rusty so read up on the relevant documentation to update the HTML correctly.
You can also use Pusher
take a look at this
https://pusher-community.github.io/real-time-laravel/

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 include the same form in multiples views?

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.

How PHP MVC should look like with jQuery/javascript code?

Well, I've read this tutorial if I could say: http://www.symfony-project.org/book/1_1/02-Exploring-Symfony-s-Code
And, actually, I write my code very similiary. But my question, where should I insert my jQuery code? I am sure it should be in the part of the View, but are there any good examples on how should I combine it to make "live" websites?
Edit: By saying live, I mean, for example, send POST request through Ajax and get information and similar. So, no refreshes for people.
Thank you.
jQuery as a part of javascript resources should be included in html.head or in-place, depending on what should jquery do and if the code is reusable for more views.
So it has to be part of View and you're choice is to set it up in layout or action view itself
If you need the javascript on every page then add it to your master view if not then just include it to the particular view files.
In context to codeigniter:
I extend the controller with MY_Controller and initialize a property as array which would hold the scripts that are added dynamically to the view.
eg.
var $templateData['scripts'] = array();
The controllers then pass $this->templateData to the views
And the views load the extra scripts( available as $scripts) as directed by the controllers in the head tag
This way you can even avoid loading the main jquery file when not needed.
Loading jquery.js only for the controller that need it.

Categories