Search code examples
handlebars.jspugmustache

Jade precompile input attributes with handlebars conditions


Short version: I'd like jade to generate something like {{#if readOnly}}readonly{{/if}} attribute. Is this possible?

I precompile my Jade templates to client side handlebars .hbs templates files so that jade syntax like

input(type="text", name="dlg_{{fieldName}}",id="dlg_{{fieldName}}")

becomes with precompilation

<input type="text" name="dlg_{{fieldName}}" id="dlg_{{fieldName}}" />

I want to include a client side conditional "readonly" attribute but this kind of solution doesn't work

input(type="text",readonly="{{#if readOnly}}true{{else}}false{{/if}}",name="dlg_{{fieldName}}",id="dlg_{{fieldName}}")

what I need to generate at precompilation (no jade conditionals) is

<input type="text" {{#if readOnly}}readonly{{/if}} name="dlg_{{fieldName}}" id="dlg_{{fieldName}}" />

I know that I can include directly the html code in the jade template but this voids the interest and the beauty of jade enterely.

Solutions like this also work but are not optimal

|{{#if readOnly}}
input(type="text", name="dlg_{{fieldName}}",id="dlg_{{fieldName}}",readonly)
|{{else}}
input(type="text", name="dlg_{{fieldName}}",id="dlg_{{fieldName}}")
|{{/if}}

I also thought to include an attribute like "data-readony={{#if readOnly}}true{{else}}false{{/if}}" and set the readonly on client side but this needs more processing after the template is rendered.


Solution

  • To simply answer,I think you can not. I'd rather recommend using jade client-side:

    if readOnly
      input(type="text", name="dlg_#{fieldName}", id="dlg_#{fieldName}", readonly)
    

    just pass {client: true} to jade.