php Laravel - 在树枝视图中打印_r
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18024216/
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
Laravel - print_r in twig view
提问by Tobias Hagenbeek
I have a "simple" question I would hope, and that is how can I print_r or at least see the contents of all defined variables in a twig file.
我希望有一个“简单”的问题,那就是我如何才能 print_r 或至少看到树枝文件中所有已定义变量的内容。
I have tried: {{ variable }}
(where variable is an array set for the view
我试过:({{ variable }}
其中变量是为视图设置的数组
$viewData['variable'] = array('1','2','3');
in the controller.
在控制器中。
I have also tried: {{ $variable }}
That gives an error.
我也试过:{{ $variable }}
这会出错。
I would just want to know what is available from my array in the twig file.
我只想知道树枝文件中我的数组中有什么可用。
回答by SirDerpington
You could use the built in {{ dump() }}
function. See the documentation.
您可以使用内置{{ dump() }}
函数。请参阅文档。
If you use it without any value in the brackets it will dump all variables available. For dumping only your array you would do it like this:
如果您在括号中没有任何值的情况下使用它,它将转储所有可用变量。对于仅转储您的数组,您可以这样做:
{{ dump(viewData) }}
{{ dump(viewData) }}
With something like xdebug the output looks quite nice and is readable.
使用 xdebug 之类的东西,输出看起来很不错并且可读。
array (size=3)
0 => string '1' (length=1)
1 => string '2' (length=1)
2 => string '3' (length=1)
Although the documentation says it's not available by default it was added in twig 1.5 and should be ready to use by default.
虽然文档说它默认不可用,但它是在 twig 1.5 中添加的,默认情况下应该可以使用。
Of course not the same as print_r
but with xdebug enabled it outputs nice and readable var_dump
information.
当然print_r
与启用 xdebug 不同,它会输出漂亮且可读的var_dump
信息。
回答by Jason Lewis
I'm not sure about doing it in Twig but you can use Laravel's dd
helper to dump and die from within the controller.
我不确定是否在 Twig 中执行此操作,但您可以使用 Laravel 的dd
助手从控制器内转储和死亡。
dd($viewData['variable']);
回答by jjz
The dump function works but requires the debug option enabledin the environment. If you don't have the ability to set this you can also just loop through the array:
转储功能有效,但需要在环境中启用调试选项。如果您没有能力设置它,您也可以循环遍历数组:
<pre>
{% for key, item in variable %}
* {{ key }} - {{ item }}
{% endfor %}
</pre>
回答by Kulai
In Laravel 5.4 in config
-> twigbridge.php
set environment
to 'debug' => config('app.debug', true)
Now twigbridge debug is enabled on Laravel env
在 Laravel 5.4 中config
->twigbridge.php
设置environment
为'debug' => config('app.debug', true)
Now twigbridge debug is enabled on Laravel env