| 380 | === `py:with` === |
| 381 | |
| 382 | The `py:with` directive lets you assign expressions to variables, which can be used to make expressions inside the directive less verbose and more efficient. For example, if you need use the expression `author.posts` more than once, and that actually results in a database query, assigning the results to a variable using this directive would probably help. |
| 383 | |
| 384 | For example: |
| 385 | |
| 386 | {{{ |
| 387 | #!xml |
| 388 | <div xmlns:py="http://markup.edgewall.org/"> |
| 389 | <span py:with="y=7; z=x+10">$x $y $z</span> |
| 390 | </div> |
| 391 | }}} |
| 392 | |
| 393 | Given `x=42` in the context data, this would produce: |
| 394 | |
| 395 | {{{ |
| 396 | #!xml |
| 397 | <div> |
| 398 | <span>42 7 52</span> |
| 399 | </div> |
| 400 | }}} |
| 401 | |
| 402 | This directive can also be used as an element: |
| 403 | |
| 404 | {{{ |
| 405 | #!xml |
| 406 | <div xmlns:py="http://markup.edgewall.org/"> |
| 407 | <py:with vars="y=7; z=x+10">$x $y $z</py:with> |
| 408 | </div> |
| 409 | }}} |
| 410 | |