From 146c277bb59fd48be450dad666afe0f501749f42 Mon Sep 17 00:00:00 2001 From: Tariq Date: Thu, 29 Nov 2018 22:19:38 -0600 Subject: [PATCH 1/3] Add 'JavaScript::setHtmlAttributes' --- readme.md | 47 ++++++++++++++++++++++++++++-- src/LaravelViewBinder.php | 49 ++++++++++++++++++++++++++++---- src/Transformers/Transformer.php | 10 +++++++ src/ViewBinder.php | 8 ++++++ 4 files changed, 106 insertions(+), 8 deletions(-) diff --git a/readme.md b/readme.md index 50a98af..cd31cdb 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,7 +69,7 @@ console.log(age); // 29 This package, by default, binds your JavaScript variables to a "footer" view, which you will include. For example: -``` +```blade

My Page

@@ -69,9 +77,42 @@ This package, by default, binds your JavaScript variables to a "footer" view, wh ``` -Naturally, you can change this default to a different view. See below. +Naturally, you can change this default to a different view. See ["Defaults"](#defaults) below. + +#### JavaScript::setHtmlAttributes(...) + +The second public function provided by this facade is `JavaScript::setHtmlAttributes(...)`, which allows you to set the element attributes on the HTML ` + +``` + +> **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. +> For more information on Content Security Policy, see [the MDN 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