O projekcie / Python / SimpleTAL / Przykłady
Każdy przykład zaczyna się od części napisanej w XML po której następuje odpowiadający jej skrypt w Pythonie. Obecnie jest tu zaprezentowana jedynie część funkcjonalności oferowanej przez SimpleTAL.
Prosty przykład
structure.html
<html>
<h1 tal:content="title">Tu będzie wstawiony tytuł strony.</h1>
</html>
basic.py
import sys
from simpletal import simpleTAL, simpleTALES
# utwórz kontekst używany przez instancję szablonu
context = simpleTALES.Context()
context.addGlobal ("title", "Witaj!")
templateFile = open ("basic.html", 'r')
template = simpleTAL.compileHTMLTemplate (templateFile)
templateFile.close()
template.expand (context, sys.stdout)
Dane strukturalne
structure.html
<html>
<body>
<h1 tal:content="title">Tytuł</h1>
<div tal:repeat="chapters doc">
<h2 tal:content="chapters/heading">Nagłówek</h2>
<p tal:content="structure chapters/text">Tekst</p>
</div>
</body>
</html>
structure.py
from simpletal import simpleTAL, simpleTALES
import sys
context = simpleTALES.Context()
context.addGlobal ("title", "Witaj!")
context.addGlobal ("author", "Autor")
# lista zawierająca słownik
chapters =
[ {"heading": "Wprowadzenie", "text": "Jakiś <b>tekst</b>"}
, {"heading": "Details", "text": "Zauważ, że tagi są zachowywane."}
]
advancedText = 'Zagnieżdżony szablon: napisane przez <b tal:replace="author">Ja</b>'
chapters.append
( {"heading": "Zagnieżdzony"
, "text": simpleTAL.compileHTMLTemplate (advancedText)}
)
context.addGlobal ("doc", chapters)
templateFile = open ("structure.html", 'r')
template = simpleTAL.compileHTMLTemplate (templateFile)
templateFile.close()
template.expand (context, sys.stdout)
Makra
macro.html
<html>
<body>
<p metal:define-macro="notice">
To makro używa systemu: <b metal:define-slot="source">System</b>.
</p>
</body>
</html>
page.html
<html>
<body>
<h1 tal:content="title">Tytuł</h1>
<div metal:use-macro="sitemacros/macros/notice">
<i metal:fill-slot="source">SimpleTAL!</i>
</div>
</body>
</html>
page.py
from simpletal import simpleTAL, simpleTALES
import sys
context = simpleTALES.Context()
context.addGlobal ("title", "Prosty przykład METAL")
templateFile = open ("macro.html", 'r')
macros = simpleTAL.compileHTMLTemplate (templateFile)
templateFile.close()
context.addGlobal ("sitemacros", macros)
templateFile = open ("page.html", 'r')
page = simpleTAL.compileHTMLTemplate (templateFile)
templateFile.close()
page.expand (context, sys.stdout)
QuasiGame

