Loading a part of view using a PHP variable - php

I am loading a part of view in by main view using a PHP variable
here is the code of the view
<body>
<div id="wrapper">
<div id="gallery">
<?= $view_thing ?>
</div>
</div>
</body>
and my controller is
$data['view_thing']=$this->load->view('model/portion',$data); //$data gets data from model
$this->layout->view('model/model',$data);
but the problem is "$view_thing" do not show things in the div in which I wrote it, this page is shown below body tag
note:I have wrote only a part of code from my view

The view loader function needs a third argument to return it as a string, as said in the documentation, for example:
$data['view_thing']=$this->load->view('model/portion', $data, true);

Related

How to insert a code in a blade view dynamically

Using laravel 5.1, could I insert a custom code into a blade view dynamically?
For example, I have this view.blade.php :
<html>
<head>
<title>App Name - #yield('title')</title>
</head>
<body>
#section('sidebar')
This is the master sidebar.
#show
<div class="container">
#yield('content')
// Here, I want to insert a code dynamically, for example :
<div> <p> something </p> </div>
</div>
</body>
So, Is there a good way to do something like this in controller :
$customCode = "<div> <p> something </p> </div>";
$content = (string) $view;
//For example I want to insert this code into view file on line 11
updateContent($content, $customCode, $line_position_in_view);
Here is your code with jQuery :
Your HTML FILE OR VIEW FILE
<div class="container">
#yield('content')
// Here, I want insert a code dynamically, for example :
<div> <p> something </p> </div>
</div>
$.post("test.php",{},function (data){
$(".container").append(data);
});
Note : if you want to use controller function then change test.php with function name with proper path.
test.php file or controller function
<?php
echo "<div> <p> something </p> </div>";
exit;
?>
For instance let's say you don't want to insert something like
{{$user->name}}
but more styled html chunk of code instead with the same string showed,
say by bootstrap 4. then you have to assign to the name a string like
$user->update(['name' => '<span class="badge badge-success">SHILPILDOQ</span>']);
AND FINALLY for the blade file you have a little change now :
{!! $user->name !!}
You are not suppose to just put html and javascript in your controller so you can dump it out into your blade view. This goes against the seperation of concerns.
You should just create a master layout and extend this master layout on other pages you create. This way you can dynamically adjust the content of the master layout page.
Naturally, you should never ever mix php functions into your blade view. Run the php functionality in your controller, put it into a variable and pass it along to your view to use.

Fetch data from inside Blade view

I'm learning about Laravel. I know how to create a controller that builds an array of data from an Eloquent model, pass that array to a view and display it.
What I am trying to do is create a main view that has the header and footer information all pages use. Then I try to use a child view to display the body.
The main blade file has an include for a file that builds a dynamic navigation bar.
How can I go about retrieving data from within the include file the main blade file that's called by a child?
What I have (very abbreviated) is:
main.blade.php
<html>
#include('header')
<body>
#section('content')
#stop
</body>
</html>
home.blade.php
#extends('main')
#section('content')
<h1>Home</h1>
#endsection
Now from the controller, I call the home.blade.php view
Can I fetch the data thats needed in the header view from INSIDE the header file rather than having to pass the data from every view I call? Or do I need to simply create a global function that I call in every controller before I call a view?
In your main page:
<html>
#include('header')
<body>
#yield('content')
</body>
</html>
In your view:
#extends('main')
#section('content')
<h1>Home</h1>
#endsection
The dynamic navigation bar you mention should come from a view composer. In this way it will be available all the time to all views.
Do a test with the following in your routes.php file:
View::composer('*', function($view)
{
// $navBar stuff
$view->with('navBar', $navBar);
});
Now $navBar will be available in your views.
http://laravel.com/docs/5.1/views#view-composers

Method Illuminate\View\View::__toString() must not throw an exception when loading views inside view in Laravel

I am loading a header and footer in each page without using blade and just using echo View:make('templates/header'); and at the end of the page likewise with the footer.
I am loading these same header and footer views in exactly the same manner in other views without problems, but when I call this particular view from my controller, I get this very non-informational Laravel error message. It's very basic HTML right now as I'm still in the building/testing phase. I don't know why loading the header and footer views in other views I'm using works, but here they're crashing.
Here's the code:
<?php
echo View::make('templates/header');
?>
<div class="row">
<div class="col-md-5 col-md-offset-3">
<h1>Record Saved!</h1>
</div>
</div>
<?
echo View::make('templates/footer');
Is there any way to get more information out of Laravel to find out what it's not liking?
__toString() calls the render() method on View (source). the exception is therefore thrown within templates.footer or templates.header.
In blade template file, You do not need to write <?php ?> Tag.
also use include instead of View::make in blade file.
#include('templates.header');
<div class="row">
<div class="col-md-5 col-md-offset-3">
<h1>Record Saved!</h1>
</div>
</div>
#include('templates.footer');
Note: Blade file must has extension .blade.php
Hope it will help you :-)

adding header and footer codeigniter to controller

I am trying to find out away to get.
Just trying to find the right way of getting it done correctly I am looking through there help index but bit confused.
I have three controllers in my controllers/common file called home.php, header.php, footer.php
I have set home.php controller to be my default one.
I tried adding the header and footer controller link to home controller like this
this->load->view('common/home');
$this->children = array(
'common/footer',
'common/header'
);
And on the views part
<?php echo $header; ?>
<div id="body">
<p>The page you are looking at is being generated dynamically by CodeIgniter.</p>
<p>If you would like to edit this page you'll find it located at:</p>
<code>application/views/common/home.php</code>
<p>The corresponding controller for this page is found at:</p>
<code>application/controllers/common/home.php</code>
<p>If you are exploring CodeIgniter for the very first time, you should start by reading the User Guide.</p>
</div>
<?php echo $footer; ?>
But the links in the controller file don't work for header and footer. Where abouts would I find the correct way so I can echo the header and footer to the views pages.
You need save header to variable, with third parameter set to TRUE.
Then send data to common/home
$data['header'] = $this->load->view('common/header', $variables, TRUE);
$data['footer'] = $this->load->view('common/footer', $variables, TRUE);
$this->load->view('common/home', $data);
And view common/home
<?php echo $header; ?>
<div id="body">
<p>The page you are looking at is being generated dynamically by CodeIgniter.</p>
<p>If you would like to edit this page you'll find it located at:</p>
<code>application/views/common/home.php</code>
<p>The corresponding controller for this page is found at:</p>
<code>application/controllers/common/home.php</code>
<p>If you are exploring CodeIgniter for the very first time, you should start by reading the User Guide.</p>
</div>
<?php echo $footer; ?>

Laravel 4 - including a "partial" view within a view (without using Blade template)

In Laravel 3, I used to do this.
<?php render('partials.header'); ?>
This was done in "PHP" views, without using Laravel's Blade templates.
What's the equivalent of this in version 4?
I tried
<?php #include('partials.header'); ?>
This doesn't work.
If I do
#include('partials.header')
I have to save my file as ".blade.php"
How do I include a "subview" without using the blade template?
There are different ways to include a view within a view in Laravel 4. Your choice will depend on any one of the outcomes outlined below...
For Flexibility
You can compile (render) the partial views in the appropriate Controller, and pass these views to the Main View using the $data[''] array.
This may become tedious as the number of views increase, but hey, at least there's a lot of flexibility :)
See the code below for an example:
Controller
...
public function showMyView()
{
/* Header partial view */
$data['header'] = View::make('partials.header');
/* Flexible enough for any kind of partial views you require (e.g. a Header Menu partial view) */
$data['header_menu'] = View::make('partials.header_menu');
/* Footer partial view */
$data['footer'] = View::make('partials.footer');
return View::make('myView', $data);
}
...
View
You can include the partials above as follows (at any position in your View code):
<html>
<head></head>
<body>
<!-- include partial views -->
<?php echo ($header) ?>
<?php echo ($header_menu) ?>
<div id="main-content-area"></div>
<?php echo ($footer) ?>
</body>
</html>
Your partial views will now be added to your main View.
For Simplicity
There's actually a much easier way than using the method above: Simply include this in the html of the view...
View
<html>
<head></head>
<body>
<!-- include partial view: header -->
<?php echo View::make('partials.header') ?>
<div id="main-content-area">
</div>
<!-- include partial view: footer -->
<?php echo View::make('partials.footer') ?>
</body>
</html>
Make sure that the folder structure for the partials is [views/partials/header.php] in order to provide the correct file-path to the View::make() function of Laravel.
WARNING
If you try to pass the $data['page_title'] in a controller, the nested views wont receive the data.
To pass data to these nested views you need to do it like this:
<html>
<head></head>
<body>
<?php
/* Pass page title to header partial view */
$data ['page_title'] = "My awesome website";
echo View::make('partials.header', $data);
?>
<div id="main-content-area"></div>
<?php echo View::make('partials.footer') ?>
</body>
</html>
NOTE
The question clearly stated: "Without using Blade template", so I have made sure to give a solution that does not include any Blade templating code.
Good luck :)
You can nest your partials in views try this
View::make('folder.viewFile')->nest('anyname', 'folder.FileName');
Then access the nested view file from your template {{ $anyname }} this way you don't have to include files in your view and this should work for .php file also.
I am not sure how many people have been using Laravel 4 in this post, since this post, but if you are looking to include partials or separate your view types you can do it with #includes
for example, if you want a partials folder for your header, footer, sidebar etc
create a directory for the partials under
app/views/partials
Then create a partial
app/views/partials/navigation.blade.php
Then in your master template file add the line
#include('partials.navigation')
That is all it takes.
** Bonus you can also pass data to a partial or include nested partials within a partial
I know this is a bit of a late answer, but I figured since I didn't see this solution amongst the other answers it was ok.
If you want to include your header and footer on every page I would add them into the before and after filters. Just go to filters.php in your app folder
App::before(function($request)
{
echo View::make('partials.header');
});
App::after(function($request, $response)
{
echo View::make('partials.footer');
});
When doing it this way you don't need to add anything in the view files to include them.
You can use View's nest function
View::make('default.layout')->nest('header', 'default.header');
Use the third parameter to pass data to the template
View::make('default.layout')->nest('header', 'default.header', ['name' => 'John Doe', 'test' => 'It works!']);
on your views/default/header.blade.php
<div>hey {{ $name }}! {{ $test }}</div>
I am still pretty new to Laravel, but I think the below is pretty ideal ...
Route::get('/', function()
{
$data['title'] = 'sample';
return View::make('page', $data);
});
# /views/partials/header.php
# /views/partials/footer.php
View::composer('page', function($view)
{
$view->with('header', View::make('partials.header', $view->getData()));
$view->with('footer', View::make('partials.footer', $view->getData()));
});
See Laravel View Composers .. http://laravel.com/docs/responses#view-composers
/views/page.php
<?php echo $header; ?>
<div>CONTENT</div>
<?php echo $footer; ?>
From within a view, just echo the other view:
echo View::make('header'); //This will look for a view called header.php

Categories