Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jade - way to add dynamic includes

Tags:

express

pug

I'd like to do something like the following within a jade template.

include page-content/#{view.template}

As this won't work I have ended up with.

-if(view.path==="/")
include ../page_content/home
-else if(view.path==="/login/")
include ../page_content/login
-else if(view.path==="/join/")
include ../page_content/join
-else if(view.path==="/user/")
include ../page_content/user
 ad nauseum  

I asked TJ whether it was possible, he replied

unfortunately no, they're compile-time includes, which is somewhat necessary for a few technical reasons that I wont get into but we may eventually need to add a dynamic alternative

I'm wondering if anyone has come up with any alternatives, for example using view helpers.

I'm stuck with a big config file to generate the views - and the if-else statements in the template , I know are going to come back and haunt me. :)

If this is possible using another engine, like ejs or mustache, I'd love to know.

Any ideas much appreciated.

like image 949
Chin Avatar asked Dec 06 '11 02:12

Chin


1 Answers

Feels like way too much logic in the view to me. Seems like the best way to do this would be through a dynamicHelper or possibly a mixin

like image 73
Paul Avatar answered Nov 09 '22 16:11

Paul