buzzfeed/text-caml

Name: text-caml

Owner: BuzzFeed

Description: A mustache-like template engine for Perl

Created: 2015-06-19 19:24:36.0

Updated: 2015-10-02 08:58:25.0

Pushed: 2015-08-27 22:26:12.0

Homepage:

Size: 199

Language: Perl

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

NAME

Text::Caml - Mustache template engine

SYNOPSIS

my $view = Text::Caml->new;

my $output = $view->render_file('template', {title => 'Hello', body => 'there!'});

# template
<html>
    <head>
        <title>{{title}}</title>
    </head>
    <body>
        {{body}}
    </body>
</html>

$output = $view->render('{{hello}}', {hello => 'hi'});

DESCRIPTION

Text::Caml is a Mustache-like (http://mustache.github.com/) template engine. That means it tends to have no logic in template files.

Syntax
Context

Context is the data passed to the template. Context can change during template rendering and be specific in various cases.

Variables

Variables are inserted using {{foo}} syntax. If a variable is not defined or empty it is simply ignored.

Hello {{user}}!

By default every variable is escaped when parsed. This can be omitted using & flag.

# user is '1 > 2'
Hello {{user}}! => Hello 1 &gt; 2!

Hello {{&user}}! => Hello 1 > 2!

Using a . syntax it is possible to access deep hash structures.

# user => {name => 'Larry'}
{{user.name}}

Larry
Comments

Comments are ignored. They can be multiline too.

foo{{! Comment}}bar

foo{{!
Comment
}}bar
Sections

Sections are like iterators that iterate over your data. Depending on a variable type different iterators are created.

Inverted sections

Inverted sections are run in those situations when normal sections don't. When boolean value is false, array is empty etc.

# repo => []
{{#repo}}
  <b>{{name}}</b>
{{/repo}}
{{^repo}}
  No repos :(
{{/repo}}

No repos :(
Partials

Partials are like inludes in other templates engines. They are run with the current context and can be recursive.

{{#articles}}
{{>article_summary}}
{{/articles}}

ATTRIBUTES

templates_path
my $path = $engine->templates_path;

Return path where templates are searched.

set_templates_path
my $path = $engine->set_templates_path('templates');

Set base path under which templates are searched.

default_partial_extension

If this option is set that the extension is automatically added to the partial filenames.

my $engine = Text::Caml->new(default_partial_extension => 'caml');

---
{{#articles}}
{{>article_summary}} # article_summary.caml will be searched
{{/articles}}

METHODS

new
my $engine = Text::Caml->new;

Create a new Text::Caml object.

render
$engine->render('{{foo}}', {foo => 'bar'});

Render template from string.

render_file
$engine->render_file('template.mustache', {foo => 'bar'});

Render template from file.

DEVELOPMENT

Repository
http://github.com/vti/text-caml

AUTHOR

Viacheslav Tykhanovskyi, vti@cpan.org

CREDITS

Sergey Zasenko (und3f)

Andrew Rodland (arodland)

Alex Balhatchet (kaoru)

COPYRIGHT AND LICENSE

Copyright © 2011-2015, Viacheslav Tykhanovskyi

This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.