The spylambda module is loaded implicitly and allows the definition of
functions based on Spyce scripts. The spylambda module provides the following
methods:
define( args, code, [memoize] ): Returns a function that
accepts the given args and executes the Spyce script defined by the
code parameter. Note that the code is compiled immediately and that
spyce.spyceSyntaxError or spyce.spycePythonError exceptions can be thrown for
invalid code arguments. The optional memoize parameter sets whether
the spyce can or can not be memoized, with the default being false.
Memoizing a function means capturing the result and output and caching them,
keyed on the function parameters. Later, if a function is called again with
the same parameters, the cached information is returned, if it exists, and
the function may not actually be called. Thus, you should only memoize
functions that are truly functional, i.e. they do not have side-effects:
they only return a value and output data to the response object, and their
behaviour depends exclusively on their parameters. If you memoize code that
does have side-effects, those side-effects may not occur on every
invocation.
__call__( args, code, _spyceCache ): This is an alias to the
define function. Because of the special method name, the spylambda module
object can be called as
if it were a function.
This function is not frequently called directly from Spyce code, because
writing the Spyce code argument in a manner that does not conflict with the
Spyce tag delimiters is cumbersome. Rather the Spyce lambda syntax is used and
translated into this function call at compilation time, as in the example
below.
It often useful to use the spylambda module directly from other Spyce modules
that may need to perform significant amounts of output. Rather than calling
print repeatedly, it is more convenient to invoke
a Spyce, as in the example below. Though highly simplified, this example also
shows how Spyce lambdas can be used to easily build a complex rendering
environment.
examples/myPortal.spy
[[.import name=myPortal]]
[[
# this data might be pulled from a database
news = {
'heading': 'News',
'data': [
('<a href="http://www.nytimes.com">nyt</a>',
'today', 'sun rose'),
('<a href="http://www.cnn.com">cnn</a>',
'yesterday', 'sun set'),
('<a href="http://news.google.com">goo</a>',
'long time ago', 'let there be light!'), ] }
weather = {
'heading': 'Weather',
'data': [
('nyc', 'too cold'),
('seattle', 'too wet'),
('tucson', 'too dry'),
('houston', 'too humid'),
('chicago', 'too windy'),
('<a href="http://www.carrier.com/">carrier</a>',
'just right'), ] }
movies = {
'heading': 'Movies',
'data': [
('over-priced theatre', '15 movies'),
("'el cheapo", '3 movies'),
('home', 'blockbuster'), ] }
selection = [ news, movies, weather ]
]]
<html><body>
Dear user [[='XYZ']], <br>
Welcome to your portal. Here are your selected views... <br>
[[-- a module that does a lot of output --]]
[[ myPortal.show( selection ) ]]
</body></html>