| 1 | = Wiki Macros = |
| 2 | Trac macros are plugins to extend the Trac engine with custom 'functions' written in Python. A macro inserts dynamic HTML data in any context supporting WikiFormatting. |
| 3 | |
| 4 | Another kind of macros are WikiProcessors. They typically deal with alternate markup formats and representation of larger blocks of information (like source code highlighting). |
| 5 | |
| 6 | == Using Macros == |
| 7 | Macro calls are enclosed in two ''square brackets''. Like python functions, macros can also have arguments, a comma separated list within parentheses. |
| 8 | |
| 9 | === Examples === |
| 10 | |
| 11 | {{{ |
| 12 | [[Timestamp]] |
| 13 | }}} |
| 14 | Display: |
| 15 | [[Timestamp]] |
| 16 | |
| 17 | {{{ |
| 18 | [[HelloWorld(Testing)]] |
| 19 | }}} |
| 20 | Display: |
| 21 | [[HelloWorld(Testing)]] |
| 22 | |
| 23 | == Available Macros == |
| 24 | |
| 25 | ''Note that the following list will only contain the macro documentation if you've not enabled `-OO` optimizations, or not set the `PythonOptimize` option for [wiki:TracModPython mod_python].'' |
| 26 | |
| 27 | [[MacroList]] |
| 28 | |
| 29 | == Macros from around the world == |
| 30 | The [http://projects.edgewall.com/trac/ Trac Project] has a section dedicated to user-contributed macros, [http://projects.edgewall.com/trac/wiki/MacroBazaar MacroBazaar]. If you're looking for new macros, or have written new ones to share with the world, don't hesitate adding it to the [http://projects.edgewall.com/trac/wiki/MacroBazaar MacroBazaar] wiki page. |
| 31 | |
| 32 | ---- |
| 33 | |
| 34 | == Developing Custom Macros == |
| 35 | Macros, like Trac itself, are written in the [http://www.python.org/ Python programming language]. They are very simple modules, identified by the filename and should contain a single ''entry point'' function. Trac will display the returned data inserted into the HTML where the macro was called. |
| 36 | |
| 37 | It's easiest to learn from an example: |
| 38 | {{{ |
| 39 | #!python |
| 40 | # MyMacro.py -- The world's simplest macro |
| 41 | |
| 42 | def execute(hdf, args, env): |
| 43 | return "Hello World called with args: %s" % args |
| 44 | }}} |
| 45 | |
| 46 | You can also use the environment (`env`) object, for example to access configuration data and the database, for example: |
| 47 | {{{ |
| 48 | #!python |
| 49 | def execute(hdf, txt, env): |
| 50 | return env.get_config('trac', 'repository_dir') |
| 51 | }}} |
| 52 | |
| 53 | Note that since version 0.9, wiki macros can also be written as TracPlugins. This gives them some capabilities than “classic” macros do not have, such as directly access the HTTP request. |
| 54 | |
| 55 | For more information about developing macros, see the [http://projects.edgewall.com/trac/wiki/TracDev development resources] on the main project site. |
| 56 | |
| 57 | ---- |
| 58 | See also: WikiProcessors, WikiFormatting, TracGuide |