Git Product home page Git Product logo

gotmpljs's Introduction

GoTmplJs

A program to compile Go templates into JavaScript.

Copyright (C) 2014 Jochen Voss

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Overview

The Go standard library provides a template language for generating HTML output in the html/template package. GoTmplJs is a program which can be used to compile Go templates into JavaScript, so that the same template can be used both for rendering the HTML version of a web page, and for rendering later updates via JavaScript. The JavaScript code generated by GoTmplJs is meant to be used with the Google Closure Tools.

warning: Implementation of GoTmplJs is not yet complete. It should work for simple templates, but it may generate invalid or wrong output in more complicated cases.

Example

Consider the following, simple Go template:

<p>Hello, {{.}}

If we store this template in a file, say test.tmpl, then we can convert this template to JavaScript using the following command:

gotmpljs -n templates test.tmpl

The -n option specifies the JavaScript name space to use for the resulting function. The generated code is written to standard output:

// generated by github.com/seehuhn/gotmpljs, do not edit

goog.provide('templates');

goog.require('seehuhn.gotmpl');


/**
 * Execute the "test" template.
 * @param {*} data The data to apply the template to.
 * @return {string} The template output.
 */
templates.test = function(data) {
  var res = new Array();
  res.push('<p>Hello, ');
  res.push(seehuhn.gotmpl.htmlescaper(data));
  res.push('\n');
  return res.join('');
};

The generated code depends on functions defined in the seehuhn.gotmpl namespace. The required code implementing these functions is distributed as part of the GoTmplJs source code, in the file gotmpl.js.

Installation

GoTmplJs can be installed using the go get command:

go get github.com/seehuhn/gotmpljs

gotmpljs's People

Contributors

seehuhn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  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.