render data from controller before view - laravel

I have a project that needs to render data from the controller before pass to view on every page
Ex. controller --data--> {do something} --> view.blade
I tried middleware, but it's not working, because it ??? before the controller.
Does anyone know how to achieve this?

You might want to use the render method from a Illuminate\View\View object:
class QuestionsController
{
public function show(Question $question)
{
$content = view('questions.show', compact('question'))->render();
// Do something with the $content string.
}
}

Related

Laravel disable controller action layout

Is there a way to disable layout for certain controller method?
Im using something like $this->layout = null ,yet it still render the layout
The view im rendering obviously have a layout associate with it, i just wonder is it possbile to disable the layout from within controller method, without need to modify the blade file itself
Here is the controller:
class PurchaserController extends \BaseController
{
public function index()
{
$this->layout = null;
return View::make('purchasers.index');
}
}
The view:
#extends('layouts.master')
#section('content')
Content
#stop
Im using Laravel 4
Just remove
#extends('layouts.master')
from your view. That will prevent the view from loading.
Also - if you are using the #extends - then you dont actually need $this->layout() in your controller at all
Edit:
" i just wonder is it possbile to disable the layout from within controller method, without need to modify the blade file itself"
The idea is you do it either entirely from the controller, or entirely from the blade file. Not both together.

CodeIgniter Dry Navigation

My boss told me make dry navigation dont use repetitive code, for navigation i am trying to extend CI_Controler and with construct load header nav, body, footer files.
My question is when i create new controller and when i try to load different view files, how to achive that???
my extended controler
class MY_Controller extends CI_Controller
{
public function __construct() {
parent::__construct();
$this->load->view('view_header');
$this->load->view('includes/nav_home');
$this->load->view('view_home');
$this->load->view('view_footer');
}
}
and later when i am creating new controler how to load diffrent view files
class Pages extends MY_Controller
{
public function __construct() {
$this->load->view('includes/nav_new_view');
$
}
}
You can create a template library yourself. For example :
function template($view_name,$view_data){
//below will return html string from view name
$data['content'] = $this->load->view($view_name,$view_data,true)
//load main template view and pass the html string to main template
$this->load->view('main_template',$data);
}
In main template, just echo $content
If I understand your question, you're trying to achieve a template situation. For this, the best way is to actually call your templates view files within a primary page view. What I mean is your controller function (not the constructor, an actual class function representing a page) should call a primary view such as
$this->load->view('page1', $this->data);
and within that file, you call
$this->load->view('nav', $this->data);
then your content and then
$this->load->view('footer', $this->data);
You would then repeat the process for page 2 where in your controller's page2 function, you would call
$this->load->view('page2', $this->data);
and your page2 view file is almost identical to page1 except you use your page 2 content in that area.
You could even use a single template view file and pass it a $content variable (which obviously changes per page) and call
$this->load->view('template', $this->data);

CodeIngiter: Load a view inside of another view

I'm using CodeIgniter. I want to load views inside of other views. How can I do this?
Example:
Let's say I have a "view" called "CommentWall". In CommentWall, I want a bunch of "Comment" views. I use the view for "comment" all over my site!
How can I do this? It seems CodeIgniter only allows me to load views sequentially, which is sort of strange considering I use reusable views INSIDE of other views!
Can I do a $this->load->view('comment'); inside of my view for CommentWall? Or is there some other way to have reusuable views contained inside a view?
You can do it easily, just load the main view, for example CommentWall from the controller
$this->load->view('CommentWall');
To add child views in CommentWall view you can add following line inside your CommentWall view
$this->view('Comment');
For example, if you load CommentWall view from your controller like this
$data['comments'][] = 'Comment one';
$data['comments'][] = 'Comment two';
// load the parrent view
$this->load->view('CommentWall', $data);
Now in the CommentWall (parent view), if you put this
foreach ($comments as $comment) {
$this->view('Comment', array('comment' => $comment));
}
And in your Comment (child view) if you have this
echo $comment . '<br />';
Then you should get output something like this
Comment one
Comment two
Update : Alos, check this answer.
Try
class Main extends CI_Controller {
function __construct()
{
parent::__construct();
$data->comments =$this->load->view('comment');
$this->load->vars($data);
}
And in every view try
echo $comments;
Just load the "Comment" vies as string in controller and pass it to "CommentWall" view.
You can do it like this:
//Controller:
public function load_comment_wall($param) {
$comments_view = ""; //String that holds comment views
//here load the comments for this wall as follows:
//assuming $comment_ids is array of id's of comment to be put in this wall...
foreach($comment_ids as $comment_id) {
$temp = $this->load->view("comment",array('comment_id'=>$comment_id),TRUE); //Setting last parameter to TRUE will returns the view as String
$comments_view = $comment_views.$temp;
}
$data['comments'] = $comments_view;
//load comment wall
$this->load->view('comment_wall',$data);
}
//In Comment wall View, add the following line
echo $comments;

call cakePHP element via Ajax

just a quick question, Is it possible to call a cakePHP element via jQuery Ajax? I know the standard way to call an element in cakePHP is:
<?php echo $this->element('path_to_element', 'data_to_send_to_element'); ?>
But what if I wanna call my element inside the $.ajax or .load() function? How do I achieve this?
Thank you
To call anything in Cake, by Ajax or otherwise, you need to define an action in a controller. You could create a view too, but you can also have the action render an element directly by setting the viewPath. Example:
class MyController extends AppController {
// Apply Ajax layout automatically
var $components = array('RequestHandler');
function doSomething() {
$this->autoRender = false;
... // set parameters needed by the element...
// render an element
$this->viewPath = 'elements';
$this->render('path_to_element');
}
}

add iframe while calling controller to view in codeigniter

call controller to view i use this code..
i want to add "template/home/home" page in to iframe tag while calling controller to view.
how can i do?
class main_con extends Controller
{
function main_con()
{
parent::Controller();
// $this->freakauth_light->check('user');
$this->_container = $this->config->item('FAL_template_dir') . 'template/container';
$this->load->library('FAL_front', 'fal_front');
}
function index()
{
$data['redirect_page'] = 'template/home/home';
$this->load->vars($data);
$this->load->view($this->_container);
}
}
thank you in advance...
I'm not quite sure if I understand your problem but:
The iframe is in your view template right? So what you need to do is just print the content of your variable in the src attrib of the iframe.

Resources