Handling mustache indentation in Jade/Slim templates

162 views
Skip to first unread message

Robert Hall

unread,
Jan 11, 2016, 4:54:06 PM1/11/16
to Ractive.js
Consider the following valid Jade or Slim code:

| {{#if someVariableIsTrue}}
| {{#if showComponentOne}}
ComponentOne
| {{/if}}
| {{#if showComponentTwo}}
ComponentTwo
| {{/if}}
| {{/if}}

Versus a more readable but invalid structure:

| {{#if someVariableIsTrue}}
| {{#if showComponentOne}}
ComponentOne
| {{/if}}
| {{#if showComponentTwo}}
ComponentTwo
| {{/if}}
| {{/if}}

The first is valid, the second, while more readable, breaks the Jade/Slim parser.

Now imagine an application FULL of nested if statements and it starts to get a little gangly and very very unreadable.

Does anyone have a solution to this problem? The pipe | character is necessary to output the mustaches in the first place, but it makes code impossible to indent in any readable way.

(Some of you work in HTML, which can be nested however wanted; this is not that problem. This is Jade/Slim.)

Marty Nelson

unread,
Jan 11, 2016, 7:23:10 PM1/11/16
to Ractive.js
Based on this SO, you could do something like:


mixin
if(exp)
 
!= '{{#if ' + exp + '}}'
  block
 
!= '{{/if}}'
 
+if('someVariableIsTrue')
 
+if('showComponentOne')
   
ComponentOne
 
 
+if('showComponentTwo')
   
ComponentTwo




While I don't care for having to pass the expression as a string, it does provide full block semantics.

Robert Hall

unread,
Jan 12, 2016, 10:05:51 AM1/12/16
to Ractive.js
This is really helpful. Since I work in *both* Slim (at work) and Jade (personal) projects, I hope I can figure out an equivalent Slim version. This at least gives me a starting point.

Thanks!

- Robert

Marty Nelson

unread,
Jan 12, 2016, 10:21:27 AM1/12/16
to Robert Hall, Ractive.js
--
You received this message because you are subscribed to the Google Groups "Ractive.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ractive-js+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Robert Hall

unread,
Feb 18, 2016, 5:31:05 PM2/18/16
to Ractive.js, thebook...@gmail.com
Just found this awesome gem, FYI, for anyone looking for an alternative SLIM solution in the future:


It's not perfect; doesn't support `else` syntax, but that can be navigated still the old way:

~if someVariableIsTrue
~#if showComponentOne
ComponentOne
| {{else}}
ComponentTwo


Reply all
Reply to author
Forward
0 new messages