如何访问 PHP 在 javascript 的树枝中定义的常量?

How to access PHP defined constant in javascript of twig?

我使用 Silex 中的 Composer 自动加载了 constants.php 文件。

constants.php

<?php
define('SENT_SUCCESS_','Chat Transcript has been successfully sent');
define('INVALID_CASE_NUMBER','Invalid Case Number');
define('INVALID_FILE_EXTENSIONS','Following attachment(s) couldn\'t be uploaded with this chat due to an Invalid Extensions');
?>

我想在我的 index.html.twig 文件中的 js <script /> 标签中访问它。 我们可以很容易地在 js 中使用 php 变量,但我不知道如何使用 php 常量。 我试过了,但在 index.html.twig.

中不起作用
<script>
const INVALID = '{{INVALID_FILE_EXTENSIONS}}';
</script>

任何人都有想法,如何获得这个变量?

只需使用文档中显示的函数 constant

<script>
const INVALID = '{{ constant('INVALID_FILE_EXTENSIONS') }}';
</script>