Charles
Charles

Reputation: 11758

Jade include template based on a variable

I would like to add a template with a name based on a variable.

Something like that:

include= templateName

Upvotes: 4

Views: 1892

Answers (2)

jcoffland
jcoffland

Reputation: 5431

There is a very good reason this doesn't work in Jade. Jade compiles templates into Javascript then executes the Javascript to produce HTML. Includes must be processed during the compile stage but Javascript expressions are not evaluated until later. Therefore it is currently impossible for Jade to offer variable includes.

carlituxman's solution works because it includes all of the needed include files in the generated Javascript, but it's limited to include files that you know of ahead of time.

Upvotes: 0

carlituxman
carlituxman

Reputation: 1221

I solve with case/when, like switch/case:

case myvar
    when 0
      include mytemplate0
    when 1 
      include mytemplate1
    ....
    default
        p Select option

Upvotes: 1

Related Questions