Jinja Reserved Keywords
Overview
β This document is intended to provide an overview of Jinja's reserved keywords Rewst users will encounter while building their own automations, and addresses a common TypeError issue that users face when conflicts occur.β
List of Reserved Keywords
β
Control Structures
if
,elif
,else
: Conditional statementsfor
: Looping through sequenceswhile
: Loop based on a conditiontry
,except
,finally
: Exception handlingwith
: Context management β
Variables and Data Types
True
,False
: Boolean valuesNone
: Represents null valuedel
: Deletes a variable β
Functions and Definitions
def
: Function definitionreturn
: Returns a value from a functionyield
: Produces a generatorlambda
: Anonymous functionclass
: Defines a class β
Operators and Keywords
and
,or
,not
: Logical operatorsin
,is
: Comparison operatorsraise
: Raise an exceptionassert
: Assertion statementbreak
,continue
: Loop controlpass
: Null operationglobal
,nonlocal
: Scope specifiers β
Built-in Methods
split
: Splits a string into a listitems
: Returns a list of dictionary's key-value tuple pairsreplace
: Replaces a string with another stringget
: Retrieves the value of a dictionary keyresult
,results
: Used in Rewst to store outputs. Note that these are not built-in methods, they're used in Rewst as variable named to store outputs and should not cause issues. β
Imports and Modules
import
: Importing modulesfrom
: Specifies what attributes to import from a module β
Common Error: JSON Serialization Issue
β
Error:
β JSON cannot serialize 'builtin_function_or_method': <built-in method items of dict object at 0x7f618cb407c0>
β
Solution:
β Ensure that you're not inadvertently referencing a built-in method like items
as an object when you actually intend to use its output. Execute the method first before attempting to serialize it to JSON. β
Example:
β If the output of an API call stores all the items in a key named items
, reference it like this: β
β This ensures that you're referencing the actual data rather than the built-in items
method, thus avoiding the serialization issue.
Best Practices
β
Always prefix your own variables and function names to avoid collision with these reserved words.
Use these words in the appropriate context to ensure readability and maintainability. β
Conclusion
β Understanding these protected words in Jinja is critical for effective template development. Familiarize yourself with these terms to write better, error-free code.
Last updated