trac-ticketlinks / wiki-default / WikiMacros

=  Wiki Macros =
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.

Another kind of macros are WikiProcessors. They typically deal with alternate markup formats and representation of larger blocks of information (like source code highlighting).

== Using Macros ==
Macro calls are enclosed in two ''square brackets''. Like python functions, macros can also have arguments, a comma separated list within parentheses. 

=== Examples ===

{{{
 [[Timestamp]]
}}}
Display:
 [[Timestamp]]

{{{
 [[HelloWorld(Testing)]]
}}}
Display:
 [[HelloWorld(Testing)]]

== Available Macros ==

''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].''

[[MacroList]]

== Macros from around the world ==
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.

----

== Developing Custom Macros ==
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.

It's easiest to learn from an example:
{{{
#!python
# MyMacro.py -- The world's simplest macro

def execute(hdf, args, env):
    return "Hello World called with args: %s" % args
}}}

You can also use the environment (`env`) object, for example to access configuration data and the database, for example:
{{{
#!python
def execute(hdf, txt, env):
    return env.get_config('trac', 'repository_dir')
}}}

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.

For more information about developing macros, see the [http://projects.edgewall.com/trac/wiki/TracDev development resources] on the main project site.

----
See also:  WikiProcessors, WikiFormatting, TracGuide
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.