| 48 | === New API for Generating HTML fragments === |
| 49 | |
| 50 | Since Trac [milestone:0.9.3], the generation of HTML using ClearSilver has been modified so that by default, every string content will get HTML-escaped (e.g. "<br />" replaced by "<br />"). |
| 51 | Most plugin developers discovered this change the hard way ;) |
| 52 | The recommended way for avoiding this escaping was to wrap the strings containing markup in a `Markup` instance. |
| 53 | While this is still valid in 0.10, you should note that `Markup` is now defined in the `trac.util.html` module, though importing from `trac.util` will still work. |
| 54 | |
| 55 | More importantly, there's now a new way to programmatically generate markup content, using the `html` object, also defined in that `trac.util.html` module. |
| 56 | That object will dynamically generate `Element` objects, that can be nested. |
| 57 | The way to use it is: |
| 58 | {{{ |
| 59 | html.<elementname>(*other_elements_or_strings, **attributes) |
| 60 | }}} |
| 61 | |
| 62 | Example: (from the [trachacks:DoxygenPlugin]) |
| 63 | {{{ |
| 64 | #!diff |
| 65 | --- a/doxygentrac/doxygentrac.py Tue Aug 29 14:05:15 2006 +0200 |
| 66 | +++ b/doxygentrac/doxygentrac.py Wed Aug 30 14:35:12 2006 +0200 |
| 67 | @@ -40,43 +69,36 @@ class DoxygenPlugin(Component): |
| 68 | |
| 69 | def get_active_navigation_item(self, req): |
| 70 | return 'doxygen' |
| 71 | + |
| 72 | def get_navigation_items(self, req): |
| 73 | if req.perm.has_permission('DOXYGEN_VIEW'): |
| 74 | - # Get config variables. |
| 75 | - title = self.env.config.get('doxygen', 'title', 'Doxygen') |
| 76 | - |
| 77 | # Return mainnav buttons. |
| 78 | - yield 'mainnav', 'doxygen', Markup('<a href="%s">%s</a>' % \ |
| 79 | - (self.env.href.doxygen() + '/', title)) |
| 80 | + yield 'mainnav', 'doxygen', \ |
| 81 | + html.a(self.title, href=req.href.doxygen()) |
| 82 | }}} |
| 83 | |
| 84 | For more details, refer to the [source:trunk/trac/util/html.py@head#L324 Element] docstring. |
| 85 | |