# HG changeset patch # User cmlenz # Date 1149337733 0 # Node ID 518a8520a6e10534271dff3af65d264b89a4eee6 # Parent edbbe45da6e2e94aba4caed79dd17188e60b3fc7 Added basic example. diff --git a/examples/basic/common/default_header.html b/examples/basic/common/default_header.html new file mode 100644 --- /dev/null +++ b/examples/basic/common/default_header.html @@ -0,0 +1,4 @@ + + Hello ${hello} + + diff --git a/examples/basic/common/default_header.kid b/examples/basic/common/default_header.kid new file mode 100644 --- /dev/null +++ b/examples/basic/common/default_header.kid @@ -0,0 +1,3 @@ + + Hello ${hello} + diff --git a/examples/basic/common/macros.html b/examples/basic/common/macros.html new file mode 100644 --- /dev/null +++ b/examples/basic/common/macros.html @@ -0,0 +1,12 @@ +
+
reference me, please
+
+ Hello ${name.title()} +
+ + Hello ${select('@name')} + + +
diff --git a/examples/basic/common/macros.kid b/examples/basic/common/macros.kid new file mode 100644 --- /dev/null +++ b/examples/basic/common/macros.kid @@ -0,0 +1,13 @@ +
+
reference me, please
+
+ Hello ${name.title()} +
+ + Hello ${item.get('name')} + + + ${item.findtext('')} + +
diff --git a/examples/basic/kidrun.py b/examples/basic/kidrun.py new file mode 100755 --- /dev/null +++ b/examples/basic/kidrun.py @@ -0,0 +1,47 @@ +from datetime import datetime, timedelta +import os +import sys + +import kid + +def test(): + base_path = os.path.dirname(os.path.abspath(__file__)) + kid.path = kid.TemplatePath([os.path.join(base_path, 'common'), + os.path.join(base_path, 'module')]) + + ctxt = dict(hello='', hey='ZYX', bozz=None, + items=['Number %d' % num for num in range(1, 15)], + prefix='#') + + start = datetime.now() + template = kid.Template(file='test.kid', **ctxt) + print ' --> parse stage: ', datetime.now() - start + + times = [] + for i in range(100): + start = datetime.now() + for output in template.generate(): + if i == 0: + sys.stdout.write(output) + if i == 0: + print + else: + sys.stdout.write('.') + sys.stdout.flush() + times.append(datetime.now() - start) + print + + total_ms = sum([t.seconds * 1000 + t.microseconds for t in times]) + print ' --> render stage: %s (avg), %s (min), %s (max):' % ( + timedelta(microseconds=total_ms / len(times)), + timedelta(microseconds=min([t.seconds * 1000 + t.microseconds for t in times])), + timedelta(microseconds=max([t.seconds * 1000 + t.microseconds for t in times]))) + +if __name__ == '__main__': + if '-p' in sys.argv: + import profile, pstats + profile.run('test()', '.tmpl_prof') + stats = pstats.Stats('.tmpl_prof') + stats.strip_dirs().sort_stats('time').print_stats(10) + else: + test() diff --git a/examples/basic/module/test.html b/examples/basic/module/test.html new file mode 100644 --- /dev/null +++ b/examples/basic/module/test.html @@ -0,0 +1,23 @@ + + + + + + +
    +
  • Item $prefix${item.split()[-1]}
  • + XYZ ${hey} +
+ ${macro1()} ${macro1()} ${macro1()} + ${macro2('john')} + ${macro2('kate', classname='collapsed')} +
Replace me
+ + + Hello Silicon + + diff --git a/examples/basic/module/test.kid b/examples/basic/module/test.kid new file mode 100644 --- /dev/null +++ b/examples/basic/module/test.kid @@ -0,0 +1,19 @@ + + + +
    +
  • Item $prefix${item.split()[-1]}
  • + XYZ ${hey} +
+ ${macro1()} ${macro1()} ${macro1()} + ${macro2('john')} + ${macro2('kate', classname='collapsed')} +
Replace me
+ + + Hello Silicon + + diff --git a/examples/basic/run.py b/examples/basic/run.py new file mode 100755 --- /dev/null +++ b/examples/basic/run.py @@ -0,0 +1,51 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +from datetime import datetime, timedelta +import os +import sys + +from markup.template import Context, TemplateLoader + +def test(): + base_path = os.path.dirname(os.path.abspath(__file__)) + loader = TemplateLoader([os.path.join(base_path, 'common'), + os.path.join(base_path, 'module')], + auto_reload=True) + + start = datetime.now() + tmpl = loader.load('test.html') + print ' --> parse stage: ', datetime.now() - start + + ctxt = Context(hello='', skin='default', hey='ZYX', bozz=None, + items=['Number %d' % num for num in range(1, 15)], + prefix='#') + + print tmpl.generate(ctxt).render(method='html') + + times = [] + for i in range(100): + start = datetime.now() + list(tmpl.generate(ctxt)) + sys.stdout.write('.') + sys.stdout.flush() + times.append(datetime.now() - start) + print + + total_ms = sum([t.seconds * 1000 + t.microseconds for t in times]) + print ' --> render stage: %s (avg), %s (min), %s (max)' % ( + timedelta(microseconds=total_ms / len(times)), + timedelta(microseconds=min([t.seconds * 1000 + t.microseconds for t in times])), + timedelta(microseconds=max([t.seconds * 1000 + t.microseconds for t in times]))) + +if __name__ == '__main__': + if '-p' in sys.argv: + import hotshot, hotshot.stats + prof = hotshot.Profile("template.prof") + benchtime = prof.runcall(test) + stats = hotshot.stats.load("template.prof") + stats.strip_dirs() + stats.sort_stats('time', 'calls') + stats.print_stats() + else: + test()