|
2 | 2 | Web Applications |
3 | 3 | ================ |
4 | 4 |
|
| 5 | +As a powerful scripting language adapted to both fast prototyping |
| 6 | +and bigger projects, Python is widely used in Web applications |
| 7 | +development. |
5 | 8 |
|
6 | 9 | Context |
7 | 10 | ::::::: |
@@ -203,6 +206,44 @@ Gondor publishes guides to deploying `Django projects |
203 | 206 | <https://gondor.io/support/setting-up-pinax/>`_ on their platform. |
204 | 207 |
|
205 | 208 |
|
| 209 | +Templating |
| 210 | +:::::::::: |
| 211 | + |
| 212 | +Most WSGI applications are responding to HTTP requests to serve |
| 213 | +content in HTML or other markup languages. Instead of generating directly |
| 214 | +textual content from Python, the concept of separation of concerns |
| 215 | +advise us to use templates. A template engine manage a suite of |
| 216 | +template files, with a system of hierarchy and inclusion to |
| 217 | +avoid unnecessary repetition, and is in charge of rendering |
| 218 | +(generating) the actual content, filling the static content |
| 219 | +of the templates with the dynamic content generated by the |
| 220 | +application. |
| 221 | + |
| 222 | +As template files are |
| 223 | +sometime written by designers or front-end developpers, |
| 224 | +it can be difficult to handle increasing complexity. |
| 225 | + |
| 226 | +Some general good pratices apply to the part of the |
| 227 | +application passing dynamic content to the template engine, |
| 228 | +and to the templates themselves. |
| 229 | + |
| 230 | +- Template files should be passed only the dynamic |
| 231 | + content that is needed for rendering the template. Avoid |
| 232 | + to be tempted to pass additional content "just in case": |
| 233 | + it is easier to add some missing variable when needed than to remove |
| 234 | + a likely unused variable later. |
| 235 | + |
| 236 | +- Many template engine allow for complex statements |
| 237 | + or assignments in the template itself, and many |
| 238 | + allow some Python code to be evaluated in the |
| 239 | + templates. This convenience can lead to uncontrolled |
| 240 | + increase in complexity, and often harder to find bugs. |
| 241 | + |
| 242 | +- It is often possible or necessary to mix javascript templates with |
| 243 | + HTML templates. A sane approach to this design is to isolate |
| 244 | + the parts where the HTML template passes some variable content |
| 245 | + to the javascript code. |
| 246 | + |
206 | 247 | .. rubric:: References |
207 | 248 |
|
208 | 249 | .. [1] `The mod_python project is now officially dead <http://blog.dscpl.com.au/2010/06/modpython-project-is-now-officially.html>`_ |
|
0 commit comments