php Symfony2、twig 和 JavaScript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12572506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:50:16  来源:igfitidea点击:

Symfony2, twig and JavaScript

phpsymfonytwig

提问by Doo Dah

What do I need to do to get twig to process a JavaScript file? I have an html.twig that uses a JavaScript twig. Something like this:

我需要做什么才能让 twig 处理 JavaScript 文件?我有一个使用 JavaScript 树枝的 html.twig。像这样的东西:

{% extends 'BaseBundle::layout.html.twig' %}

{% block javascripts %}
    {{ parent() }}
    {% javascripts
        '@BaseBundle/Resources/js/main.js.twig'
    %}
    {% endjavascripts %}
{% endblock %}

< more template omitted >

And parts of main.js.twig:

和 main.js.twig 的部分:

function testFunction()
{
    alert('{{VariableFromPHP}}');
}

And the controller:

和控制器:

/**
 * @Route("/",name="home")
 * @Template("MyBundle:Default:index.html.twig")
 */
public function indexAction()
{
   return array( 'VariableFromPHP' => 'Hello World');
}

I expected the JavaScript to look like this at run-time:

我希望 JavaScript 在运行时看起来像这样:

alert('Hello World');

But, the code is unchanged. Any ideas what I am doing wrong?

但是,代码不变。任何想法我做错了什么?

Thanks, Scott

谢谢,斯科特

采纳答案by Zsolt Gy?ngy?si

Assetic does not include twig templates; you should create a separate controller for the javascript file. Although I would consider it bad practice performance-wise, because you will have to process two requests this way.

Assetic 不包含树枝模板;您应该为 javascript 文件创建一个单独的控制器。尽管我认为在性能方面这是不好的做法,因为您必须以这种方式处理两个请求。

/**
 * @Route("/main.js")
 */
public function mainJsAction() {
    $params = array( 'test' => 'ok' );
    $rendered = $this->renderView( 'MyBundle:Default:main.js.twig', $params );
    $response = new \Symfony\Component\HttpFoundation\Response( $rendered );
    $response->headers->set( 'Content-Type', 'text/javascript' );
    return $response;
}
{% extends 'BaseBundle::layout.html.twig' %}

{% block javascripts %}
    {{ parent() }}
    <script type="text/javascript" src="{{ path('my_default_mainjs') }}"></script>
{% endblock %}

An alternative is to dump dynamic variables in the html, and only use static javascript files.

另一种方法是在 html 中转储动态变量,并且只使用静态 javascript 文件。

回答by pleerock

My sugestion to use global variables:

我建议使用全局变量:

{% javascripts '@AcmeBundle/Resources/public/js/*' output='js/compiled/main.js'%}
        <script>
            var TWIG = {};
            TWIG.variableOne   = "{{ path('rote_to_nowhere') }}";
            TWIG.variableTwo   = "{{ helloworldVar }}";
            TWIG.variableThree = "{{ "something"|trans }}";
        </script>
        <script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}

then you can use it in your jsfile:

然后你可以在你的js文件中使用它:

alert(TWIG.variableTwo);

回答by Doo Dah

Instead, what I did is this in the main.js:

相反,我在 main.js 中所做的是:

function doGetStringFromSubClass()
{
    if (typeof getStringFromSubClass == 'function')
    {
        return getStringFromSubClass();
    }
    else
    {
        alert('getStringFromSubClass() needs to be defined.')
    }
}

function testFunction()
{
    alert(doGetStringFromSubClass());
}

and, in the subclass twigs, we have this main.js:

并且,在子类 twigs 中,我们有这个 main.js:

function getStringFromSubClass(){
    return "baseClassString";
    }

And this twig:

还有这根树枝:

{% extends 'BaseBundle:Default:index.html.twig' %}

{% block javascripts %}
    {{ parent() }}

    {% javascripts
        '@SomeSubclassBundle/Resources/js/main.js'
    %}

    <script type="text/javascript" src="{{ asset_url }}"></script>
    {% endjavascripts %}
{% endblock %}

Kinda goofy, but, it works.

有点傻,但是,它有效。

Scott

斯科特