Cosmo (000006)
XSSFilter could not parse (X)HTML:


<p>Cosmo is a "safe templates" engine that is used by Sputnik but which can also be used for many other purposes.</p>

<p>You can get the trunk copy of Cosmo from <a href="http://sputnik-wiki.googlecode.com/svn/trunk/cosmo.lua">SVN</a>.</p>

<p>Cosmo provides three functions that you would want to use. <br/>
The first one is <code>cosmo.fill</code> which takes two parameters: a string and a table.  The string can be in any format and cosmo only looks for two kind of patterns in it: <code>$var_name</code> and <code>$fn_name[[ a subtemplate ]]</code></p>

<p>Note that in the latter case, <code>[=[...]=]</code>, <code>[==[...]==]</code>, etc. can be used instead of <code>[[...]]</code>.</p>

<p>If Cosmo encounters something that looks like <code>$var_name</code> it will simply look up the value in the template and substitute it.  E.g.:</p>

<p>   template = [==[  $x+$y = $sum  ]==]
   cosmo.fill( template,</p>
<pre><code>           { x   = 1,
             y   = 2,
             sum = "?" } )
</code></pre>
<p>will return</p>

<pre><code> 1+2 = ?  
</code></pre>

<p>If <code>cosmo.fill</code> finds something like <code>$fn_name[=[...]=]</code>, it will look up the <code>fn_name</code> field in the table but will assume this field to be a <strong>function</strong>.  Cosmo will then call this function in a coroutine and resume it until until the function stops yielding.  Each time the function yields, it is expected to yield a value, and this value will be used to fill the template inside <code>[=[...]=]</code>.  The function should yield by calling <code>cosmo.yield</code>.  E.g.:</p>

<pre><code>template = [==[ 
<h1>$list_name</h1>
<ul>
 $do_items[=[<li>$item</li>]=]
</ul>
]==]

cosmo.fill(template,
           { list_name = "My List",
             do_items  = function()
                for i=1,5 do
                   cosmo.yield { item = i }
                end
             end
           }
          )
</code></pre>

<p>Will return</p>

<pre><code><h1>My List</h1>
<ul>
 <li>1</li><li>2</li><li>3</li><li>4</li><li>5</li>
</ul>
</code></pre>

<p>Finally, the third function (i.e. after <code>cosmo.fill</code> and <code>cosmo.yield</code>) is used for the frequent case where we want to include a fill string 0 or 1 times depending on whether a condition holds.  <code>cosmo.cond</code> takes two parameters: a boolean and a table and yields the table if a condition holds.</p>

<p>Thus</p>

<pre><code>cosmo.fill ( "-- $if_warning[=[<b>Beware of $warning!<b>]=] --",
             { if_warning = cosmo.cond(warn_about_alligators,
                                       { warning = "ALLIGATORS" }
                                      )
             }
           )
</code></pre>

<p>Will return "-- <b>Beware of ALLIGATORS!<b> --" if <code>warn_about_alligators</code> is true and "--  --" otherwise.</p>

<h2>Future of Cosmo</h2>

<p>We are planning to add an additional pattern in future, which allow insertion of a Lua table between the function name and the inner table.  The table would then be passed the only parameter to the function:</p>

<pre><code>template = [===[
   $do_warnings{animals={"Alligators", "Hippos"}, threat_level="orange"}[=[
       <b>Beware of $threat!<b>
   ]=]
]===]

f = function(params) 
       for i, animal in ipairs(params.animals} do
          if is_dangerous(animal) and params.threat_level=="orange" then
             yield { threat = animal }
          end
       end
    end

return cosmo.fill(template, {do_warnings = f})
</code></pre>


Powered by Sputnik | XHTML 1.1