Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hogan JS IF statements

I don't really like the jade syntax and was wondering if I could do this simple comparison using hoganJS instead?

The example code is written in JADE.

I did some googling and there seems to be mixed opinion.. I just want to know if there is a way or will I need to change something?

if user
 li
   a(href='/dashboard') Dashbaord
 li
   a(href='/logout') Logout
else
 li
   a(href='/login') Logi§n

block body
like image 317
user61026 Avatar asked Jul 26 '14 11:07

user61026


1 Answers

Hogan is an implementation of Mustache so the same syntax applies.

{{#user}}
  <li><a href="/dashboard">Dashboard</a></li>
  <li><a href="/logout">Logout</a></li>
{{/user}}
{{^user}}
  <li><a href="/login">Login</a></li>
{{/user}}

PS I used to debate whether to use Hogan or some other Mustache implementation over Handlebars because it was little faster/lighter. My advice is to use Handlebars not Hogan, and compile your front end and only use Handlebars runtime on build - because it has nicer conditional syntax and supports a few more useful things without going too over the top.

In Handlebars it would be the cleaner:

{{#if user}}
...
{{else}}
...
{{/if}

But anyway Hogan is still nice, so your choice. I also don't like Jade it reminds me of CoffeeScript or something.

like image 190
Dominic Avatar answered Oct 27 '22 00:10

Dominic