irishdan / js-settings-bundle
A bundle for injecting data as javascript into a page
Installs: 30
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=7.1
- symfony/framework-bundle: ~3.0|~4.0
- symfony/property-access: ~3.4|~4.0
Requires (Dev)
- phpunit/phpunit: 4.8.*
README
This bundle provides a service and a twig extension for easily injecting data into a webpage as a javascript object.
1: Installation
Install with composer.
composer require irishdan/js-settings-bundle
Step 2: Enable the Bundle
Enable the bundle by adding it to the list of registered bundles
in the app/AppKernel.php
file of your project:
<?php // app/AppKernel.php // ... class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new IrishDan\JsSettingsBundle\JsSettingsBundle(), ); // ... } // ... }
Import the service definition in your config.yml file imports:
- { resource: "@JsSettingsBundle/Resources/config/services.yml" }
3: Configuration
No configuration is needed. Some is available.
In config.yml you can add:
js_settings: object_name: 'JsData' defaults: local: "%locale%" page_data: site: 'www.example.com'
object_name: By default the Javascript object it Called 'Symfony'. You can change the use 'JsData', by using the configuration above.
defaults: Default values are added to the javascript object by default and are always available.
4: Usage
The bundle provides a single service 'js_settings.settings' to add data and remove data from the javascript object.
To add variables simple pass in a key value pair like so:
// The key can be a string... $this->get('js_settings.settings')->addData('key', $values); // The key can be an array... $this->get('js_settings.settings')->addData(['first_key', 'second_key'], $values); // The key can be index notation $this->get('js_settings.settings')->addData('[first_key][second_key]', $values);
Or, use pushData to push the data into an array.
$this->get('js_settings.settings')->pushData('[first_key][second_key]', $values);
To inject the javascript object into the page, simply add the following twig function to your twig template
{{ get_js_settings() }}