Tornado documentation is very scarce, but this is a superficial phenomenon, in fact, the Qian Kun are in the source file, the source file notes have very document information, it is worth careful study. Translate the template system content for future use. Here, the translation is mainly used, and it is not translated word by word.
A simple template system that compiles templates to Python code.
A simple template system that compiles a template into PythonCode
Basic usage looks like ::
The basic usage is as follows:
T = template. template ("<HTML >{{ myvalue }}
Print T. Generate (myvalue = "XXX ")
Loader is a class that loads templates from a root directory and caches
The compiled templates ::
The loader is a class that loads template files from the root directory and compiles cache templates.
Loader = template. loader ("/home/btaylor ")
Print loader. Load ("test.html"). Generate (myvalue = "XXX ")
We compile all templates to raw python. Error-reporting is currently... uh,
Interesting. syntax for the templates ::
We compile all the templates as Python code (an error is reported at present .... Note: It may be written during development)
### Base.html
<HTML>
<Head>
<Title >{% block title %} default title {% end %} </title>
</Head>
<Body>
<Ul>
{% For student in students %}
{% Block student %}
<Li >{{ escape (student. Name) }}</LI>
{% End %}
{% End %}
</Ul>
</Body>
</Html>
### Bold.html
{% Extends "base.html" %}
{% Block title %} a bolder title {% end %}
{% Block student %}
<Li> <span style = "bold" >{{ escape (student. Name) }}</span> </LI>
{% End %}
Unlike most other template systems, we do not put any restrictions on
Expressions you can include in your statements. If and for blocks get
Translated exactly into python, you can do complex expressions like ::
Unlike other template systems, we do not impose any restrictions on the expressions you embed in the Template Name. The IF and for blocks fully support the python syntax. You can use the complete syntax as follows:
{% For student in [p for P in people if p. Student and P. age> 23] %}
<Li >{{ escape (student. Name) }}</LI>
{% End %}
Translating directly to Python means you can apply functions to expressions
Easily, like the escape () function in the examples above. You can pass
Functions in to your template just like any other variable ::
Convert directlyPythonCode means that you can easily call a method in an expression. For example, if you call the escape function above, you can pass the function to the template like any other variable.
### Python code
Def add (x, y ):
Return x + y
Template.exe cute (add = add)
### The Template
{Add (1, 2 )}}
We provide the functions escape (), url_escape (), json_encode (), and squeeze ()
To all templates by default.
By default, each template providesEscape (), url_escape (), json_encode (), and squeeze ()These functions
Typical applications do not create 'template' or 'loader 'instances
Hand, but instead use the 'render' and 'render _ string' methods
'Tornado. Web. requesthandler', which load templates automatically based
On the ''template _ path''' application 'setting.
DefaultApplicationNot created manuallyTemplateAndLoaderBy calling the render and render_string methods in the 'tornado. Web. requesthandler' instance.
These methods are used to set 'template _ path' in the setting field of the application to automatically load the template in the directory.
Syntax reference
Syntax
----------------
Template expressions are surrounded by double curly braces :''{{...}}''.
The contents may be any Python expression, which will be escaped according
To the current autoescape setting and inserted into the output. Other
Template ctictives use ''{%}''. These tags may be escaped ''{{! ''
And ''{%! ''If you need to include a literal' {''or'' {% ''in the output.
The template expression is enclosed in two braces:''{{... }}''.
The content can be any Python expression. The expression uses the current autoescape to set the escape and insert it into the output.
Other template commands use '{% }'. These tags are converted ''{{! ''And'' {%! '', If you want to insert'' {''or'' {% ''to the output.
''{% Apply * function * %}... {% end % }''
Applies a function to the output of all template code between ''appl''
And ''end ''::
Apply the template output between the tags as a parameter to a method as follows:
{% Apply linkify % }{{ name }}said: {message }}{% end %}
''' {% Autoescape * function * % }''
Sets the autoescape mode for the current file. This does not affect
Other files, even those referenced by ''{% include %}''. Note that
Autoescaping can also be configured globally, at the 'application'
Or 'loader '.::
This label is used to set the automatic escape mode of the current file. This setting is invalid for other files, which are the templates with the current file inserted instantly.
Automatic escape can also be set globally in application and loader.
{% Autoescape xhtml_escape %}
{% Autoescape none %}
''{% Block * name * %}... {% end % }''
Indicates a named, replaceable block for use with ''{% extends % }''.
Blocks in the parent template will be replaced with the contents
The same-named block in a child template .::
It indicates a replaceable block that can be named and used together with ''{% extends %. These blocks in the parent template will be replaced by blocks with the same name in the template.
<! -- Base.html -->
<Title >{% block title %} default title {% end %} </title>
<! -- Mypage.html -->
{% Extends "base.html" %}
{% Block title %} My page title {% end %}
''' {% Comment... % }''
A comment which will be removed from the template output. Note that
There is no ''{% end %}'' tag; the comment goes from the word '''comment''
To the closing ''' %} ''' tag.
Comment block, not output. No idea'{% End % }'Tag;
''' {% Extends * filename * % }''
Inherit from another template. templates that use ''extends ''should
Contain one or more ''block'' tags to replace content from the parent
Template. Anything in the Child template not contained in a ''block''
Tag will be ignored. For an example, see the ''{% block %}'' tag.
Inherit from other templates. UseExtendThe s tag template must contain one or more block tags to replace blocks of the same name in the parent template. Any content in the sub-template that is not in the block will be ignored. For example, see the section of the ''{% block %}'' tag.
''{% For * Var * In * expr * %}... {% end % }''
Same as the python ''for'' statement.
ForLoop tag, equivalentPythonInForExpression
''{% From * x * import * y * % }''
Same as the python ''import ''statement.
ImportTag, equivalentImportExpression
''{% If * condition * %}... {% Elif * condition * %}... {% else %}... {% end % }''
Conditional statement-outputs the first section whose condition is
True. (The ''elif'' and ''else' sections are optional)
IfConditional expression label, equivalentPythonOfIf... Else... Expression
''' {% Import * module * % }''
Same as the python ''import ''statement.
ImportTag syntax
''' {% Include * filename * % }''
Includes another template file. The specified ded file can see all the local
Variables as if it were copied directly to the point of the ''include''
Directive (the ''{% autoescape %}'' direve VE is an exception ).
Alternately, ''{% module template (filename, ** kwargs) %}'' may be used
To include another template with an isolated namespace.
Reference another template file. The introduced file can access all the templates that introduce it.LocalsVariable, equivalent to directCopyThe content of the introduced template file. An example is provided in the autoescape section.
In addition, ''{% module template (filename, ** kwargs) %} ''can be used to introduce a template file in an independent namespace.
''' {% Module * expr * % }''
Renders '~ Tornado. Web. uimodule '. The output of the ''uimodule ''is
Not escaped ::
InsertUIModule label,UIThe module output is not escaped.
{% Module template ("foo.html", Arg = 42) %}
''' {% Raw * expr * % }''
Outputs the result of the given expression without autoescaping.
Outputs the value of an expression without escaping
''{% Set * x * = * y * % }''
Sets a local variable.
Create a local variable
''{% Try %}... {% failed T %}... {% finally %}... {% end % }''
Same as the python ''try'' statement.
AndPythonOfTry... Else T...Consistent block features
''{% While * condition * %}... {% end % }''
Same as the python ''while ''statement.
AndPythonLiWhileStatement consistency