pass django template file content to javascript

I want to pass a template file content to my javascript file for later use to create DOM elements there. What I have tried is to pass it to js as a variable with an include tag like this:

<script>const list_item = {% include 'myapp/list_item.html' %}</script>

But this end up getting:

Uncaught SyntaxError: Unexpected token ‘<‘

Is there a way I can pass template file content to js file?

Thanks in advance

Answer

Looking just at your sample (not making any recommendations about other possible ways to achieve the desired result), it should be:

<script>const list_item = "{% include 'myapp/list_item.html' %}";</script>

The content of the template file should be included as a string, otherwise it will keep throwing a syntax error.