trac-ticketlinks / wiki-default / WikiMacros

=  Wiki Macros =
Trac macros are plugins to extend the Trac engine with custom 'functions' written in Python. They allow insertion of custom dynamic HTML data in any module 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). See also: WikiProcessors.

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

=== Examples ===

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

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


== Available Macros ==
As of 0.6, macros are still a new feature in Trac, and the list of available (and distributed) macros is 
admittedly not very impressive. In future Trac releases, we hope to build a library of useful macros, and will of course happily include contributed macros (see below). 

 * '''!HelloWorld''' -- An example macro, useful for learning how to write macros.
 * '''Timestamp''' -- Insert the current date and time.


----


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

  http://projects.edgewall.com/trac/wiki/MacroBazaar


----


== Developing New 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:
{{{
# MyMacro.py -- The world's simplest macro

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

=== Advanced Topics: Template-enabled Macros ===
For advanced uses, macros can also render structured output in HDF, to be rendered to HTML using clearsilver templates - like most Trac output. In short, this allows more generic and well-designed advanced macros.

Macros gain direct access to the main HDF tree, and are free to manipulate it. 

Example:

{{{
def execute(hdf, args):
    # Currently hdf is set only when the macro is called
    # From a wiki page
    if hdf:
        hdf.setValue('wiki.macro.greeting', 'Hello World')
        
    # args will be null if the macro is called without parentesis.
    args = args or 'No arguments'
    return 'Hello World, args = ' + args
}}}



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