Class Web::Narflates
In: lib/web/template.rb
Parent: Object

Purpose

The purpose of the templater is to simplify testing cgi’s, bya seperating the constants of a page from the dynamic things one would want to test.

To define a template define a file in the directory hierarchy described in the README. For example, create the following file in ’/myappdir/html/templates/mytemplate.html‘

    <html>
      <body>
        {$myvar}
        <narf:foreach from=alist item=i>
          {$i.x}, {$i.x}<br>
        </narf:foreach>
      </body>
    </html>

Create a script such as the following in ’/myappdir/html/templates/myapp.rb‘

    #!/usr/bin/narf

    class SimpleClass

Narflates also provide for form handling and submission. They are designed to work with a dispatching framework like Web::SimpleDispatcher, so see the (future) examples there.

The form tags correspond closely to html form tags:

    Web.print_template "mytemplate.html",
                       "field1" => "val1",
                       "field2" => "val2",
                       "field3" => "val3",
                       "field4" => "3",
                       "values" => [{ "name" => "one", "value" => "1"},
                                    { "name" => "two", "value" => "2"},
                                    { "name' => "three", "value" => "3"}]

    <narf:input type="text" name="field1">
    <narf:input type="hidden" name="field2">
    <narf:textarea name="field3">
    <narf:select name="field4" values="values">

This will produce the output:

    <input type="text" name="field1" value="val1">
    <input type="hidden" name="field2" value="val2">
    <textarea name="field3">
    val3
    </textarea>
    <select name="field3">
            <option value="1">one</option>
            <option value="2">two</option>
            <option value="3" selected>three</option>
    </select>

You can also wrap your forms in a <narf:form> tag, which will add a hidden field with the value __submitted = true. This can be used to give frameworks a not-too-subtle hint about where in the form processing cycle they are.

Methods
template_file    template_include_path   
Attributes
templates  [R] 
Public Class methods
template_include_path()

File.dirname(FILE)+"/../"

template_file( template )