Home

Awesome

MGTemplateEngine

By Matt Legend Gemmell<br> http://mattgemmell.com/

What is it?

MGTemplateEngine is a native Cocoa system for generating text output based on templates and data. It's a close cousin of systems like Smarty, FreeMarker, Django's template language, and other such systems.

The default syntax for markers (functions or language-constructs) is:

{% for 1 to 5 %} foo {% /for %}

and the default syntax for variables/expressions is:

{{ foo.bar | uppercase }}

The pipe-character indicates a filter is being applied; i.e. the value of "foo.bar" will then be fed to the "uppercase" filter before being displayed. You can apply filters to markers as well as variables.

The marker, variable and filter delimiters are completely customizable, so you're not stuck with the defaults if you prefer different syntax.

Features

MGTemplateEngine offers the following features:

Requirements

MGTemplateEngine requires Mac OS X 10.5 (Leopard) or later.

License

Please see the included Source Code License file for the license this code is released under. Summary: it's an attribution license. Credit me, and you can freely use, modify and redistribute in source or binary forms as you see fit. Closed-source/commercial use is absolutely fine.

Extensibility

MGTemplateEngine offers 3 main types of extensibility, as detailed below. You should also read the included documents specific to each API for more details.

  1. Markers. You can create new markers, which provide new tags for use in templates. Markers can be standalone or can be complex blocks (like if-else-/if), can iterate/loop, enable or disable output, set new variables within their scope, and much more.

  2. Filters. Filters modify data for display purposes, for example the built-in "date_format" filter which formats an NSDate as a string using a specified formatting definition. You can easily write new filters to format/transform your data in new ways.

  3. Matchers. A matcher is a very important object which performs a conceptually simple task: it finds the next marker or expression in a template, and splits it into its components (such as the marker name or variable, extra arguments, and any filter specified). MGTemplateEngine ships with two matchers which you can choose between, or implement your own:

You can freely write your own matcher if you don't want to link against libicucore, or don't want to include RegexKit in your application. For example, you could write a matcher which uses NSScanner, or one which uses OgreKit instead.

Standard language features

All language features are implemented as plug-in markers, so you can freely inspect and modify how they work. At time of writing, MGTemplateEngine supports the following constructs:

Note: both "for" constructs provide several useful variables during the body of the loop, including currentLoop.currentIndex, currentLoop.startIndex and so on, including currentLoop.parentLoop if appropriate.

Standard data filters

There are a few standard filters currently included with MGTemplateEngine, and you can easily add your own. The standard ones are:

Feature Requests and Bug Reports

Please submit any feature requests or bug reports to me via email; you can find my address on the About page of my site, here: http://mattgemmell.com/about

I hope you enjoy using MGTemplateEngine!

Cheers, -Matt Legend Gemmell

http://mattgemmell.com/