简体   繁体   English

将部分刀片视图模板添加到所有视图?

[英]Addd partial blade view template to all views?

I have a partial view with a script I want to include in all my views, I dont't wanna @include this partial in all my layouts and blade views but instead add it to all rendered views, my partial looks like this:我有一个带有脚本的部分视图,我想在我的所有视图中包含一个脚本,我不想在我的所有布局和刀片视图中@include 这个部分,而是将它添加到所有渲染视图中,我的部分看起来像这样:

<script type="text/javascript">window.$app = {!! json_encode(app(App\Helpers\Javascript::class)->app) !!};</script>

How can I do this?我怎样才能做到这一点?

You could:你可以:

  • include it in your main(s) layout template (the one(s) handling <html> and <body> tags)将其包含在您的主布局模板中(处理<html><body>标签的模板)
  • you could use a Laravel View Composer to add the JSON data to the required views.您可以使用 Laravel View Composer 将 JSON 数据添加到所需的视图中。 Those views would so include the JSONed data, that you'd echo in the right place to be handled by your JS.这些视图将包含 JSON 化的数据,您将在正确的位置回显以由您的 JS 处理。
  • you could check the Php-Vars-To-Js package that allows you to put some data from PHP in a custom JS namespace (eg window.yourNameSpace ).您可以检查Php-Vars-To-Js package,它允许您put来自 PHP 的一些数据放入自定义 JS 命名空间(例如window.yourNameSpace )。 And then use it from a View Composer or the specific controller's methods concerned by your views.然后从 View Composer 或与您的视图相关的特定控制器的方法中使用它。

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

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