Canny

Home
Download
Documentation/
  Examples
Mailing list
ChangeLog
Todo
Donate

SF project page

License: LGPL

SourceForge.net Logo

Support This Project
Canny
Smarty-like template library for Ruby.


About

Canny is a template library for Ruby. Its template format and features are very similar to that of the Smarty template system for PHP.

Canny "compiles" each template into Ruby code when it first encounters it, so subsequent accesses execute without the overhead of template parsing.


Important differences from Smarty

Note: See the list below also for differences with specific functions.

There are two important differences to be aware of when using Canny+Ruby if you're used to Smarty+PHP:
  • Literal values in the template must be quoted.
Smarty: { if $section == 1 }
Canny: { if $section == "1" }

Smarty: { if $options[0] == "yes" }
Canny: { if $options["0"] == "yes" }

Non-literal values do not need to be quoted:

Smarty/Canny: { if $section == $section_one }
Smarty/Canny: { if $options[$num] == $saved_options[$num] }
  • The empty string ('') and 0 evaluate to true by default (just like in Ruby).
C.assign('name', '')
C.assign('widgets', 0)

{ if $name }
      Welcome, { $name }!

      { if $widgets }
           You have { $widgets } widgets.
     {/if}
{else}
      Welcome, Guest!
{/if}

Will output:

Welcome,! You have 0 widgets.

You can change this behavior, however.

C.empty_string_is_nil = true
C.zero_is_nil = true
C.assign('name', '')
C.assign('widgets', 0)

Using the same template as above, this will output:

Welcome, Guest!


What works / what doesn't

 
 = Implemented
 
 = Not Implemented
 
 = Not Implemented, and no plans to implement

Methods
assign
display
register_modifier
template_dir=
compile_dir=
empty_string_is_nil=
zero_is_nil=

register_function

Modifiers
capitalize
count_characters
cat
count_paragraphs
count_sentences
count_words
date_format
Currently, ":" is not allowed in the format string, so "%H:%M:%S" will not work. Use : instead if working with HTML.
default
indent
lower
nl2br
regex_replace
replace
spacify
string_format
strip
strip_tags
truncate
upper

escape
wordwrap
Basics
capture
Currently, the contents of a {capture} block are evaluated in the context where the {capture} block is displayed, not where it is declared. This is a bug, but will not effect most uses of {capture}.
foreach,foreachelse
include
if,elseif,else
section,sectionelse
{ section name="index" loop=$loop }

Smarty: $data[index]
Canny: $data[$index]


include_ruby
ldelim,rdelim
literal
ruby
strip

config_load
insert

$canny variables
now
capture
section
foreach
template

env
get
config
const
cookies
post
request
server
session
version