hospect/php-vars-to-js-bundle

Transformation of PHP vars to JS for Symfony2 with Twig

Installs: 30 328

Dependents: 0

Suggesters: 0

Security: 0

Stars: 9

Watchers: 3

Forks: 1

Open Issues: 0

Type:symfony-bundle

1.0.2 2017-08-08 14:05 UTC

This package is not auto-updated.

Last update: 2024-04-13 15:17:19 UTC


README

Build Status

Description

Bundle allows you to avoid horrible statements in Twig templates while setting JS variables from PHP ones, like:

var someJsVar = '{{ somePhpVar }}',
    anotherJsVar = '{{ anotherPhpVar }}';

Installation

Simply require it with composer:

composer require hospect/php-vars-to-js-bundle

And register in your AppKernel.php:

public function registerBundles()
{
    $bundles = array(
        // some other bundles...
        
        new \Hospect\PhpVarsToJsBundle\HospectPhpToJsBundle(),
    );
        
    return $bundles;
}

Usage

In your controller call:

$this->get('php_to_js')->put([
    'someJsVar' => $somePhpVar,
    // more vars here...
]);

Then in Twig template (probably that will be useful to move it to layout):

<script>
    {{ initPhpVars() }}
</script>