Giter VIP home page Giter VIP logo

meteor-spacebars-tohtml's Introduction

Meteor Spacebars.toHTML

A simple helper function to assist with rendering spacebars to HTML. Works on both the server and the client.

Installation

$ meteor add dburles:spacebars-tohtml

API

Super basic example

Spacebars.toHTML({ name: 'foo' }, '<p>Hello {{name}}</p>');

Any helpers defined with Template.registerHelper will be rendered just like usual. If you're on the server you'll need to make sure they're not just defined on the client so it can see them.

Rendering templates on the server

We can render templates on the server by creating files within the private directory.

For example, we could create private/example.html and its contents might look like:

<html>
<head>
  <title>Example</title>
</head>
<body>
  <h1>Example</h1>
  
  <p>Hello {{name}} this is just an example!</p>
</body>
</html>

We can then render it using Meteor's Assets API

var html = Spacebars.toHTML({ name: 'foo' }, Assets.getText('example.html'));

This is very useful for sending HTML emails.

Iron Router

We can also serve templates using Iron Router's server side routes.

Here's a simple example:

Router.route('/test', function() {
  // This could also come from a Collection
  var data = { name: 'foo' };
  
  this.response.writeHead(200, { 'Content-Type': 'text/html' });
  this.response.end(Spacebars.toHTML(data, Assets.getText('example.html')));
}, { where: 'server' });

Template inclusion

Server side only

Currently there's no way to pull in templates using the {{> inclusion}} operator, though we can create our own helper to do much the same thing.

Template.registerHelper('include', function(template, data) {
  data = data || {};
  return Spacebars.toHTML(data, Assets.getText(template));
});

and within our template, for example:

<nav>
  {{{include 'navigation.html'}}}
</nav>
...

We can also pass in an optional data context:

<nav>
  {{{include 'navigation.html' navItems}}}
</nav>
...

Note that we use the triple-stash here so the HTML is not escaped.

License

MIT

meteor-spacebars-tohtml's People

Contributors

dburles avatar tmeasday avatar

Watchers

James Cloos avatar Martin Eboh avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.