Skip to content

holyspecter/HospectPhpVarsToJsBundle

Repository files navigation

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>