Answer the question
In order to leave comments, you need to log in
How to inject Twig template variable into external js ?
Hello! :)
There is a site where twig template engine variables are substituted directly into js on the page. something like this: alert( "{{ alrt_message }}" );
but you need to close all scripts from indexing, taking them out into separate files and closing them through robots
and, of course, in the external file when generating the page, instead of the variable value, there will be the text itself, {{ alrt_message }}
tell me the solution, how can I remove scripts from the main content of the page while saving the twig file 'a
Thank you.
Answer the question
In order to leave comments, you need to log in
Actually, I've already come up with a solution. Create a function in php, which will show in js format everything that is transferred to it by the getom. Thus, without calling the script, I will substitute the necessary data in the link, and the browser will load them as if it were js.
I will end up with something like:
getdata = {
var1:"23",
var2:"hello"
}
{% set protected_fields = protected_route_info(protectedRoute, [], true).code %} {# получаем переменную в twig #}
<script> // и пробрасываем её в script
(function () {
let protectedFields = {{ protected_fields|json_encode|raw }};
console.dir(protectedFields);
})();
</script>
And what does indexing, robots.txt and external js files have to do with it?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question