1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
|
==============
Kajiki Runtime
==============
.. module:: kajiki
:synopsis: Kajiki Runtime APIs
It's sometimes good to have a mental model of the Python code that Kajiki
creates in order to generate your templates. This document uses several
examples taken from the text templating language to illustrate the semantics of
Kajiki templates. If in doubt, you can always view the Python text generated for
a template by examining the py_text attribute of the generated Template class.
.. automethod:: kajiki.xml_template.XMLTemplate
.. autoclass:: kajiki.ir.TemplateNode
:members:
.. automethod:: kajiki.template.Template
.. autoclass:: kajiki.template._Template
:members:
.. autoclass:: kajiki.xml_template._Compiler
:members:
.. autoclass:: kajiki.xml_template._Parser
:members:
.. autoclass:: kajiki.xml_template._DomTransformer
:members:
Basic Expressions
=========================
Let's start with a hello world template:
.. code-block:: none
Hello, World!
This converts to the equivalent Python::
@kajiki.expose
def __call__():
yield 'Hello, World!\n'
Slightly more verbose "hello_name.txt":
.. code-block:: none
Hello, $name!
This converts to the equivalent Python::
@kajiki.expose
def __call__():
yield 'Hello, '
yield name
yield '!\n'
By default, the $-syntax picks up any identifiers following it, as well as any
periods. If you want something more explicit, use the extended expression form
as in "hello_arithmetic.txt":
.. code-block:: none
Hello, 2 + 2 is ${2+2}!
This converts to::
@kajiki.expose
def __call__():
yield 'Hello, 2 + 2 is '
yield 2+2
yield '!'
If you wish to include a literal $, simply prefix it with a backslash.
Control Flow
============
Kajiki provides several tags that affect the rendering of a template. The
following template "control_flow.txt" illustrates:
.. code-block:: none
A{%for i in range(5)%}
{%if i < 2%}Low{%elif i < 4%}Mid{%else%}High{%end%}$i
{%switch i % 2%}
{%case 0%}
even
{%default%}
odd
{%end%}{%end%}{%end%}
This yields the following Python::
@kajiki.expose
def __call__():
yield 'A\n' # from the {%for... line
for i in range(10):
yield '\n ' # from the newline and initial indent of next line
if i < 2:
yield 'Low'
elif i < 4:
yield 'Mid'
else:
yield 'High'
yield i
yield '\n ' # from the {%if... newline and next indent
local.__kj__.push_switch(i%2)
# whitespace after {%switch is always stripped
if local.__kj__.case(0):
yield '\n even\n '
else:
yield '\n odd\n '
local.__kj__.pop_switch()
Which would in turn generate the following text:
.. code-block:: none
A
Low0
even
Low1
odd
Mid2
even
Mid3
odd
High4
even
If you want to strip whitespace before or after a tag, just replace
``{%`` with ``{%-`` (for stripping leading whitespace) or ``%}`` with
``-%}`` (for stripping trailing whitespace). If you would like to remove
newlines, just end a line with a backslash. Here is the equivalent template
with whitespace removed, "control_flow_ws.txt":
.. code-block:: none
A{%-for i in range(5) -%}\
{%-if i < 2%}Low{%elif i < 4%}Mid{%else%}High{%end%}$i
{%-switch i % 2%}\
{%-case 0%}\
even
{%-default%}\
odd
{%-end%}\
{%-end%}\
{%-end%}\
This would generate the following Python::
@kajiki.expose
def __call__():
yield 'A'
for i in range(10):
if i < 2:
yield 'Low'
elif i < 4:
yield 'Mid'
else:
yield 'High'
yield i
yield '\n'
local.__kj__.push_switch(i % 2)
if local.__kj__.case(0):
yield 'even\n'
else:
yield 'odd\n'
local.__kj__.pop_switch()
Which would generate the following text:
.. code-block:: none
ALow0
even
Low1
odd
Mid2
even
Mid3
odd
High4
even
which is probably closer to what you wanted. There is also a shorthand syntax
that allows for line-oriented control flow as seen in
"control_flow_ws_short.txt":
.. code-block:: none
A\
%for i in range(5)
%if i < 2
Low\
%elif i < 4
Mid\
%else
High\
{%-end%}$i
%switch i % 2
%case 0
even
%default
odd
%end
%end
%end
This syntax yields exactly the same results as "control_flow_ws.txt" above.
Python Blocks
==============
You can insert literal Python code into your template using the following syntax
in "simple_py_block.txt":
.. code-block:: none
{%py%}\
yield 'Prefix'
{%end%}\
Body
or alternatively:
.. code-block:: none
%py
yield 'Prefix'
%end
Body
or even more succinctly:
.. code-block:: none
%py yield 'Prefix'
Body
all of which will generate the following Python::
def __call__():
yield 'Prefix'
yield 'Body'
Note in particular that the Python block can have any indentation, as long as it
is consistent (the amount of leading whitespace in the first non-empty line of
the block is stripped from all lines within the block). You can insert
module-level Python (imports, etc.) by using the %py% directive (or {%py%%} as in
"module_py_block.txt":
.. code-block:: none
%py%
import sys
import re
%end
Hello
%py% import os
%end
This yields the following Python::
import sys
import re
import os
@kajiki.expose
def __call__():
yield 'Hello'
Functions and Imports
====================================
Kajiki provides for code reuse via the %def and %import directives. First, let's
see %def in action in "simple_function.txt":
.. code-block:: none
%def evenness(n)
%if n % 2 == 0
even\
%else
odd\
%end
%end
%for i in range(5)
$i is ${evenness(i)}
%end
This compiles to the following Python::
@kajiki.expose
def evenness(n):
if n % 2:
yield 'even'
else:
yield 'odd'
@kajiki.expose
def __call__():
for i in range(5):
yield i
yield ' is '
yield evenness(i)
The %import directive allows you to package up your functions for reuse in
another template file (or even in a Python package). For instance, consider the
following file "import_test.txt":
.. code-block:: none
%import "simple_function.txt" as simple_function
%for i in range(5)
$i is ${simple_function.evenness(i)}
%end
This would then compile to the following Python::
@kajiki.expose
def __call__():
simple_function = local.__kj__.import_("simple_function.txt")
for i in range(5):
yield i
yield ' is '
yield simple_function.evenness(i)
Note that when using the %import directive, any "body" in the imported template
is ignored and only functions are imported. If you actually wanted to insert the
body of the imported template, you would simply call the imported template as a
function itself (e.g. ${simple_function()}).
Sometimes it is convenient to pass the contents of a tag to a function. In this
case, you can use the %call directive as shown in "call.txt":
.. code-block:: none
%def quote(caller, speaker)
%for i in range(5)
Quoth $speaker, "${caller(i)}."
%end
%end
%call(n) quote('the raven')
Nevermore $n\
%end
This results in the following Python::
@kajiki.expose
def quote(caller, speaker):
for i in range(5):
yield 'Quoth '
yield speaker
yield ', "'
yield caller(i)
yield '."'
@kajiki.expose
def __call__():
@kajiki.expose
def _fpt_lambda(n):
yield 'Nevermore '
yield n
yield quote(_fpt_lambda, 'the raven')
del _fpt_lambda
Which in turn yields the following output:
.. code-block:: none
Quoth the raven, "Nevermore 0."
Quoth the raven, "Nevermore 1."
Quoth the raven, "Nevermore 2."
Quoth the raven, "Nevermore 3."
Quoth the raven, "Nevermore 4."
Includes
===============
Sometimes you just want to pull the text of another template into your template
verbatim. For this, you use the %include directive as in "include_example.txt":
.. code-block:: none
This is my story:
%include "call.txt"
Isn't it good?
which yields the following Python::
@kajiki.expose
def __call__():
yield 'This is my story:\n'
yield _fpt.import("simple_function.txt")()
yield 'Isn't it good?\n'
Which of course yields:
.. code-block:: none
This is my story:
Quoth the raven, "Nevermore 0."
Quoth the raven, "Nevermore 1."
Quoth the raven, "Nevermore 2."
Quoth the raven, "Nevermore 3."
Quoth the raven, "Nevermore 4."
Isn't it good?
Inheritance
==============
Kajiki supports a concept of inheritance whereby child templates can extend
parent templates, replacing their methods and "blocks" (to be defined below).
For instance, consider the following template "parent.txt":
.. code-block:: none
%def greet(name)
Hello, $name!\
%end
%def sign(name)
Sincerely,
$name\
%end
${greet(to)}
%block body
It was good seeing you last Friday. Thanks for the gift!
%end
${sign(from)}
This would generate the following Python::
@kajiki.expose
def greet(name):
yield 'Hello, '
yield name
yield '!'
@kajiki.expose
def sign(name):
yield 'Sincerely,\n'
yield name
@kajiki.expose
def _fpt_block_body():
yield 'It was good seeing you last Friday! Thanks for the gift!\n'
@kajiki.expose
def __call__():
yield greet(to)
yield '\n\n'
yield self._fpt_block_body()
yield '\n\n'
yield sign(from)
Here is the corresponding "child.txt":
.. code-block:: none
%extends "parent.txt"
%def greet(name)
Dear $name:\
%end
%block body
${parent_block()}\\
And don't forget you owe me money!
%end
This would then yield the following Python::
@kajiki.expose
def greet(name):
yield 'Dear '
yield name
yield ':'
@kajiki.expose
def _fpt_block_body():
yield parent._fpt_block_body()
yield '\n\n'
yield 'And don\'t forget you owe me money!\n'
@kajiki.expose
def __call__():
yield local.__kj__.extend(local.__kj__.import_('parent.txt')).__call__()
The final text would be (assuming context had to='Mark' and from='Rick':
.. code-block:: none
Dear Mark:
It was good seeing you last Friday! Thanks for the gift!
And don't forget you owe me money!
Sincerely,
Rick
|