A method for real-time parsing template engine
Through the real-time parsing template engine method, YAML and jinja2 template syntax are used to establish a tree structure and perform variable substitution, which solves the problem of jinja2's insufficient ability to describe complex configurations and improves the readability and maintainability of the template.
Patent Information
- Application Number
- CN202110928172.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-08-13
- Publication Date
- 2024-10-11
- Estimated Expiration
- 2041-08-13
AI Technical Summary
The existing template engine jinja2 lacks the ability to describe complex configurations, resulting in reduced template readability and unfavorable for later maintenance.
Adopting real-time parsing template engine method, using YAML as the description language, based on jinja2 template syntax, by separating variable names and values, establishing a tree structure, and using a cyclic increment method to replace and update variables to ensure the legality and scope of each node Consistency, use full or relative paths to reference variables, use the '[update]' and '[throw]' flags when dealing with variables with the same name in the same scope.
It effectively solves the problem of describing complex configurations and improves the readability and maintainability of the template.
Abstract
Description
Technical Field
[0001] The invention relates to a template engine, and in particular to a method for real-time parsing of a template engine. Background Art
[0002] Template engines are widely used in a wide range of fields, such as jinja2, ejs, etc., which are used in HTML templating, and some are used in environment and parameter configuration, such as ansile uses jinja2 to parse environment and parameter configuration, but jinja2 only supports "lazy" loading and parsing, and cannot describe slightly more complex configurations well, which greatly reduces the readability of the template and is not conducive to later maintenance. The present invention can well solve the description of complex configurations by performing template parsing in real time. Summary of the invention
[0003] 1. Technical problems to be solved:
[0004] In the existing template engine application field, jinja2 cannot describe slightly complex configurations well, which greatly reduces the readability of the template and is not conducive to subsequent maintenance.
[0005] 2. Technical solution:
[0006] In order to solve the above problems, the present invention provides a method for real-time parsing template engine, which uses YAML as a description language and jinja2 template syntax as a basis, and includes the following steps: Step S01: Separate variable names and variable values: in a left-to-right, top-to-bottom order, with a colon as a separator, the variable name on the left and the value on the right, and indentation as a scope separator. The scopes with the same indentation are the same, thereby establishing a "tree" structure, the root of the tree is the top-level scope, there are multiple tree roots, and the "trees" are stored in sequence from top to bottom according to the parsing order, and each "tree" is stored in sequence according to the parsing order. The order of analysis is stored from top to bottom, and the nodes of each "tree" are stored from left to right in the order of analysis. If there is any illegal or non-compliant one, the analysis is stopped and an exception is thrown; Step S02: variable replacement and update: based on the "tree" structure obtained in step S01, the variables are replaced and updated in a cyclic increment manner, starting from the first node of the first tree, from left to right, from top to bottom, and the already parsed and defined ones are replaced. If an undefined one appears during the replacement, the replacement is stopped and an exception is thrown, until the last node of the last tree is fully parsed.
[0007] In step S02, if the node has multiple nodes at the same level and has child nodes, the child nodes are parsed first.
[0008] During the parsing process of step S02, each node can clearly determine its own location and uniquely determine whether a variable is defined. If a variable is defined after a reference, the variable is considered to not exist and an exception is thrown. The variable is referenced using a full path or a relative path.
[0009] During the parsing process of step S02, if there are variables with the same name and the same scope, the labels "[update]" and "[throw]" are used to indicate update and throw exception respectively.
[0010] 3. Beneficial effects:
[0011] The present invention can well solve the description of complex configurations by performing template parsing in real time. DETAILED DESCRIPTION
[0012] The present invention is described in detail below with reference to the embodiments.
[0013] A method for real-time parsing template engine, using YAML as a description language and jinja2 template syntax as a basis, comprising the following steps: Step S01: Separating variable names and variable values: in a left-to-right, top-to-bottom order, using a colon ":" as a separator, with the variable name on the left and the value on the right, using the indentation "TAB key" as a scope separator, and the scopes of the same indentation are the same, thereby establishing a "tree" structure, with the root position of the tree being the top-level scope, with multiple tree roots, and "trees" are stored in sequence from top to bottom in accordance with the parsing order, and each "tree" is stored in accordance with the parsing order. The nodes of each "tree" are stored in order from top to bottom, and the nodes of each "tree" are stored in order from left to right in the order of parsing. If there are illegal or non-compliant ones, the parsing will be stopped and an exception will be thrown; Step S02: variable replacement and update: based on the "tree" structure obtained in step S01, the variables are replaced and updated in a cyclic increment manner, starting from the first node of the first tree, from left to right, from top to bottom, and the already parsed and defined ones are used for replacement. If undefined ones appear during replacement, the replacement will be stopped and an exception will be thrown, until the last node of the last tree is fully parsed.
[0014] The present invention solves the problem of describing complex configurations by establishing a "tree".
[0015] In one embodiment, in step S02, if a node has multiple sibling nodes and child nodes, the child nodes are parsed first.
[0016] In one embodiment, during the parsing process of step S02, each node can clearly determine its own position, such as "A1.B2.C3", it can be known that the "C3" node is the third child node of the parent node, "B2" is the second child node of the parent node, and so on, the position of "C3" in the entire tree can be determined, and then it can be uniquely determined whether the variable is defined. If the variable is defined after a reference, then the variable is considered to not exist and an exception is thrown. Reference variables can use a complete path or a relative path, such as "..C3" is a relative path, and ".A1.B2.C3" is an absolute path, and "." represents the root node.
[0017] In one embodiment, during the parsing process of step S02, variables with the same name and scope may appear, such as ".A1.B2.C3" appears in the first tree, and ".A1.B2.C3" also appears in the second tree. Here, the symbols "[update]" and "[throw]" are used to represent update and throw exception respectively. It is customized by the describer. If you need to update the definition of the previous tree, use ".A1[update].B2.C3", and if you want to throw an exception, use ".A1[throw].B2.C3".
[0018] In one embodiment, in the description of a tree, <.A1.B2.C3> can be used to indicate the assignment of a value to a node other than the current tree. For example, .A1.B2.C3 = 3, which is defined in tree A, can be changed directly in any node position in tree B by using <.A1.B2.C3> = 4.
[0019] Although the present invention has been disclosed as above in terms of preferred embodiments, they are not intended to limit the present invention. Anyone skilled in the art can make various changes or modifications without departing from the spirit and scope of the present invention. Therefore, the scope of protection of the present invention should be based on the scope of protection defined by the claims of this application.
Claims
1. A method for real-time parsing template engine, using YAML as description language and jinja2 template syntax as basis, comprising the following steps: Step S 01: Separate variable names and variable values: from left to right, from top to bottom, with colon as separator, variable name on the left and value on the right, indentation as scope separator, the same indentation range has the same scope, thus establishing a "tree" structure, the root of the tree is the top-level scope, there are multiple tree roots, "trees" are stored from top to bottom in the order of parsing, each "tree" is stored from top to bottom in the order of parsing, and the nodes of each "tree" are stored from left to right in the order of parsing. If there is illegal or non-compliant, stop parsing and throw an exception reason; Step S02: Variable replacement and update: Based on the "tree" structure obtained in step S01, replace and update variables in a cyclic incrementing manner, starting from the first node of the first tree, increasing from left to right and from top to bottom, and use the parsed and defined ones to replace. If undefined appears during replacement, stop replacing and throw an exception reason, until the last node of the last tree is fully parsed.
2. The method according to claim 1, characterized in that: In step S02, if the node has multiple peer nodes and child nodes, the child nodes are parsed first.
3. The method according to claim 1, characterized in that: During the parsing process of step S02, each node can clearly determine its own position and uniquely determine whether a variable is defined. If a variable is defined after a reference, the variable is considered not to exist and an exception is thrown.
4. The method according to claim 3, characterized in that: The variable can be referenced using a full path or a relative path.
5. The method according to claim 1, characterized in that: During the parsing process of step S02, if there are variables with the same name and the same scope, the labels "[update]" and "[throw]" are used to represent update and throw exception respectively.
