Canny

Home
Download
Documentation/
  Examples
Mailing list
ChangeLog
Todo
Donate

SF project page

License: LGPL

SourceForge.net Logo

Support This Project


This page features a basic example to get you started with Canny.

For details on syntax and available features, refer to the Smarty manual
and the implemented features list.

example.rb

#!/usr/bin/env ruby

require "Canny"

# Template and compile directories should be outside your
# webroot if serving web pages.
C = Canny.new
C.template_dir = 'templates'
C.compile_dir = 'templates_c'

name = 'Canny user'

people = [
          {'name' => 'John Doe', 'city' => 'New York', 'zip' => '10000'},
          {'name' => 'Jane Doe', 'city' => 'Los Angeles', 'zip' => '90000'},
      ]

fruits = ['Apple', 'Orange', 'Banana', 'Strawberry', 'Cherry', 'Lemon', 'Lime']

def my_modifier(str, params=[])
     if params[0] == 'pie'
          str += " Pie"
     elsif params[0] == "smoothie"
          str += " Smoothie"
     else
          str
     end
end

Canny.register_modifier('my_modifier', 'my_modifier')

C.assign('name', name)
C.assign('people', people)
C.assign('fruits', fruits)

C.display('example.tpl')


example.tpl

Hello { $name }.

<br />
<br />

{ foreach from=$fruits item=fruit }
     { if $fruit == "Apple" or $fruit == "Cherry" }
          { $fruit|my_modifier:"pie" }
     {else}
          { $fruit|my_modifier:"smoothie" }
     {/if}

     <br />
{/foreach}

{ foreach name=people from=$people item=person }
     { $canny.foreach.people.iterator } { $person.name|upper } lives in { $person.city|upper }.
     <br />
{/foreach}

<br />
<br />

{ include file="example_show_total.tpl" total=$canny.foreach.people.total }


example_show_total.tpl

{ $total } people total.