简体   繁体   中英

How to access PHP defined constant in javascript of twig?

I have constants.php file autoloaded using composer in Silex.

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');
?>

I want to access it in my index.html.twig file in js <script /> tag. We can easily use php variable in js but I don't have idea, how to use php constant. I tried this but not working in index.html.twig .

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

Anyone have idea, how to get this variable?

Just use the function constant as seen on the documentation

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM