Costa Michailidis
Costa Michailidis

Reputation: 8178

Using jade as a template language for plain text

I'm using jade to craft html emails. You know, personalize the 'to' field, things like that. I'd like to provide a plain text version as well. Is there any way to compile jade to plain text?

Upvotes: 1

Views: 577

Answers (1)

Sid
Sid

Reputation: 523

If jade -> html -> plain is an acceptable solution you could do: http://requirebin.com/?gist=cbc4d126c842754fd545eff554a7767d

var htmlToText = require('html-to-text');
var jade = require('jade');

const jadeTemplate = `doctype html
html(lang="en")
  head
    title= pageTitle
    script(type='text/javascript').
      if (foo) {
         bar(1 + 5)
      }
  body
    h1 Jade - node template engine
    #container.col
      if youAreUsingJade
        p You are amazing
      else
        p Get on it!
      p.
        Jade is a terse and simple
        templating language with a
        strong focus on performance
        and powerful features.`;





var html = jade.compile(jadeTemplate, {})({});
var text = htmlToText.fromString(html, {
    wordwrap: 130
});



document.getElementById('text').value = text;

Upvotes: 2

Related Questions