php laravel 的 render() 方法有什么用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37772786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:22:24  来源:igfitidea点击:

what is laravel render() method for?

phplaravelblade

提问by hikefd

I didn't deal with render method yet !!
is it for blade template ?
I have to pass dynamic data in blade.php file dynamically.

我还没有处理render方法!!
是刀片模板吗?
我必须在blade.php 文件中动态传递动态数据。

回答by Mizanur Rahman

Render(), when applied to a view, will generate the corresponding raw html and store the result in a variable.

Render(),当应用于视图时,将生成相应的原始 html 并将结果存储在变量中。

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

将页面转换为 pdf 时(例如使用 dompdf,将其传递给loadhtml()),将 HTML 内容返回给 ajax 调用

回答by Ben Swinburne

Given that you've tagged the question with Blade, I'll assume you mean render inside Laravel's View class.

鉴于您已经用 Blade 标记了问题,我假设您的意思是在 Laravel 的 View 类中渲染。

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.

Illuminate\View\View::render()返回视图的字符串内容。它也在类的__toString()方法中使用,该方法允许您回显 View 对象。

// 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.

Laravel 通常会为您处理这个问题,即在必要时调用 render 或将对象用作字符串。

Blade's @include('viewname') directive will load the view file and call the render method behind the scenes for example.

例如,Blade 的 @include('viewname') 指令将加载视图文件并调用幕后的渲染方法。

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

当您想要获得编译后的视图以执行一些后续操作时,您可以自己使用它。有时,如果视图本身导致异常并且在 PHP 中解释,我会显式调用 render 而不是字符串

Fatal error: Method a::__toString() must not throw an exception in /index.php on line 12

致命错误:方法 a::__toString() 不得在第 12 行的 /index.php 中抛出异常

Calling render() in the above case gives a more useful error message.

在上述情况下调用 render() 会给出更有用的错误消息。

回答by Bibhudatta Sahoo

You can get php blade file with passingdynamicvalue in a stringform

您可以通过字符串形式传递动态值来获取 php 刀片文件

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