Creating your own template engine in JavaScript: part 1.5

Tuesday, November 19, 2013 Posted by Ruslan Matveev 2 comments
It's been a while since I had a time to write something here. Yeah yeah yeah, I'm just lazy guy, but I will try to do it more often. Last time I've promised you second part of this never ending story, it's been about a year ago! I learn many new tricks, so before going into the second part of "creating your own template engine in JavaScript", I've decided to rewrite my uber Tokenizer from the first part. What was wrong with that? Well many things:

  • - too many strings and identifiers that I had to use every time when I ask it for a token
  • - escaping regular expressions in the string literals was really pain in the ass
  • - ignored tokens (which is sometimes very nice to have) were completely ignored and there was no way to read them somehow
  • - all this context switching things were looking very nice a year ago, but now I would make it a bit different

Cross - platform wrapper function for JavaScript libraries

Thursday, October 25, 2012 Posted by Ruslan Matveev 0 comments
In old times, when JavaScript libraries had to work only in the web - browsers, common pattern for exporting your stuff into the global namespace was looking like this:

(function() {
    window['MyLibraryMethod'] = function() {
        alert('HELLO WORLD!');
    };
})();

We were just attaching our methods and properties to the global namespace, and this was all fine before somebody come up with the idea to run JavaScript code in non - browser environment. There was no window object so we had to find global object ourselves:

(function(global) {
    global['MyLibraryMethod'] = function() {
        alert('HELLO WORLD!');
    };
})(function() { return this; }.call(null));

Then things started to change very quickly, CommonJS standard and it's implementations against AMD (Asynchronous Module Definition), RequireJS and so on, but how should I package my library so the end - users won't have to repackage it in order to use it?

Mozilla Rhino + CommonJS - making a smart modules

Monday, October 15, 2012 Posted by Ruslan Matveev 0 comments
Hi all, in my last post about Mozilla Rhino CommonJS support I've described the basics. In this post I'll explain you how to organize your code so it can work when there is no CommonJS sandbox enabled. Imagine that we have decided to organize all our Mozilla Rhino libraries as CommonJS modules, but what if there is an old project that has no knowledge of this change, and it still relies on old good load() function that is importing everything into a global namespace? The question is: is that possible to make a library that can work same way with and without CommonJS support?

Using CloudMine to store your application data in the cloud

Saturday, October 6, 2012 Posted by Ruslan Matveev 0 comments
Nowadays HTML5 becomes more and more popular, offline cache can help you when you need to build an application that does not require a web - server, client - side storage can be used to store application data in built - in web - browser database. But what if you need to share application data between users? There are many web - applications that allows you to share your work with the rest of the world, for example jsFiddle allows you to save your code snippet and get a permanent link that you can post in twitter or send to your friend. So how can you make something like this using HTML and JavaScript only?

Histone template engine playground application

Thursday, October 4, 2012 Posted by Ruslan Matveev 0 comments
Histone template engine now has it's own online playground application that allows you to test your templates and play with examples right in your web - browser.

Histone template engine online playground

Histone - open source template engine for Java, JavaScript and PHP

Friday, August 24, 2012 Posted by Ruslan Matveev 0 comments
Hi all, it's been a while since my last post, but I'm back and I have a good news. I'm happy to give you a short introduction to a project that I've been working on with my team for the last half a year. It's finally came to the state when I can share it with all of you. Histone is cross-platform open source template engine, implemented in JavaScript, Java and PHP.

Creating your own template engine in JavaScript: part 1

Monday, March 5, 2012 Posted by Ruslan Matveev 6 comments
Today I'm going to tell you how to create your own template engine in JavaScript. The question is why? There are dozens of template engines already available so why do we need to create another one? This is the list of my reasons:

  • - There are many template engines but most of them are built with no API to extend it (so you cannot add your own function, your own constructions and so on...)
  • - There are JavaScript template engines that compile your template into plain JavaScript, on one hand it's good, but on the other hand - we cannot reuse (port) this code to other languages (for example Java), so I've decided to make it easily portable.
  • - I used to use Smarty template engine for PHP, so I wanted to make my template engine syntax look close to it.

Mozilla Rhino CommonJS module support

Friday, September 30, 2011 Posted by Ruslan Matveev 0 comments
Good news, last version of Mozilla Rhino (1.7R3) introduces a fully compliant CommonJS module implementation, and in this blog post I'm going to give you a brief introduction on how to use it in your projects. Okay, so how do we normally manage dependencies in the JavaScript code that is running under Mozilla Rhino? Normally when you want to include the contents of another JavaScript file (for example some library), you call load() function (provided by Rhino shell) and pass the name of the file that you want to include as an argument:

// include XSLTProcessor library
load('XLSTProcessor.js');
// use XSLTProcessor class
var xsltProcessor = new XSLTProcessor('test.xsl');

JavaScript interface for Saxon XSLT processor and Mozilla Rhino

Monday, September 12, 2011 Posted by Ruslan Matveev 0 comments
In one of my previous posts, I've explained how to set up Apache Ant and Mozilla Rhino in order to create platform independent JavaScript environment. I've also mentioned that you can extend JavaScript by accessing Java packages and classes. Last time I showed you how to make use of java.io.File class in order to retrieve list of the files in the specific directory, this very simple example might have made you think about what else can you do with it? So today I'm going to focus a little bit more on this essential part of Mozilla Rhino JavaScript engine, by showing you how to perform XSLT transformation using Saxon XSLT processor.

Managing dependencies with RequireJS

Tuesday, September 6, 2011 Posted by Ruslan Matveev 1 comments
As web applications becoming more and more mature, they inherit more and more techniques and patterns from their older brothers - desktop applications. One of the things that's been there for a while is dependency management. JavaScript itself doesn't provide such functionality out of the box, so there is no way to include file "b.js" from file "a.js" as you would do that in php or any other server side language. However there are plenty of solutions available, that will help you to organize dependency management in your JavaScript code. Today I'm gonna give you an introduction into managing dependencies with RequireJS.