How to access PHP defined constant in javascript of twig?

1.4k Views Asked by At

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?

1

There are 1 best solutions below

2
DarkBee On BEST ANSWER

Just use the function constant as seen on the documentation

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