外部 JS 文件中的 Twig 变量

发布于 2025-01-12 16:31:42 字数 117 浏览 4 评论 0原文

我想外部化我的 JS 代码,但代码中有 Twig 变量。
你有什么技巧可以让它发挥作用?

team: {{ 'Select your team'|trans }}

I would like to externalize my JS code, but there is Twig variable in the code.
What are your tricks to make this working?

team: {{ 'Select your team'|trans }}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青衫儰鉨ミ守葔 2025-01-19 16:31:42

当您需要将 twig 变量传递到外部 javascript 文件时,有两种方法

  1. 在 twig 模板中的脚本块内定义变量
<!DOCTYPE html>
<html>
   <head>
      <title></title>
   </head>
   <body>
       <script>
           var my_twig_var = {% if twig_var is defined %}'{{ twig_var }}'{% else %}null{% endif %}
       </script>
       <script src="scripts/functions.js"></script>
   </body>
</html>

对于这种方法,我通常在我的主/基本模板中添加一个名为“javascript”块的块

base.twig.html

<!DOCTYPE html>
<html>
   <head>
      <title></title>
   </head>
   <body>
       {% block body %}
       {% endblock %}
       {% block javascript %}
       {% endblock %}
   </body>
</html>

page.html.twig

{% extends base.twig.html %}
{% block body%}
    <h1>Hello World</h1>
{% endblock %}
{% block javascript %}
    <script>
      alert('{{ twig_var|default('Hello World') }}');
    </script>
{% endblock %}

  1. 借助 data-* 属性将变量传递给 javascript
<div data-foo="{{ foo }}">...</div>
$(function() {
    $(document).on('click', '.button', function(e) {
        console.log($('div[data-foo]').data('foo'));
    });
});

旁注:如果你想传递一个对象或一个数组 该过滤器会将变量转换为有效的 javascript 对象

twig 您始终可以使用 json_encode 过滤器,如果您想控制 公开哪些对象属性, json_encode 过滤器,您始终可以实现接口 Serialized

There a two approaches when you need to pass a twig variable to an external javascript file

  1. Define the variables inside a script block in the twig template
<!DOCTYPE html>
<html>
   <head>
      <title></title>
   </head>
   <body>
       <script>
           var my_twig_var = {% if twig_var is defined %}'{{ twig_var }}'{% else %}null{% endif %}
       </script>
       <script src="scripts/functions.js"></script>
   </body>
</html>

For this approach I usally add a block called "javascript" block in my main/base template

base.twig.html

<!DOCTYPE html>
<html>
   <head>
      <title></title>
   </head>
   <body>
       {% block body %}
       {% endblock %}
       {% block javascript %}
       {% endblock %}
   </body>
</html>

page.html.twig

{% extends base.twig.html %}
{% block body%}
    <h1>Hello World</h1>
{% endblock %}
{% block javascript %}
    <script>
      alert('{{ twig_var|default('Hello World') }}');
    </script>
{% endblock %}

  1. Pass the variables to javascript with the aid of data-* attributes
<div data-foo="{{ foo }}">...</div>
$(function() {
    $(document).on('click', '.button', function(e) {
        console.log($('div[data-foo]').data('foo'));
    });
});

Sidenote: if you want to pass an object or an array to twig you can always use the json_encode filter, which will convert the variable to a valid javascript object

If you want to have control over which object properties are exposed by the json_encode filter you can always implement the interface Serializable

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文