Laravel - 在刀片中使用功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44033222/
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 - Use function in blade
提问by Daniel J?rgensen
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. 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?
我开始使用 Laravel 构建应用程序,我现在面临的挑战之一是在我的刀片视图中,我需要显示一堆不同的引导模式。因此,我相信创建某种辅助方法会是一种更好的方法,该方法需要一些参数来定义模态的外观。但我目前不确定这样做的最佳实践是什么。我什至可以在视图中使用这样的辅助方法吗?
How would you do this?
你会怎么做?
回答by Thiago Cardoso
You can still process any data using the PHP tags (<?php echo 'hi'; ?>
), or even using the complete namespace of the file and then call the desired function, like {{ \Carbon\Carbon::today() }}
您仍然可以使用 PHP 标记 ( <?php echo 'hi'; ?>
)处理任何数据,甚至可以使用文件的完整命名空间,然后调用所需的函数,例如{{ \Carbon\Carbon::today() }}
回答by Sandeesh
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.
您可以创建一个帮助文件,然后定义可以全局使用的方法,包括视图。但是在您的情况下,理想的情况是使用部分,然后包含具有不同参数的部分以更改属性。这与方法完全一样,但更清晰,更符合视图。这应该对您有所帮助,子视图称为局部视图。