简体   繁体   中英

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:

<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)
  • you could use a Laravel View Composer to add the JSON data to the required views. Those views would so include the JSONed data, that you'd echo in the right place to be handled by your 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 ). And then use it from a View Composer or the specific controller's methods concerned by your views.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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