rosio/php-to-javascript-variables

Installs: 84

Dependents: 0

Stars: 1

Watchers: 1

Forks: 1

Open Issues: 1

Language: PHP

v1.0.1 2014-06-18 05:00 UTC

README

Build Status

Simple package which allows easy sharing of variables from PHP to JavaScript. This is a rewrite of the laracasts/utilities package. I was prompted to do this due to the aformentioned package requiring PHP 5.4+, and I disliked their design.

Installation

composer.json

"require": {
    ...
    "rosio/php-to-javascript-variables": "~1.0"
}

config/app.php

'providers' => array(

    ...
    'Rosio\PhpToJavaScriptVariables\PhpToJavaScriptVariablesServiceProvider',

),

Usage

controllers\HomeController.php

    public function showWelcome()
    {
        JSLocalize::put(array(
            'variableName' => 'variableValue',
            'anotherVariable' => array(1, 2, 3)
        ));

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

views\hello.php

<!doctype html>
<html lang="en">
<head>
    ...
    {{ App::make('JSLocalizeDumper')->dump() }}
    ...
</head>
<body>

    <script type="text/javascript">
        alert(app.variableName);
    </script>
</body>
</html>