JADE: Sirviendo recursos específicos de la ruta
Pregunta
En mi ./inc/footer.jade
, tengo un montón de etiquetas de script.Me gustaría servir script adicional solo en una ruta específica. ¿Cómo lograr esto?
en los controladores que podría hacer:
res.render('someTemplate', {
extraScript: true
});
y en footer.jade
- if (extraScript)
<script src="/script.js"></script>
Pero entonces tendría que incluir esta variable 'extrascript' en cada ruta (se establece en falso).
Solución
You can use typeof
to check whether a variable exists.
- if (typeof extraScript !== 'undefined')
<script src="/script.js"></script>
In this way, for all the routes that do not need the additional script, you simply don't pass in extraScript
, and then in the template, typeof extraScript !== undefined
will be evaluated to false
.
Otros consejos
The extraScript solution works, but I believe a better solution would be to use template inheritance (block and extends keywords) . This way you don't have to add logic and a local variable to your templates, and you can still keep things DRY in your jade markup.