{ "info": { "author": "David Bau", "author_email": "david.bau@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "License :: OSI Approved :: BSD License", "Topic :: Software Development :: Libraries", "Topic :: Text Processing :: General" ], "description": "# Python Templating with @templet\n\nHere is an elegant lightweight python templating module that supports inline python, speedy compilation, subtemplates, and subclassing, all in a simple decorator module implemented in about 125 lines of python.\n\n## What's New\n\nTemplet was created by David Bau, and modifications for version 4 are by Kriszti\u00e1n Fekete. Version 4 adds:\n\n * Python 3 support.\n * Proper unit tests, verifying python2, python3, and pypy support.\n * Support only unicode strings (no bytestrings).\n * Removed special case for empty-first-newline.\n * Cleaned up code for readability.\n\n## Usage\n\nTo use templet, just annotate a python function with @templet, and then put the template text where the docstring would normally be. Leave the function body empty, and efficient code to concatenate the contents will be created.\n\n```\n from templet import templet\n \n @templet\n def myTemplate(animal, body):\n \"the $animal jumped over the $body.\"\n \n print(myTemplate('cow', 'moon'))\n```\n\nThis is turned into something like this:\n\n```\n def myTemplate(animal, body):\n out = []\n out.append(\"the \")\n out.append(str(animal))\n out.append(\" jumped over the \")\n out.append(str(body))\n out.append(\".\")\n return ''.join(out)\n```\n\nThere are just six constructs that are supported, all starting with $:\n\n| syntax | meaning |\n|--------|---------|\n| `$myvar` | inserts the value of the variable `myvar` |\n| `${...}` | evaluates the expression and inserts the result |\n| `${[...]}` | runs a list comprehension and concatenates the results |\n| `${{...}}` | executes enclosed code; use `out.append(text)` to insert text |\n| `$$` | an escape for a single `$` |\n| `$` | (at the end of the line) is a line continuation |\n\nAll ordinary uses of `$` in the template need to be escaped by doubling the `$$` - with the exception of (as mentioned above) `$.`, `$(`, `$/`, `$'`, and `$\"`.\n\nIn the actual generated code, line numbers are aligned exactly so that both syntax errors and runtime errors in exception traces are reported on the correct line of the template file in which the code appears.\n\n## Philosophy\n\nThe philosophy behind templet is to introduce only the concepts necessary to simplify the construction of long strings in python; and then to encourage all other logic to be expressed using ordinary python.\n\nA @templet function can do everything that you can do with any function that returns a string: it can be called recursively; it can have variable or keyword arguments; it can be a member of a package or a method of a class; and it can access global imports or invoke other packages. As a result, although the construct is extremely simple, it brings all the power of python to templates, and the @templet idea scales very well.\n\nBeyond simple interpolation, templet does not invent any new syntax for data formatting. If you want to format a floating-point number, you can write `${\"%2.3f\" % num}`; if you want to escape HTML sequences, just write `${cgi.escape(message)}`. Not as brief as a specialized syntax, but easy to remember, brief enough, and readable to any python programmer.\n\nSimilarly, templet does not invent any new control flow or looping structures. To loop a template, you need to use a python loop or list comprension and call the subtemplate as a function:\n\n```\n @templet\n def doc_template(table):\n \"\"\"\\\n
\n