gotmpljs

command module
v0.0.0-...-92a274e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 1, 2016 License: GPL-3.0 Imports: 12 Imported by: 0

README

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.

.. _Go templates: http://golang.org/pkg/html/template/

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`_.

.. _html/template: http://golang.org/pkg/html/template/
.. _Google Closure Tools: https://developers.google.com/closure/

**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 :code:`test.tmpl`, then we
can convert this template to JavaScript using the following command::

  gotmpljs -n templates test.tmpl

The :code:`-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
:code:`seehuhn.gotmpl` namespace.  The required code implementing
these functions is distributed as part of the GoTmplJs source code, in
the file `gotmpl.js`_.

.. _gotmpl.js: https://github.com/seehuhn/gotmpljs/blob/master/gotmpl.js

Installation
------------

GoTmplJs can be installed using the :code:`go get` command::

  go get github.com/seehuhn/gotmpljs

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL