Genshi render template




















Changed in version 0. Deprecated since version 0. See the Genshi documentation for more filters you can use. Initialize a Flask application for use with this extension. Useful for the factory pattern but not needed if you passed your application to the Genshi constructor. A genshi. TemplateLoader that loads templates from the same places as Flask. To add a new attribute, use the operator, where the right hand value is a sequence of name, value tuples which includes Attrs instances :.

If the attributes already contain an attribute with a given name, the value of that attribute is replaced:. Return the value of the attribute with the specified name, or the value of the default parameter if no such attribute is found. The returned event is a TEXT event, the data is the value of all attributes joined together. This class helps create and test these strings. The Namespace object can than be used to generate QName objects with that namespace:.

The same works using item access notation, which is useful for element or attribute names that are not valid Python identifiers:. A Namespace object can also be used to test whether a specific QName belongs to that namespace using the in operator:.

The namespace URI can be obtained through the additional namespace attribute, while the local name can be accessed through the localname attribute. Docs » genshi. In text templates although only those using the new syntax introduced in Genshi 0. Code blocks can import modules, define classes and functions, and basically do anything you can do in normal Python code.

What code blocks can not do is to produce content that is emitted directly tp the generated output. Using the print statement will print to the standard output stream, just as it does for other Python code in your application.

The support for Python code blocks in templates is not supposed to encourage mixing application code into templates, which is generally considered bad design. But please note that disallowing code blocks in templates does not turn Genshi into a sandboxable template engine; there are sufficient ways to do harm even using plain expressions. By default, Genshi raises an UndefinedError if a template expression attempts to access a variable that is not defined:. In that case, accessing undefined variables returns an Undefined object, meaning that the expression does not fail immediately.

See below for details. To check for existence of attributes on an object, or keys in a dictionary, use the hasattr , getattr or get functions, or the in operator, just as you would in regular Python code:. Lenient error handling was the default in Genshi prior to version 0. Strict mode was introduced in version 0. The reason for this change was that the lenient error handling was masking actual errors in templates, thereby also making it harder to debug some problems.

If you instruct Genshi to use the lenient variable lookup mode, it allows you to access variables that are not defined, without raising an UndefinedError. You will however get an exception if you try to call an undefined variable, or do anything else with it, such as accessing its attributes:.

If you need to know whether a variable is defined, you can check its type against the Undefined class, for example in a conditional directive:. For example, you could use lenient error handling in a production environment, while also logging a warning when an undefined variable is referenced. See the API documentation of the genshi.

The following functions and types are available by default in template code, in addition to the standard built-ins that are available to all Python code.



0コメント

  • 1000 / 1000