Jinja2


1. Data Manipulation in Templates

{{ user.name.title() }}

Explanation: Modifies user's name to uppercase and concatenates it.

2. Conditional Statements


<div data-gb-custom-block data-tag="if">

  <p>Administrator</p>

<div data-gb-custom-block data-tag="else"></div>

  <p>User</p>

</div>

Explanation: Displays "Administrator" if the user is an admin, otherwise "User".

3. Loops and Iterations


<div data-gb-custom-block data-tag="for">

  <div>{{ post.title }}</div>

</div>

Explanation: Loops through a list of posts and displays the title for each post.

4. Data Filtering

Explanation: Sorts a list of posts based on creation date.

5. Custom Filters

Explanation: Defines a custom filter to format a duration in minutes.

6. Macros

Explanation: Defines a reusable block that renders a post's title and content.

7. Includes

Explanation: Includes another template file into the current one.

8. Inheritance

Explanation: Creates a template that inherits the layout from a base template.

9. Static Asset Handling

Explanation: Generates the URL to a static asset, allowing for caching.

10. Raw Data Output

Explanation: Outputs raw data without escaping, allowing for dynamic content like HTML or JavaScript.

11. URI Escaping

Explanation: Escapes a URL string to protect against XSS attacks.

12. HTML Escape

Explanation: Escapes HTML characters to prevent cross-site scripting.

13. Date Manipulation

Explanation: Formats the current time using a specific date format.

14. Mathematical Operations

Explanation: Performs mathematical operations on template variables.

15. Exception Handling

Explanation: Catches Jinja2 exceptions and allows for error handling.

16. Default Values

Explanation: Provides a default value if a variable is undefined.

17. URL Building

Explanation: Generates a URL for a specific route.

18. Form Rendering

Explanation: Renders a HTML form with the specified action and method.

19. Delimiters

Explanation: Allows comments to be written within Jinja2 templates.

20. Unicode Handling

Explanation: Converts a string to unicode to handle non-ASCII characters.

21. Iteration Over Attributes

Explanation: Loops over the attributes of a dictionary object.

22. Dictionaries in Templates

Explanation: Retrieves a list of keys from a dictionary.

23. Callback Function Execution

Explanation: Calls a specified callback function with arguments.

24. String Joins

Explanation: Concatenates elements of a list into a string using the specified separator.

25. Object Attributes as Variables

Explanation: Assigns an object attribute to a template variable.

26. Function Arguments

Explanation: Calls a function with a specified argument.

27. Variable Assignment

Explanation: Assigns a value to a template variable.

28. Comparison Operators

Explanation: Compares template variables using comparison operators.

29. Regular Expression Matching

Explanation: Checks if a string matches a regular expression.

30. Boolean Operations

Explanation: Performs boolean operations on template variables.

31. List Operations

Explanation: Sorts a list in-place.

32. Dictionary Operations

Explanation: Retrieves a value from a dictionary, returning a default value if the key is not found.

33. Formatted String Literal

Explanation: Formats a string using the f-string syntax.

34. Call a Class Method

Explanation: Calls a method on an object and passes an argument.

35. Access a Class Attribute

Explanation: Accesses an attribute of a class.

36. Create an Object

Explanation: Creates an object of a class and passes an argument.

37. Loop Over a Range

Explanation: Iterates through a range of numbers.

38. Check if Variable is Defined

Explanation: Verifies whether a variable is defined.

39. Load a Template from a File

Explanation: Imports a template from a file and makes all its variables available.

40. Assign a Template Variable to Global Namespace

Explanation: Assigns a variable to the global namespace, making it available throughout the application.

41. Access Environment Variables

Explanation: Retrieves an environment variable.

42. Call a Callback Function with Keyword Arguments

Explanation: Calls a callback function with specific keyword arguments.

43. Execute Custom Code

Explanation: Executes custom Python code within the template.

44. Create a Custom Context

Explanation: Creates a temporary context with modified values.

45. Access Function Annotations

Explanation: Retrieves the type annotation for a function input parameter.

46. Call a Function with Positional Arguments

Explanation: Calls a function with positional arguments.

47. Index into a List or Tuple

Explanation: Accesses an element of a list or tuple using an index.

48. Slice a List or Tuple

Explanation: Slices a list or tuple to create a new list or tuple.

49. Get the Length of a List or Tuple

Explanation: Retrieves the number of elements in a list or tuple.

50. Combine Multiple Templates

Explanation: Combines multiple templates into a single cohesive template.

51. Access a Dictionary Value

Explanation: Retrieves the value associated with a key in a dictionary.

52. Get the Keys of a Dictionary

Explanation: Retrieves a list of the keys in a dictionary.

53. Get the Values of a Dictionary

Explanation: Retrieves a list of the values in a dictionary.

54. Check if a Key Exists in a Dictionary

Explanation: Checks if a key exists in a dictionary.

55. Iterate Over a Dictionary

Explanation: Iterates over the key-value pairs in a dictionary.

56. Get the First Item in a List or Tuple

Explanation: Retrieves the first item in a list or tuple.

57. Get the Last Item in a List or Tuple

Explanation: Retrieves the last item in a list or tuple.

58. Reverse a List or Tuple

Explanation: Creates a new list or tuple with the elements in reverse order.

59. Sort a List or Tuple

Explanation: Sorts a list or tuple in place.

60. Filter a List or Tuple

Explanation: Creates a new list or tuple containing only the items that pass a filter condition.

61. Map a List or Tuple

Explanation: Creates a new list or tuple by applying a function to each item in the original list or tuple.

62. Combine Multiple Lists or Tuples

Explanation: Combines multiple lists or tuples into a single list or tuple.

63. Check if a Value is in a List or Tuple

Explanation: Checks if a value is contained in a list or tuple.

64. Create a Set

Explanation: Creates a new empty set.

65. Add an Item to a Set

Explanation: Adds an item to a set.

66. Iterate Over a Set

Explanation: Iterates over the items in a set.

67. Check if a Set Contains an Item

Explanation: Checks if a set contains a specific item.

68. Intersect Two Sets

Explanation: Creates a new set containing the elements that are common to both sets.

69. Union Two Sets

Explanation: Creates a new set containing all the elements from both sets.

70. Difference Between Two Sets

Explanation: Creates a new set containing the elements that are in set1 but not in set2.

71. Symmetric Difference Between Two Sets

Explanation: Creates a new set containing the elements that are in either set1 or set2 but not in both.

72. Create a Frozen Set

Explanation: Creates a new frozen set.

73. Iterate Over a Frozen Set

Explanation: Iterates over the items in a frozen set.

74. Check if a Frozen Set Contains an Item

Explanation: Checks if a frozen set contains a specific item.

75. Convert a Set to a List

Explanation: Converts a set to a list.

76. Convert a Tuple to a List

Explanation: Converts a tuple to a list.

77. Convert a Dictionary to a List of Tuples

Explanation: Converts a dictionary to a list of tuples, where each tuple represents a key-value pair.

78. Convert a Dictionary to a List of Lists

Explanation: Converts a dictionary to a list of lists, where each list contains the values for a key in the dictionary.

**79