|
|
Documentation - Modules
3.8.10. Include
Many websites carry a theme across their various pages, which is often
achieved by including a common header or footer. This is best done with a
parent template from the spy:parent tag,
but you can also do this with the include module for backwards compatibility
with Spyce 1.x.
Another option to consider for repeating a common task is
a custom
active tag.
The include module can also pretty print Spyce code or include the contents of
anything in your filesystem.
The example below (taken from this documentation file), uses a common header
template only requiring two context variables to change the title and the
highlighted link:
[[.import name=include]]
[[include.spyce('inc/head.spi',
{'pagename': 'Documentation',
'page': 'manual.html'}) ]]
|
In head.spi, we use this information to set the title:
[[.import name=include]]
<title>[[=include.context['pagename'] ]]</title>
|
By convention, included files are given the extension .spi.
Below we contrast the difference between static and dynamic includes. A
dynamic include is included on each request; a static include is inserted at
compile time. A static include runs in the same context, while a dynamic
include has a separate context.
examples/include.spy
|
[[.import name=include]]
<html><body>
main file<br>
<hr>
[[
context = {'foo': 'old value'}
result=include.spyce('include.spi', context)
]]
<hr>
main file again<br>
context: [[=context]]<br>
return value: [[=result]]<br>
</body></html>
|
Run this code
|
examples/include.spi
|
begin include<br>
context: [[=include.context ]]<br>
from: [[=request.stack()[-2] ]]<br>
foo was [[=include.vars.foo]]<br>
setting foo to 'new value' [[include.vars.foo = 'new value']]<br>
returing 'retval'<br>
end include<br>
[[ return 'retval' ]]
|
examples/includestatic.spy
|
<html><body>
[[x=1]]
main file<br>
x=[[=x]]<br>
<hr>
[[.include file="includestatic.spi"]]
<hr>
main file again<br>
x=[[=x]]
</body></html>
|
Run this code
|
examples/includestatic.spi
|
begin included file<br>
changing value of x<br>
[[x=2]]
end included file<br>
|
|