diff --git a/readme.md b/readme.md index 50a98af..9ae55c4 100644 --- a/readme.md +++ b/readme.md @@ -36,6 +36,14 @@ For Laravel users, there is a service provider you can make use of to automatica When this provider is booted, you'll gain access to a helpful `JavaScript` facade, which you may use in your controllers. +## Usage + +#### JavaScript::put(...) + +The `JavaScript` facade has two public functions. The first/main one is `JavaScript::put(...)`, which transforms the given PHP data and passes it to the front-end view file. + +For example: + ```php public function index() { @@ -61,17 +69,53 @@ console.log(age); // 29 This package, by default, binds your JavaScript variables to a "footer" view, which you will include. For example: -``` +```html

My Page

- @include ('footer') // <-- Variables prepended to this view + + @include ('footer') + +``` + +Naturally, you can change this default to a different view. See ["Defaults"](#defaults) below. + +#### JavaScript::setHtmlAttributes($htmlAttributes) + +The second public function provided by this facade is `JavaScript::setHtmlAttributes($htmlAttributes)`, which allows you to set the element attributes on the HTML ` ``` -Naturally, you can change this default to a different view. See below. +> **NOTE**: This package does not provide any sort of Content Security Policy logic; the above is just an example of an additional HTML attribute you may need to add to a script tag. +> +> For more information on Content Security Policy, see [the Mozilla Developer docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP). +> +> If you do not already have CSP and are using Laravel, I would recommend package [spatie/laravel-csp](https://github.com/spatie/laravel-csp). -### Defaults +## Defaults If using Laravel, there are only two configuration options that you'll need to worry about. First, publish the default configuration. diff --git a/src/LaravelViewBinder.php b/src/LaravelViewBinder.php index 6a2b5ec..5457a8a 100644 --- a/src/LaravelViewBinder.php +++ b/src/LaravelViewBinder.php @@ -20,16 +20,54 @@ class LaravelViewBinder implements ViewBinder */ protected $views; + /** + * An array of HTML attributes to add to the "; + $this->event->listen("composing: {$view}", function () use ($htmlAttributes, $js) { + echo "{$js}"; }); } } diff --git a/src/Transformers/Transformer.php b/src/Transformers/Transformer.php index ca7a550..1eb7a06 100644 --- a/src/Transformers/Transformer.php +++ b/src/Transformers/Transformer.php @@ -45,6 +45,16 @@ public function put() return $js; } + /** + * Add HTML attributes to the