Search code examples
expressswig-template

Template data not rendering in view with ExpressJS


My view has:

{% if (process.env.NODE_ENV == 'development') %}
    <!-- Livereload script rendered -->
    <script type="text/javascript", src="http://" + req.host  + ":35729/livereload.js"></script>
{% endif %}

So I'd expect the req.host to be replaced with localhost or something. But what renders is:

<script type="text/javascript", src="http://" + req.host + ":35729/livereload.js"></script>

My express config has: app.set('view engine', 'html');


Solution

  • You need to enclose your variable in the variable controls. {{ req.host }}

    {% if (process.env.NODE_ENV == 'development') %}
        <!-- Livereload script rendered -->
        <script type="text/javascript", src="http://{{ req.host }}:35729/livereload.js"></script>
    {% endif %}