Tags:
create new tag
, view all tags

Macros

Special text strings expand on the fly to display user data or system info

Macros are text strings - %MACRONAME% or %MACRONAME{ parameter="value" }% - that expand into content whenever a topic is rendered for viewing. There are two types of macros:

  1. Preference settings: Can be defined and changed by the user
  2. Predefined macros: Defined by the system or by Plugins (for example, the SpreadSheetPlugin introduces a %CALC{}% macro)

Using Macros

To use a macro type its name. For example,

  • type %T% to get TIP (a preference settings)
  • type %TOPIC% to get Macros (a predefined macro?)
  • type %CALC{ "$UPPER(Text)" }% to get TEXT (a macro? defined by a Plugin)

Note:

  • To leave a macro unexpanded, precede it with an exclamation point, e.g. type !%TOPIC% to get %TOPIC%
  • Macros are expanded relative to the topic they are used in, not the topic they are defined in
  • Type %ALLVARIABLES% to get a full listing of all macros defined for a particular topic

Macro Names

Macro names must start with a letter. The following characters can be letters, numbers and the underscore '_'. You can use both upper-case and lower-case letters and you can mix the characters. E.g. %MYVAR%, %MyVar%, %My2ndVar%, and %My_Var% are all valid macro names. Macros are case sensitive. %MyVAR% and %MYVAR% are not the same macro.

By convention all settings, predefined macros and macros used by plugins are always UPPER-CASE.

Preferences

Preferences settings are simple macros that do not accept parameters, and are defined in topics. A lot of the macros you will encounter are of this type.

Preferences can be defined by the user in various places.

Setting Preferences

You can set macros in all the following places:
  1. default level in System.DefaultPreferences (not recommended)
  2. plugin topics (see Plugins)
  3. local site level in Main.SitePreferences
  4. user level in individual user topics in Main web
  5. web level in WebPreferences of a parent web
  6. web level in WebPreferences of the web
  7. topic level in topics in webs
  8. session macros (if sessions are enabled)

Settings at higher-numbered levels override settings of the same macro at lower numbered levels, unless the macro was included in the setting of FINALPREFERENCES at a lower-numbered level, in which case it is locked at the value it has at that level.

If you are setting a preference and using it in the same topic, note that Foswiki reads all the preference settings from the saved version of the topic before it displays anything. This means you can use a macro anywhere in the topic, even if you set it somewhere inconspicuous near the end. But beware: it also means that if you change the setting of a macro you are using in the same topic, Preview will show the wrong thing, and you must Save the topic to see it correctly.

The syntax for setting macros is the same anywhere:
[multiple of 3 spaces] * [space] Set [space] MACRONAME [space] = [space] value

Examples:
  • Set MACRONAME = value
    • Set MACRONAME = value
Spaces between the = sign and the value will be ignored. You can split a value over several lines by indenting following lines with spaces - as long as you don't try to use * as the first character on the following line.
Example:
   * Set MACRONAME = value starts here
     and continues here

Whatever you include in your Macro will be expanded on display, exactly as if it had been entered directly.

Example: Create a custom logo macro

You can also set preference settings in a topic by clicking the link Edit topic preference settings under More topic actions. Preferences set in this manner are not visible in the topic text, but take effect nevertheless.

Access Control Settings

These are special types of preference settings to control access to content. AccessControl explains these security settings in detail.

Local values for preferences

Certain topics (a users home topic, web site and default preferences topics) have a problem; macros defined in those topics can have two meanings. For example, consider a user topic. A user may want to use a double-height edit box when they are editing their home topic - but only when editing their home topic. The rest of the time, they want to have a normal edit box. This separation is achieved using Local in place of Set in the macro definition. For example, if the user sets the following in their home topic:
   * Set EDITBOXHEIGHT = 10
   * Local EDITBOXHEIGHT = 20
Then when they are editing any other topic, they will get a 10 high edit box. However when they are editing their home topic, they will get a 20 high edit box. Local can be used wherever a preference needs to take a different value depending on where the current operation is being performed.

Use this powerful feature with great care! %ALLVARIABLES% can be used to get a listing of the values of all macros in their evaluation order, so you can see macro scope if you get confused.

Predefined Macros

Most predefined macros return values that were either set in the configuration when Foswiki was installed, or taken from server info (such as current username, or date and time). Some, like %SEARCH%, are powerful and general tools.

  • ALERT! Predefined macros can be overridden by preference settings (except TOPIC and WEB)
  • ALERT! Plugins may extend the set of predefined macros (see individual Plugins topics for details)
  • TIP Take the time to thoroughly read through ALL preference macros. If you actively configure your site, review macros periodically. They cover a wide range of functions, and it can be easy to miss the one perfect macro for something you have in mind. For example, see %BASETOPIC%, %INCLUDE%, and the mighty %SEARCH%.

This version of Foswiki - Foswiki-1.0.9, Sun, 17 Jan 2010, build 6075 - predefines the following macros:

Could not perform search. Error was: Use of uninitialized value in pattern match (m//) at /var/www/result/var/www/htdocs/result.sonologic.net/lib/Foswiki/Store.pm line 1488. at /usr/local/lib/perl5/5.8.8/CGI/Carp.pm line 314 CGI::Carp::realdie('Use of uninitialized value in pattern match (m//) at /var/www...') called at /usr/local/lib/perl5/5.8.8/CGI/Carp.pm line 385 CGI::Carp::die('Use of uninitialized value in pattern match (m//) at /var/www...') called at /var/www/result/var/www/htdocs/result.sonologic.net/lib/Foswiki.pm line 157 Foswiki::__ANON__('Use of uninitialized value in pattern match (m//) at /var/www...') called at /var/www/result/var/www/htdocs/result.sonologic.net/lib/Foswiki/Store.pm line 1488 Foswiki::Store::extractMetaData('Foswiki::Store=HASH(0x849f374)', 'Foswiki::Meta=HASH(0x8c62fd0)', '%META:TOPICINFO{author="ProjectContributor" date="1182547552"...') called at /var/www/result/var/www/htdocs/result.sonologic.net/lib/Foswiki/Meta.pm line 97 Foswiki::Meta::new('Foswiki::Meta', 'Foswiki=HASH(0x8100b74)', 'System', 'VarFLOWCHART', '%META:TOPICINFO{author="ProjectContributor" date="1182547552"...') called at /var/www/result/var/www/htdocs/result.sonologic.net/lib/Foswiki/Store.pm line 140 Foswiki::Store::readTopic('Foswiki::Store=HASH(0x849f374)', 'undef', 'System', 'VarFLOWCHART', 'undef') called at /var/www/result/var/www/htdocs/result.sonologic.net/lib/Foswiki/Search.pm line 1185 Foswiki::Search::_getTextAndMeta('Foswiki::Search=HASH(0x8dde760)', 'undef', 'System', 'VarFLOWCHART') called at /var/www/result/var/www/htdocs/result.sonologic.net/lib/Foswiki/Search.pm line 1146 Foswiki::Search::_extractTopicInfo('Foswiki::Search=HASH(0x8dde760)', 'System', 'VarFLOWCHART', 0, 'undef') called at /var/www/result/var/www/htdocs/result.sonologic.net/lib/Foswiki/Search.pm line 784 Foswiki::Search::searchWeb('Foswiki::Search=HASH(0x8dde760)', 'inline', 1, 'casesensitive', 'on', 'search', '^Var[A-Z_0-9]+$', 'basetopic', 'Macros', ...) called at /var/www/result/var/www/htdocs/result.sonologic.net/lib/Foswiki.pm line 3855 Foswiki::__ANON__() called at /var/www/result/var/www/htdocs/result.sonologic.net/lib/CPAN/lib//Error.pm line 379 eval {...} called at /var/www/result/var/www/htdocs/result.sonologic.net/lib/CPAN/lib//Error.pm line 371 Error::subs::try('CODE(0x8dde928)', 'HASH(0x8dde838)') called at /var/www/result/var/www/htdocs/result.sonologic.net/lib/Foswiki.pm line 3864 Foswiki::SEARCH('Foswiki=HASH(0x8100b74)', 'Foswiki::Attrs=HASH(0x8ddea84)', 'Macros', 'System', 'Foswiki::Meta=HASH(0x87d8850)') called at /var/www/result/var/www/htdocs/result.sonologic.net/lib/Foswiki.pm line 2891 Foswiki::_expandTagOnTopicRendering('Foswiki=HASH(0x8100b74)', 'SEARCH', '\x{a} "^Var[A-Z_0-9]+$"\x{a} casesensitive="on"\x{a} web="System...', 'Macros', 'System', 'Foswiki::Meta=HASH(0x87d8850)') called at /var/www/result/var/www/htdocs/result.sonologic.net/lib/Foswiki.pm line 2796 Foswiki::_processTags('Foswiki=HASH(0x8100b74)', '%STARTSECTION{type="include"}%\x{a}---+ Macros\x{a}\x{a}_Special text str...', 'CODE(0x83f8658)', 16, 'Macros', 'System', 'Foswiki::Meta=HASH(0x87d8850)') called at /var/www/result/var/www/htdocs/result.sonologic.net/lib/Foswiki.pm line 2713 Foswiki::expandAllTags('Foswiki=HASH(0x8100b74)', 'SCALAR(0x8b7ea00)', 'Macros', 'System', 'Foswiki::Meta=HASH(0x87d8850)') called at /var/www/result/var/www/htdocs/result.sonologic.net/lib/Foswiki.pm line 3041 Foswiki::handleCommonTags('Foswiki=HASH(0x8100b74)', '%STARTSECTION{type="include"}%\x{a}---+ Macros\x{a}\x{a}_Special text str...', 'System', 'Macros', 'Foswiki::Meta=HASH(0x87d8850)') called at /var/www/result/var/www/htdocs/result.sonologic.net/lib/Foswiki/UI/View.pm line 388 Foswiki::UI::View::_prepare('%STARTSECTION{type="include"}%\x{a}---+ Macros\x{a}\x{a}_Special text str...', 'Foswiki=HASH(0x8100b74)', 'System', 'Macros', 'Foswiki::Meta=HASH(0x87d8850)', 0) called at /var/www/result/var/www/htdocs/result.sonologic.net/lib/Foswiki/UI/View.pm line 368 Foswiki::UI::View::view('Foswiki=HASH(0x8100b74)') called at /var/www/result/var/www/htdocs/result.sonologic.net/lib/Foswiki/UI.pm line 304 Foswiki::UI::__ANON__() called at /var/www/result/var/www/htdocs/result.sonologic.net/lib/CPAN/lib//Error.pm line 379 eval {...} called at /var/www/result/var/www/htdocs/result.sonologic.net/lib/CPAN/lib//Error.pm line 371 Error::subs::try('CODE(0x826e058)', 'HASH(0x87bb3d4)') called at /var/www/result/var/www/htdocs/result.sonologic.net/lib/Foswiki/UI.pm line 391 Foswiki::UI::_execute('Foswiki::Request=HASH(0x847a954)', 'CODE(0x8474ee0)', 'view', 1) called at /var/www/result/var/www/htdocs/result.sonologic.net/lib/Foswiki/UI.pm line 275 Foswiki::UI::handleRequest('Foswiki::Request=HASH(0x847a954)') called at /var/www/result/var/www/htdocs/result.sonologic.net/lib/Foswiki/Engine/CGI.pm line 29 Foswiki::Engine::CGI::run('Foswiki::Engine::CGI=HASH(0x8362314)') called

Shortcuts

The following macros are preference settings and are frequently used in topic content.

  • %BR% - line break
  • %BULLET% - bullet sign
  • %BB% - line break and bullet combined
  • %BB2% - indented line break and bullet
  • %RED% text %ENDCOLOR% - colored text (also %YELLOW%, %ORANGE%, %PINK%, %PURPLE%, %TEAL%, %NAVY%, %BLUE%, %AQUA%, %LIME%, %GREEN%, %OLIVE%, %MAROON%, %BROWN%, %BLACK%, %GRAY%, %SILVER%, %WHITE%)
  • %H% - HELP Help icon
  • %I% - IDEA! Idea icon
  • %M% - MOVED TO... Moved to icon
  • %N% - NEW New icon
  • %P% - REFACTOR Refactor icon
  • %Q% - QUESTION? Question icon
  • %S% - PICK Pick icon
  • %T% - TIP Tip icon
  • %U% - UPDATED Updated icon
  • %X% - ALERT! Alert icon
  • %Y% - DONE Done icon

See ShortcutMacros for a full list of predefined shortcuts.


Related Topics: UserDocumentationCategory
Topic revision: r1 - 09 Jan 2009 - 12:00:00 - ProjectContributor
 
This site is powered by FoswikiCopyright © by the contributing authors. All material on this site is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback