Skip to content

mpneuried/soyer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

soyer

Build Status Dependency Status NPM version

Soyer is small lib for serverside use of Google Closure Templates with node.js.

NPM

Thanks to Daniel Pupius for soynode. I used this module as template and added the language support and removed the compile features.

Install

  npm install soyer

Initialize module:

var Soyer = require("soyer");
var mySoyer = new Soyer( config );

mySoyer.load( function( err, success ){
	if( err ){
		throw err
	} else {
		console.log( "templates sucessfully loaded" );
	}
});

####config object description

  • path : ( String required )
    absolute path to the directory where the module can find compiled soy files.
  • soyFileExt : ( String optional: default = ".soy.js" )
    soy file extension to select only the compiled soy files.
  • languagesupport : ( Boolean optional: default = false )
    enable the language support. If true the following options are relevant.
  • defaultlang : ( String optional: default = "en-us" )
    the default language code if the passed code will not fit.
  • availibleLangs : ( Array optional: default = "[ "en-us", "de-de" ]" )
    a list of valid language codes
  • extractLang : ( Function optional )
    a method to extract the language-code out of the filename. The filename will be passed to the method and should return a valid language code.

Example

var Soyer = require( "soyer" );

var mySoyer = new Soyer({
	path: path.resolve( __dirname, "../path/to/templates/" ) 
});

mySoyer.load( function( err, success ){
	if( err ){
		throw err
	}

	var rendered = mySoyer.render( "myNamespace.myTemplate", { param1: "abc" } );
	console.log( rendered );
});

Advanced example

// files in folder: template.soy, template.en.js, template.fr.js, template.de.js

var Soyer = require( "soyer" );

var mySoyer = new Soyer({
	path: path.resolve( __dirname, "../path/to/templates/" ) ,
	soyFileExt: ".js",
	languagesupport: true,
	defaultlang: "de",
	availibleLangs: [ "en", "de", "fr" ],
	extractLang: function( file ){
		var _lang = file.split( "." )[1]
		return _lang
	}
});


mySoyer.load( function( err, success ){
	if( err ){
		throw err
	}

	var renderedDE = mySoyer.render( "myNamespace.myTemplate", "de" { param1: "deutsch" } );
	console.log( renderedDE );

	var renderedEN = mySoyer.render( "myNamespace.myTemplate", "en" { param1: "english" } );
	console.log( renderedEN );

	var renderedFR = mySoyer.render( "myNamespace.myTemplate", "fr" { param1: "français" } );
	console.log( renderedFR );
});

get a template method ( GET )

Get's a method to render a template.

mySoyer.get( name, [ lang ] ) Arguments :

  • name: ( String required )
    soy path of the template.
  • lang: ( String required )
    the language to render if languagesupport is activated.

Example

var fnTemplate = mySoyer.get( "myNamespace.path.to.template" );

console.log( fnTemplate( { param1: "hello world" } ) );

render a template ( RENDER )

render a template immediately

mySoyer.get( name, [ lang ] ) Arguments :

  • name: ( String required )
    soy path of the template.
  • lang: ( String optional )
    the language to render if languagesupport is activated.
  • data: ( String optional: default = {} )
    template data.

Example

var rendered = mySoyer.render( "myNamespace.path.to.template", { param1: "hello world" } );
console.log( rendered );

routing helper

usually you will use soyer within a routing framework like express.
In this case the server has to finish the loading of the templates before the first .render() is called.
So you can use the method routingWait to add a middleware and make sure the templates has been loaded until the first rendering starts.

To use it you just have to add and call the method [ your soyer instance ].routingWait() as middleware.
Buff This is designed to fit to express. But you can use it in other tools, too. You just have to make sure the last argument of your routing framework is the next method ( e.g. in express it's ( request, response, next ) ).

Example

Soyer = require("soyer");

var express = require("express");
var app = express.createServer();

var mySoyer = new Soyer( config );
mySoyer.load( function( err ){
	if( err ){
		throw err
	}
});

app.get( "/myroute/:id", mySoyer.routingWait(), function( req, res ){
	// do your stuff
});

app.listen()

###General info

To define a locale my best practice is a combination of language-code ISO 639 and country-code ISO 3166.
But you can define your own logic with this module.

Release History

Version Date Description
v0.3.5 2015-04-14 Dropped node 0.6 and 0.8 travis tests, because the dependencies to build the modules are not compatible. But i think the compiled version from npm should already work
v0.3.4 2015-04-14 Added try catch during vm context create; changed to a more modern development env with grunt; Switched to lodash
v0.3.3 2014-10-30 Fixed bug in routingWait method
v0.3.1 2013-12-04 Fixed bug to ignore hidden files ( prefixed with a . )
v0.3.0 2013-03-04 Updated soyutils to version Dez. 2012

NPM

Other projects

Name Description
rsmq A really simple message queue based on Redis
rsmq-worker RSMQ helper to simply implement a worker around the message queue
redis-notifications A redis based notification engine. It implements the rsmq-worker to safely create notifications and recurring reports.
node-cache Simple and fast NodeJS internal caching. Node internal in memory cache like memcached.
redis-sessions An advanced session store for NodeJS and Redis
obj-schema Simple module to validate an object by a predefined schema
connect-redis-sessions A connect or express middleware to simply use the redis sessions. With redis sessions you can handle multiple sessions per user_id.
systemhealth Node module to run simple custom checks for your machine or it's connections. It will use redis-heartbeat to send the current state to redis.
task-queue-worker A powerful tool for background processing of tasks that are run by making standard http requests.
grunt-soy-compile Compile Goggle Closure Templates ( SOY ) templates inclding the handling of XLIFF language files.
backlunr A solution to bring Backbone Collections together with the browser fulltext search engine Lunr.js

License

(The MIT License)

Copyright (c) 2013 M. Peter

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

use Google's Closure Templates inside node.js.

Resources

License

Stars

Watchers

Forks

Packages

No packages published