2017 © Pedro Peláez
 

library put-vars

Transform your PHP to JavaScript

image

javascript/put-vars

Transform your PHP to JavaScript

  • Thursday, June 30, 2016
  • by duyluonglc
  • Repository
  • 1 Watchers
  • 1 Stars
  • 25 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 187 Forks
  • 0 Open issues
  • 9 Versions
  • 0 % Grown

The README.md

Transform PHP Vars to JavaScript

Build Status, (*1)

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., (*2)

This package simplifies the process drastically., (*3)

This source fo, (*4)

Installation

Begin by installing this package through Composer., (*5)

{
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/duyluonglc/PHP-Vars-To-Js-Transformer"
        }
    ],
    "require": {
        ...............
        "javascript/utilities": "dev-master"
    }
}

Laravel 5.1 Users


// config/app.php 'providers' => [ '...', 'JavaScript\Utilities\JavaScript\JavaScriptServiceProvider' ];

When this provider is booted, you'll gain access to a helpful JavaScript facade, which you may use in your controllers., (*6)

public function index()
{
    JavaScript::put([
        'foo' => 'bar',
        'user' => User::first(),
        'age' => 29
    ]);

    JavaScript::put('messages', ['hello', 'hi']);

    return View::make('hello');
}

In Laravel 5, of course add use JavaScript; to the top of your controller., (*7)

Add render() to export variables to views. For example:, (*8)

<body>
    <h1>My Page</h1>

    {!! JavaScript::render() !!} // <-- Variables prepended to this view
</body>

Using the code above, you'll now be able to access foo, user, and age from your JavaScript., (*9)

console.log(foo); // bar
console.log(user); // User Obj
console.log(age); // 29
console.log(messages); //array of messages

You can use jquery to extend objects and keep existing attributes by call Javascript::extend($var) instead use put method, (*10)

You can change default namespace = window of variables by change config. See below., (*11)

Defaults

php artisan vendor:publish

This will add a new configuration file to: config/javascript.php., (*12)

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | JavaScript Namespace
    |--------------------------------------------------------------------------
    |
    | By default, we'll add variables to the global window object. However,
    | it's recommended that you change this to some namespace - anything.
    | That way, you can access vars, like "SomeNamespace.someVariable."
    |
    */
    'js_namespace' => 'window',

];

The Versions

30/06 2016

dev-master

9999999-dev

Transform your PHP to JavaScript

  Sources   Download

MIT

The Requires

 

The Development Requires

by Duy Luong

laravel javascript

30/06 2016

0.1

0.1.0.0

Transform your PHP to JavaScript

  Sources   Download

MIT

The Requires

 

The Development Requires

by Duy Luong

laravel javascript

30/06 2016

2.1.1

2.1.1.0

Transform your PHP to JavaScript

  Sources   Download

MIT

The Requires

 

The Development Requires

by Duy Luong

laravel javascript

01/10 2015

2.1

2.1.0.0

Transform your PHP to JavaScript

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel javascript

30/09 2015

2.0.1

2.0.1.0

Transform your PHP to JavaScript

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel javascript

06/02 2015

2.0

2.0.0.0

Transform your PHP to JavaScript

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel javascript

24/04 2014

1.0.1

1.0.1.0

Transform your PHP to JavaScript

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel javascript

13/03 2014

1.0

1.0.0.0

Transform your PHP to JavaScript

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel javascript

25/02 2014

0.5

0.5.0.0

Transform your PHP to JavaScript

  Sources   Download

The Requires

 

The Development Requires

laravel javascript