Coder Social home page Coder Social logo

text-caml's Introduction

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.

  • Boolean, have_comments is defined, not zero and not empty.

      # have_comments => 1
      {{#have_comments}}
      We have comments!
      {{/have_comments}}
    
      We have comments!
    
  • Array, list is a non-empty array reference. Special variable {{.}} is created to point to the current element.

      # list => [1, 2, 3]
      {{#list}}{{.}}{{/list}}
    
      123
    
  • Hash, hash is a non-empty hash reference. Context is swithed to the elements.

      # hash => {one => 1, two => 2, three => 3}
      {{#hash}}
      {{one}}{{two}}{{three}}
      {{/hash}}
    
      123
    
  • Lambda, lambda is an anonymous subroutine, that's called with three arguments: current object instance, template and the context. This can be used for subrendering, helpers etc.

      wrapped => sub {
          my $self = shift;
          my $text = shift;
    
          return '<b>' . $self->render($text, @_) . '</b>';
      };
    
      {{#wrapped}}
      {{name}} is awesome.
      {{/wrapped}}
    
      <b>Willy is awesome.</b>
    

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, [email protected]

CREDITS

Sergey Zasenko (und3f)

Andrew Rodland (arodland)

Alex Balhatchet (kaoru)

COPYRIGHT AND LICENSE

Copyright (C) 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.

text-caml's People

Contributors

und3f avatar kaoru avatar arodland avatar nowox avatar

Watchers

Graham Blache avatar Steve Peterson avatar mellissa avatar Tom Harman avatar Laura Madsen avatar Curtis Thompson avatar Mark Wilkie avatar Gordon Mei avatar Ricardo Soares avatar John Templon avatar Patrick Carey avatar Nick Bauman avatar John Cleveley avatar Phil Olson avatar Ryan Johnson avatar Stephane Krzywoglowy avatar Nikolay Aviltsev avatar Suman Deb Roy avatar Kevin Lord avatar Francisco Albarran Cristobal avatar James Burns avatar  avatar Gilad Lotan avatar Jaim Zuber avatar Jesse Shapins avatar Erik Price avatar Ryan Oliver avatar Alp Aker avatar Randy Karels avatar James Cloos avatar John Niedermeyer avatar Greg Thompson Jr. avatar Justin Hines avatar Andrew Paulus avatar Tony Ngo avatar Dave Marchevsky avatar  avatar Lyle Payne Morgan Smith avatar Walter Menendez avatar Jeff Revesz avatar Chris Kelley avatar Ola Sendecka avatar Patty Delgado avatar Brian Lee avatar  avatar Kyle Lucovsky avatar Jane Kelly avatar Brian Clifton (he/him) avatar Jennifer avatar Raymond Wong avatar Funmi Doro avatar  avatar Max Woolf avatar Sofi Millares avatar Anita Mehrotra avatar Kate Zasada avatar Salim Rekaibi avatar Motahera Shermin avatar  avatar Ivan Lavriv avatar Benjamin Running avatar Dat Boi Diego avatar  avatar Jane Hwang avatar Arun avatar Jake Levy avatar  avatar  avatar Jay Gordon avatar Renée Carrington avatar Phil Wilson avatar Sergey avatar Maxim Kapustin avatar  avatar  avatar millie tran avatar Natalya avatar Lam Thuy Vo avatar  avatar Raz Shlomovich avatar Lyubomyr Rudko avatar Viktor Kushchenko avatar  avatar Swati Vauthrin avatar  avatar Kate avatar  avatar Songxiao Zhang avatar  avatar  avatar  avatar  avatar John Andrews avatar Dilip Rajan avatar igor avatar Sean avatar Polina Giralt (pgiralt) avatar Dan Meruelo avatar Nikhil Sethi avatar Jac avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.