How to use Markdown and Handlebars together? - node.js

We are using NodeJS + Express + Handlebars and it works just fine. Now we wont to use Markdown for our documentation. I looked for several days to find something I can use, and the best choice right now is mix of those packages
https://github.com/tj/consolidate.js
https://www.npmjs.com/package/marked-engine
and than register new app engine
app.engine('md', require('marked-engine').renderFile);
The main problem is that I can't use my handlebar layouts with this, so it would be great if there is another approach which would allow me to render Markdown inside handlebar layouts. And I would like to achieve that without Asemblio, Grunt or anything similar if it is possible.
Thank you in advance,
Jovan

This is a tipical use case for a filter in template engines like handlebars. So a fast google search for filters in handlebars lead me to this npm package. And a quick look to the docs show this
{{filter "## Hello *World*!" "markdown"}} → «<h2>Hello <em>World</em>!</h2>»
So maybe this is exactly what you are looking for, in case that this won't work, you should still look for filters, thats the way to go for your problem.
PS: I haven't tested this particular package

Related

Use coffeescript in jade templates

I like coffeescript and I like haml-based template engines. Im trying to figure out how do I use Jade with CoffeeScript? And I dont mean client scripts embedded in html, I mean server side logic.
Actually I found package exactly I want, but its very outdated: jade-coffee
Any suggestion for this?
I'm unsure if I'm understanding the question right or not but if you are trying to write coffeescript in jade you can make use of the Jade language filters.
http://jade-lang.com/reference/filters/
Hope this helps!

For loops and layout support in express.js template engine

I come from a PHP background and I've used frameworks such as CakePHP and Laravel and it is quite easy to work with layouts and views in all of them.
And the possibility of using PHP inside those template engines provide a way to do things such as:
<?php for($i=0; $i<1000; $i++){ ?>
<td>demo</td>
<? } ?>
Now, starting with Node and express.js I found out template engines seems quite basic here. I've tried hjs, hogan, swig, mustache, handlebars... none of them offers both :
Layout support (templates and views)
A way to do loops like the one I named before.
Am I missing something? Am I asking for too much?
Which one would you recommend me?
A lot of the template engines for Javascript take the philosophical view that it's better to enforce a fairly strict separation between logic and presentation, meaning that the complexity of code/logic allowed in the template is deliberately limited. For a quick overview of this topic, see http://blog.startifact.com/posts/older/the-new-hot-thing-in-web-development-client-side-templating-languages.html (it's about client-side templating, but since it's Javascript, a lot of those same template languages are the ones popular in node.js / Express). This idea of logicless templates exists in the PHP world as well, it's just not as common.
For a more academic treatment of this subject, see this paper: http://www.cs.usfca.edu/~parrt/papers/mvc.templates.pdf. (The author is also the author of a popular template engine for Java called StringTemplate.) The author makes a lot of good points, and in general I agree with the idea of logicless templates, but there are times when it can be inconvenient and I find myself more on the fence about it...see this link for some further considerations. Obviously there are also those who want to be able to anything from a template (as you can in PHP) and believe it's fine to rely on the self-restraint of the programmer not to put too much code in the view, which is where template engines like EJS come in.
Having said all that, it's important to note that what you want to achieve is possible in Handlebars (which is one of the "logicless" languages) and probably many of the others you tried as well. To do it in Handlebars, you'd need to create a custom helper. This might be what you're looking for:
https://www.npmjs.org/package/handlebars-helper-repeat
Example usage:
{{#repeat 10}}
{{> button }}
{{/repeat}}
You could also extend it to be able to support arguments to be able to control the starting number or increment, although that would probably be getting into logic that might be better done in the JS code (according to the Handlebars philosophy) while preparing the data for the template.
With regard to layouts, the closest thing in Handlebars (which is the template engine I'm most familiar with) is partials. This link provides a good introduction to those: http://blog.teamtreehouse.com/handlebars-js-part-2-partials-and-helpers.
Personally I'm a big fan of the template language in an awesome framework (built on Express) called Derby. Its template language is similar to Handlebars, but comes with a couple of handy extensions - just enough to make it a bit more convenient to use without allowing too much logic to creep into the template. Unfortunately I don't think there's a standalone version of it (i.e. you have to use the full Derby framework), but you could create custom helpers in Handlebars to achieve a similar effect.

MVC patterns with node.js with comon layout and partials

I am trying to apply mvc pattern (as in ruby) using node.js. I would like also to use a common layout and partials. I saw this post and I tried Locomotivejs, but I don't know how to apply a common layout and partials with Locomotivejs. Should I use another frameworks? if I should, which one has all these features? Could you please give me some suggestion?
You can see what I did in order to solve that problem with locomotive.js here:

Jade to Dust Parser?

I recently wrote using Jade Template for NodeJS. When talking with my manager.. I found that we use Dust within company. Thus I was required to switch over to dust.
While following the DRY principle.. I don't want to do this manually.
Is there good translator/parser to parse existing Jade template to Dust? I searched online but didn't found.
Additionally, if there's no such template, what about I go and implement one myself? I took compiler course before and thinking this would probably be not-to-hard to implement. But I never tried... and don't really understand the Dust template yet. how do you think of the difficulty of doing one parser myself?

Simplest suggestion box (like google suggestion when you using search) in PHP (without jQuery)

I can't find simple solution for suggestion in php/java script (no jQuery or something) so would like if some have some advice?
It is simple, when people search my site I want they to see suggestions, I made everything for 'feeding' that box, but can't code or find somewhere simple php/java script only solution, so please give me some useful link or code.
Also, forgot to say that I found "XMLHttpRequest" and made implementation which works great, but since I never ever used XMLHttpRequest I am not sure will it work all platforms and browsers? I tested in few (IE,F,Chorme,Safary) and it works on windows platform, but not sure will it work on other (Mobile platform for example) that XMLHttpRequest solution?
Go to the below link to find an awesome auto complete script without using any kind of javascript frameworks like jQuery or Prototype. Probably this might be the thing you are looking for.
http://dhtmlx.com/docs/products/dhtmlxCombo/index.shtml
UPDATE
Giving some extra links below you can find similar auto complete or
auto search without jQuery
http://www.brandspankingnew.net/specials/ajax_autosuggest/ajax_autosuggest_autocomplete.html
http://www.codeproject.com/Articles/8020/Auto-complete-Control
http://mattberseth.com/blog/2007/12/creating_a_google_suggest_styl.html
http://www.devthought.com/2008/01/12/textboxlist-meets-autocompletion/
http://wick.sourceforge.net/

Resources