Transform PHP Vars to JavaScript For Drupal 8
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
composer.json
readme.md
vars_to_javascript.info.yml
vars_to_javascript.module
vars_to_javascript.services.yml

readme.md

Transform PHP Vars to JavaScript For Drupal 8

Often, you'll find yourself in situations, where you want to pass some server-side string/array/collection/whatever to your JavaScript. Traditionally, this can be a bit of a pain - especially as your app grows.

This package simplifies the process drastically.

Installation

Download vars_to_javascript to module direcotry, then run composer install

Drupal Users

  1. enable the moudle

  2. pass php vars

     $javascript= \Drupal::service('javascript');
     $variabes = ['aa' => 32, 'bb' => 'cc', 'dd' => [23421,3423]];
     $javascript->put($variabes);
  1. use it in frontend Using the code above, you'll now be able to access aa, bb, and dd from your JavaScript.
console.log(php.aa); // 32
console.log(php.bb); // cc

js_namespace

You can your own namespace in settings.php like

$settings['js_namespace'] = 'phpjs';

License

GPL