hospect/php-vars-to-js-bundle

将 PHP 变量转换为 JS,用于 Symfony2 和 Twig

1.0.2 2017-08-08 14:05 UTC

This package is not auto-updated.

Last update: 2024-09-28 17:43:39 UTC


README

Build Status

描述

该Bundle允许您在设置从PHP变量到JS变量时避免在Twig模板中使用可怕的语句,例如

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

安装

只需用composer要求它

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

并在您的 AppKernel.php 中注册

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

使用

在您的控制器调用中

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

然后在Twig模板中(可能将其移动到布局将很有用)

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