Skip to content

Instantly share code, notes, and snippets.

@zoobestik
Created October 10, 2012 22:56
Show Gist options
  • Save zoobestik/3869070 to your computer and use it in GitHub Desktop.
Save zoobestik/3869070 to your computer and use it in GitHub Desktop.
Yate templates rendering with nodejs
module "index"
match / {
html('<!doctype html>')
<html class="no-js" lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title></title>
<meta name="description" content="" />
<meta name="viewport" content="width=device-width" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head>
<body>
</body>
</html>
}
/*
* author: kb.chernenko@gmail.com
* description: yate templates rendering with nodejs
*
* yate: https://github.com/pasaran/yate
* algorithm based: https://github.com/mctep/yate-express
*/
var fs = require('fs'),
yate = require('yate');
module.exports = YateRuntime = function(opts) {
opts = opts || {}
var runtimePath = opts.runtimePath || './node_modules/yate/lib/runtime.js';
this.runtime = new Function(fs.readFileSync(runtimePath)+';return yr;')();
};
YateRuntime.prototype.addModule = function(path) {
var js = yate.compile(path).js;
new Function('yr', js)(this.runtime);
return this;
}
YateRuntime.prototype.apply = function() {
return this.runtime.run.apply(this.runtime, arguments);
}
@mctep
Copy link

mctep commented Oct 15, 2012

Хм.. Я как-то пробовал сделать один экземпляр рантайма, на все шаблоны, но он ругался на, то, что в нем уже создан модуль 'main'. Поэтому пришлось рантайм делать на каждый шаблон отдельно.

В твоем случае addModule каждый раз будет вызывать yr.register('main', M); Не будет ли той-же проблемы? Или я что-то путаю?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment