|
| 1 | +# Variables in Components |
| 2 | + |
| 3 | +django-bird provides a way to manage local variables within components using the `{% bird:var %}` template tag. Similar to Django's built-in `{% with %}` tag, it allows you to create temporary variables, but with some key advantages: |
| 4 | + |
| 5 | +- No closing tag required (unlike `{% with %}` which needs `{% endwith %}`) |
| 6 | +- Variables are automatically cleaned up when the component finishes rendering |
| 7 | +- Variables are properly scoped to each component instance |
| 8 | +- Supports appending to existing values |
| 9 | + |
| 10 | +## Basic Usage |
| 11 | + |
| 12 | +The `{% bird:var %}` tag allows you to create and modify variables that are scoped to the current component. These variables are accessible through the `vars` context dictionary. |
| 13 | + |
| 14 | +### Creating Variables |
| 15 | + |
| 16 | +To create a new variable, use the assignment syntax: |
| 17 | + |
| 18 | +```htmldjango |
| 19 | +{% bird:var name='value' %} |
| 20 | +
|
| 21 | +{{ vars.name }} {# Outputs: value #} |
| 22 | +``` |
| 23 | + |
| 24 | +### Overwriting and Clearing Variables |
| 25 | + |
| 26 | +You can overwrite an existing variable by assigning a new value: |
| 27 | + |
| 28 | +```htmldjango |
| 29 | +{% bird:var counter='1' %} |
| 30 | +
|
| 31 | +{{ vars.counter }} {# Outputs: 1 #} |
| 32 | +
|
| 33 | +{% bird:var counter='2' %} |
| 34 | +
|
| 35 | +{{ vars.counter }} {# Outputs: 2 #} |
| 36 | +``` |
| 37 | + |
| 38 | +To reset/clear a variable, set it to None: |
| 39 | + |
| 40 | +```htmldjango |
| 41 | +{% bird:var message='hello' %} |
| 42 | +
|
| 43 | +{{ vars.message }} {# Outputs: hello #} |
| 44 | +
|
| 45 | +{% bird:var message=None %} |
| 46 | +
|
| 47 | +{{ vars.message }} {# Variable is cleared #} |
| 48 | +``` |
| 49 | + |
| 50 | +Alternatively, you can use explicit cleanup with `{% endbird:var %}` - see [Explicit Variable Cleanup](#explicit-variable-cleanup) for details. |
| 51 | + |
| 52 | +### Appending to Variables |
| 53 | + |
| 54 | +The `+=` operator lets you append to existing variables: |
| 55 | + |
| 56 | +```htmldjango |
| 57 | +{% bird:var greeting='Hello' %} |
| 58 | +{% bird:var greeting+=' World' %} |
| 59 | +
|
| 60 | +{{ vars.greeting }} {# Outputs: Hello World #} |
| 61 | +``` |
| 62 | + |
| 63 | +If you append to a non-existent variable, it will be created: |
| 64 | + |
| 65 | +```htmldjango |
| 66 | +{% bird:var message+='World' %} |
| 67 | +
|
| 68 | +{{ vars.message }} {# Outputs: World #} |
| 69 | +``` |
| 70 | + |
| 71 | +## Variable Scope |
| 72 | + |
| 73 | +Variables created with `{% bird:var %}` are: |
| 74 | + |
| 75 | +- Local to the component where they are defined |
| 76 | +- Isolated between different instances of the same component |
| 77 | +- Not accessible outside the component |
| 78 | +- Reset between renders |
| 79 | + |
| 80 | +```{code-block} htmldjango |
| 81 | +:caption: templates/bird/button.html |
| 82 | +
|
| 83 | +{% bird:var count='1' %} |
| 84 | +
|
| 85 | +Count: {{ vars.count }} |
| 86 | +``` |
| 87 | + |
| 88 | +```{code-block} htmldjango |
| 89 | +:caption: template.html |
| 90 | +
|
| 91 | +{% bird button %}{% endbird %} {# Count: 1 #} |
| 92 | +{% bird button %}{% endbird %} {# Count: 1 #} |
| 93 | +
|
| 94 | +Outside: {{ vars.count }} {# vars.count is not accessible here #} |
| 95 | +``` |
| 96 | + |
| 97 | +Each instance of the button component will have its own isolated `count` variable. |
| 98 | + |
| 99 | +### Explicit Variable Cleanup |
| 100 | + |
| 101 | +While variables are automatically cleaned up when a component finishes rendering, you can explicitly clean up variables using the `{% endbird:var %}` tag: |
| 102 | + |
| 103 | +```htmldjango |
| 104 | +{% bird:var message='Hello' %} |
| 105 | +
|
| 106 | +{{ vars.message }} {# Outputs: Hello #} |
| 107 | +
|
| 108 | +{% endbird:var message %} |
| 109 | +
|
| 110 | +{{ vars.message }} {# Variable is now cleaned up #} |
| 111 | +``` |
| 112 | + |
| 113 | +This can be useful when you want to ensure a variable is cleaned up at a specific point in your template, rather than waiting for the component to finish rendering. |
| 114 | + |
| 115 | +You can clean up multiple variables independently: |
| 116 | + |
| 117 | +```htmldjango |
| 118 | +{% bird:var x='hello' %} |
| 119 | +{% bird:var y='world' %} |
| 120 | +
|
| 121 | +{{ vars.x }} {{ vars.y }} {# Outputs: hello world #} |
| 122 | +
|
| 123 | +{% endbird:var x %} |
| 124 | +
|
| 125 | +{{ vars.x }} {{ vars.y }} {# Outputs: world (x is cleaned up) #} |
| 126 | +
|
| 127 | +{% endbird:var y %} |
| 128 | +
|
| 129 | +{{ vars.x }} {{ vars.y }} {# Both variables are now cleaned up #} |
| 130 | +``` |
| 131 | + |
| 132 | +## Working with Template Variables |
| 133 | + |
| 134 | +You can use template variables when setting values: |
| 135 | + |
| 136 | +```htmldjango |
| 137 | +{% bird:var greeting='Hello ' %} |
| 138 | +{% bird:var greeting+=user.name %} |
| 139 | +
|
| 140 | +{{ vars.greeting }} {# Outputs: Hello John #} |
| 141 | +``` |
| 142 | + |
| 143 | +## Nested Components |
| 144 | + |
| 145 | +Variables are properly scoped in nested components: |
| 146 | + |
| 147 | +```{code-block} htmldjango |
| 148 | +:caption: templates/bird/outer.html |
| 149 | +
|
| 150 | +{% bird:var message='Outer' %} |
| 151 | +
|
| 152 | +{{ vars.message }} |
| 153 | +
|
| 154 | +{% bird inner %}{% endbird %} |
| 155 | +
|
| 156 | +{{ vars.message }} {# vars.message still contains 'Outer' here #} |
| 157 | +``` |
| 158 | + |
| 159 | +```{code-block} htmldjango |
| 160 | +:caption: templates/bird/inner.html |
| 161 | +
|
| 162 | +{% bird:var message='Inner' %} |
| 163 | +
|
| 164 | +{{ vars.message }} {# Contains 'Inner', doesn't affect outer component #} |
| 165 | +``` |
0 commit comments