I didn't deal with render method yet !!
is it for blade template ?
I have to pass dynamic data in blade.php file dynamically.
Given that you've tagged the question with Blade, I'll assume you mean render inside Laravel's View class.
Illuminate\View\View::render() returns the string contents of the view. It is also used inside the class' __toString() method which allows you to echo a View object.
// example.blade.php
Hello, World!
// SomeController.php
$view = view('example');
echo $view->render(); // Hello, World!
echo $view; // Hello, World!
Laravel typically handles this for you, I.e. calls render or uses the object as a string when necessary.
Blade's #include('viewname') directive will load the view file and call the render method behind the scenes for example.
You may use it yourself when you want to get the compiled view to perform some subsequent action. Occasionally I have called render explicitly rather than to string if the view itself is causing an exception and in PHP explains
Fatal error: Method a::__toString() must not throw an exception in /index.php on line 12
Calling render() in the above case gives a more useful error message.
Render(), when applied to a view, will generate the corresponding raw html and store the result in a variable.
Typical reasons for which I use render are:
When converting pages to pdf (ex. using dompdf, pass this into loadhtml()), returning HTML content to ajax calls
You can get php blade file with passing dynamic value in a string form
Like this
Blade
<link rel="apple-touch-icon" sizes="60x60" href="{{$url}}/assets/images/favicon/apple-icon-60x60.png">
Controller
$html = view('User::html-file',['url'=>'https://stackoverflow.com'])->render();
O/P
<link rel="apple-touch-icon" sizes="60x60" href="https://stackoverflow.com/assets/images/favicon/apple-icon-60x60.png">\r\n
0pposed to ->render() when using DomPDF, you can also use ->toHtml():
$pdf->loadHtml(\view('folderX.bladeY', $data)->toHtml());
Related
I have the below content in my DB-
<p>This is dummy content for testing</p>
{{LandingPageController::getTest()}}
I want to render that into my view. But when I'm rendering this in Laravel view, this {{LandingPageController::getTest()}} is getting displayed as it is stored in DB.
I want to call the LandingPageController getTest method in my view.
Please suggest me a quick fix for this.
Landing Page Controller
public function getTest(){
return "Hello World!!!";
}
just make the function static
public static function getTest(){
return "Hello World!!!";
}
that's the only way you can call it like this {{LandingPageController::getTest()}} but I do advice not to do that in your blade file this not a good code design. you should do $test = LandingPageController::getTest() in the controller that you return the blade view and pass it like this return view('blade_file_name',compact('test')) and in your blade file just do {{$test}}
PS - if you doing it your controller use the class like this use Path\To\Controller\LandingPageController
Use namespace for that controller in your blade file. example
namespace App\Http\Controllers\LandingPageController;
You can evaluate a string as a php code using the eval() function
eval — Evaluate a string as PHP code
But it is highly discouraged.
The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.
You can use a generic string, {test} for example, when saving the content in the storage.
<p>This is dummy content for testing</p>
{test}
Then whenever you need to display the actual content, you can simply replace the generic string with the real value. You'll have this line in your blade file:
{{ str_replace('{str}', "Hello World", $content) }}
Take a look at Helper. You can call helper function in view to render your text or html
Got the solution, achieve the functionality with "laravel-shortcodes".
Found a very good tutorial on laravel-shortcodes like wordpress
Basically what I'm trying to do is to implement a little chunk of html generated by a controller in a separate view into one main view. The problem is that I need custom styles for that little chunk of html and I can't know where I'll have to include it (manually), so I'd like the css to get appended to the file calling the function somehow from the controller when the method is being called.
More detailed explanation:
I'm programatically listing small custom panels to display some properties of each instance of my model (in this case, a window). In the main view, where I'm listing, there are a lot of them, so I decided to make a separate view file to create the panel and then simply return it via a function in the controller.
So in the home.blade.php I do as follows:
#foreach($order -> windows as $window)
{!!$window->drawPanel()!!}
#endforeach
Then in my Window controller I've got a method to return the view where the window is being displayed (!differently depeding on it's properties!) like that:
public function drawPanel()
{
return view('dogrami.windowPanelThumbnail', ['window' => $this]);
}
And then in the windowPanelThumbnail file I'm displaying accordingly the html needed. The problem is: to build my panel, I use some custom css which I can't include in the builder view, because it's getting called like 100 times.
The question is - how to append the style to the file that called the method in the controller.
Basically I'd like to do as follows:
public function drawPanel()
{
//$cssFile = pathToMyCssFile;//that's the instance containing my custom css
//$callingFile = ...//somehow retrieve an instance to the file that called that method.. in this case - the path to 'home.blade.php'
//if($calling.File already has the $cssFile included in it's header)
//don't do anything
//else
//$callingFile -> somehow include the $cssFile instance in the header
return view('dogrami.windowPanelThumbnail', ['window' => $this]);
}
I have no idea if it's possible so that's what I'm asking. Or if you have better ideas of how to achieve that, I'd be really thankful!
If you want to include your css dynamically you can use stacks https://laravel.com/docs/5.4/blade#stacks this way :
$links = ["all", "the", "links", "to", "css", "files"];
return view('yourview', [/*allyourdata, */, 'stylesheets' => $links]);
And in your view you can do :
#push('stylesheets')
#foreach($stylesheets as $stylesheet)
<link rel="stylesheet" type="text/css" href="{{ $stylesheet }}">
#endforeach
#endpush
And add #stack('stylesheets') in the head of your html
PS : a stack is a lifo data structure (last in first out), meaning that if you do several #push, the last one you do will be the first one echo'ed
In the default Codeigniter installation there is the "welcome" controller which has a "index" action which loads the "welcome" view. This works as expected.
However, upon inspecting the "welcome" view I can see this variable in the footer.
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
From what I understand the variable {elapsed_time} is an example of using the built in template parser with text representations instead of using PHP short tags to echo variables.
But inside the "welcome" controller the only lines in the "index" action are these.
$this->load->view('welcome');
And it doesn't pass $data['elapsed_time']='xxx'; which means that I can't figure out where the variable elapsed_time is coming from!
My question is this.
Where does elapsed_time get defined? Is it built into the template parser class (and therefore available to be used without first defining it)? If so, where is a list of these other pre-defined varaibles? I would like to know what else I have access to as had I known that elapsed_time was available to me it would have been very useful. Does anyone have a list of the template parser pre-defined variables?
Thanks in advance.
elapsed_time defined output class. this class is initialized automatically by CodeIgniter.
more info here
CI will replace the string of "{elapsed_time}" in the final output string with actual "total_execution_time ". You can check it in system/core/Output.php Line 366 of v213
I am using Yii Framework. In the view, main.php, there is a reference to the $content block
<?php echo $content; ?>
I could not find it anywhere in the model or elsewhere in the demo project. Can someone shed light on this? Or may be this variable is never declared? I have not modified the demo project yet.
The $content value in layout files contains the rendered content of the template specified as the first attribute of the render command. (It's automatically created so I wouldn't use "content" as an additional variable name or it could cause confusion.) The variables that you pass as an additional array argument in the render statement are made available to the template you are calling, not to the layout.
If you have nested layouts, the value of $content cascades from parent to child.
All your controllers are derived from CController class. CController has a function named render which you call it for rendering your views. It works like this:
beforeRender is called.
renderPartial is called on your view file, and its output is stored in $output.
renderFile is called on the layout file, with a parameter named content like this:
$this->render(layoutFile, array('content'=>$output));
So the $content is coming from here. You can see the actual code here: Source code, and documentation here: Documentation
Found answer from Yii Documentation / Layouts,
For example, a layout may contain a header and a footer, and embed the
view in between, like this:
......header here......
<?php echo $content; ?>
......footer here......
where $content stores the rendering result of the view.
It is indeed all the text in one of the view (in my case index.php). $content basically takes the content of view. It is not declared anywhere and it is be default. As the answer said, you should not use declare/use $content in your code.
I think its being set from the controller which is calling this view.
In the controller look for something like the following
$this->render('main', array('content'=>"something here"));
i'm trying to assign $this->render() result to a method (this method renders google map's infoWindow/baloon).
I'm using method like this to create this infoWindow:
$infoWindow->setContent(<here goes the template>);
but passing it like this:
$infoWindow->setContent($this->render('WmapFrontBundle:Place:infoWindow.html.twig'));
don't work at all. What's the proper way to assign template to a variable or pass it's content to a method ?
Use renderView(), it returns the rendered template only.
render() returns a Response object (with the rendered template, headers, etc).