简体   繁体   English

Laravel-在刀片中使用功能

[英]Laravel - Use function in blade

I'm beginning to build an app using Laravel, and one of the challenges I'm now facing is that throughout my blade views, i need to show a bunch of different bootstrap modals. 我开始使用Laravel构建应用程序,现在面临的挑战之一是在整个刀片视图中,我需要展示许多不同的引导程序模式。 Because of this, i believe it would be a better approach to create some sort of helper method that takes a few parameters to define how the modal should look. 因此,我相信创建一些需要一些参数来定义模态外观的辅助方法将是更好的方法。 But im currently not sure what is best practice of doing this. 但是我目前不确定这样做的最佳实践是什么。 Am i even allowed to use such a helper method in a view? 我是否甚至可以在视图中使用这种帮助方法?

How would you do this? 你会怎么做?

您仍然可以使用PHP标记( <?php echo 'hi'; ?> ),甚至使用文件的完整名称空间来处理任何数据,然后调用所需的函数,例如{{ \\Carbon\\Carbon::today() }}

You can create a helper file and then define methods which can be used globally, including the views. 您可以创建一个帮助程序文件,然后定义可以在全局范围内使用的方法,包括视图。 But in your case the ideal scenario would be to use a partial and then to include the partial with different parameters to change the properties. 但是,在您的情况下,理想的情况是先使用部分,然后将包含不同参数的部分包括在内以更改属性。 This would work exactly like a method, but cleaner and more aligned with views. 这将完全像一种方法一样工作,但是更加清晰,并且与视图更加一致。 This should help you, the sub-views are called partials. 这应该对您有帮助,子视图称为局部视图。

https://laravel.com/docs/5.4/blade#including-sub-views https://laravel.com/docs/5.4/blade#include-sub-views

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM