Markup Expressions
authors (advanced)
The {(...)}
"expression markup" allows for a variety of string and formatting operations to be performed from within markup. Operations defined by this recipe include substr, ftime, strlen, rand, mod, toupper / tolower, ucfirst, ucwords, pagename and asspaced.
Markup expressions can be nested, using the markup {(...(...)...)}
.
substr
The "substr" expression extracts portions of a string. The arguments are
- the string to be processed. Always quote the string to be processed.
- the initial position of the substring. Note that the initial position argument is zero-based (i.e., the first character is referenced via a "0").
- the number of characters to extract
{(substr "PmWiki" 2 3)} {(substr "PmWiki" 2)} {(substr "{*$Group}" 2)} {(substr "PmWiki" 0 1)} {(substr "PmWiki" 0 -3)} {(substr "PmWiki" -3)} | Wik Wiki Wiki P PmW iki |
To obtain the last n characters of a string use {(substr "string" -n)}
To truncate the last n characters of a string use {(substr "string" 0 -n)}
ftime
"Ftime" expressions are used for date and time formatting. The generic form is
{(ftime "fmt" "when")}
{(ftime fmt="fmt" when="when" tz="tz" locale="locale")}
where fmt is a formatting string and when is the time to be formatted. The arguments can be in either order and may use the optional "fmt=" and "when=" labels.
The "tz" (time zone) argument allows the setting of a different time zone, only available on installations running PHP 5.1 or newer. See the list of supported time zones, any value not found there will be silently ignored.
The "locale" (language locale) argument allows the time format to be printed in a different language for names of weekdays and months, as long as that language locale is available on the server. You can set different locales to try, separated with commas or spaces, for example "fr_FR.utf8,fr_FR,fr". If none of the listed locales is available, the argument is silently ignored.
With international locales, if you see weird characters, try adding or removing the ".utf8" or ".UTF-8" part of the locale, e.g. try both "fr_FR.utf8" and "fr_FR" to see which one works for you. Wikis with UTF-8 enabled are more likely to need the ".utf8" part.
Examples:
{(ftime)} {(ftime fmt="%F %H:%M")} {(ftime %Y)} {(ftime fmt=%T)} {(ftime when=tomorrow)} {(ftime fmt="%Y-%m-%d" yesterday)} {(ftime "+1 week" %F)} {(ftime fmt=%D "+1 month")} {(ftime fmt="%a%e %b" when="next week")} {(ftime fmt="%A, %d %B %Y, %Hh%M" locale=fr_FR.utf8 tz=Europe/Paris)} | September 08, 2024, at 12:55 AM 2024-09-08 00:55 2024 00:55:32 September 09, 2024, at 12:00 AM 2024-09-07 2024-09-15 10/08/24 Mon 9 Sep dimanche, 08 septembre 2024, 02h55 |
The fmt parameter is whatever is given by "fmt=", the first parameter containing a '%', or else the site's default. The formatting codes are described at PHP's strftime function. In addition to those, '%o' produces the ordinal suffix ("st" for 1st), '%F' produces ISO-8601 dates like 2022-10-30, '%s' produces Unix timestamps, and '%L' produces a format like @2022-10-30T01:27:34Z
which can be displayed in the timezone of the visitor per Cookbook:LocalTimes.
Some common formatting strings:
%F # ISO-8601 dates "2024-09-08" %s # Unix timestamp "1725756932" %H:%M:%S # time as hh:mm:ss "00:55:32" %m/%d/%Y # date as mm/dd/yyyy "09/08/2024" "%A, %B %e%o, %Y" # in words "Sunday, September 8th, 2024"
All formats, click to toggle
Category | Format | Description | Example |
---|---|---|---|
Day | %a | An abbreviated textual representation of the day | Sun through Sat |
Day | %A | A full textual representation of the day | Sunday through Saturday |
Day | %d | Two-digit day of the month (with leading zeros) | 01 to 31 |
Day | %e | Day of the month, with a space preceding single digits. | " 1 " to "31 " |
Day | %j | Day of the year, 3 digits with leading zeros | 001 to 366 |
Day | %u | ISO-8601 numeric representation of the day of the week | 1 (for Monday) through 7 (for Sunday) |
Day | %w | Numeric representation of the day of the week | 0 (for Sunday) through 6 (for Saturday) |
Day | %o | Ordinal suffix of the date (PmWiki-specific) | st in January 1st |
Week | %U | Week number of the given year, starting with the first Sunday as the first week | 13 (for the 13th full week of the year) |
Week | %V | ISO-8601:1988 week number of the given year, starting with the first week of the year with at least 4 weekdays, with Monday being the start of the week | 01 through 53 (where 53 accounts for an overlapping week) |
Week | %W | A numeric representation of the week of the year, starting with the first Monday as the first week | 46 (for the 46th week of the year beginning with a Monday) |
Month | %b | Abbreviated month name, based on the locale | Jan through Dec |
Month | %B | Full month name, based on the locale | January through December |
Month | %h | Abbreviated month name, based on the locale (an alias of %b ) | Jan through Dec |
Month | %m | Two digit representation of the month | 01 (for January) through 12 (for December) |
Year | %C | Two digit representation of the century (year divided by 100, truncated to an integer) | 19 for the 20th Century |
Year | %g | Two digit representation of the year going by ISO-8601:1988 standards (see %V ) | 09 for the week of January 6, 2009 |
Year | %G | The full four-digit version of %g | 2008 for the week of January 3, 2009 |
Year | %y | Two digit representation of the year | 09 for 2009, 79 for 1979 |
Year | %Y | Four digit representation for the year | 2038 |
Time | %H | Two digit representation of the hour in 24-hour format | 00 through 23 |
Time | %k | Hour in 24-hour format, with a space preceding single digits | " 0 " through "23 " |
Time | %I | (Uppercase 'i') Two digit representation of the hour in 12-hour format | 01 through 12 |
Time | %l | (Lowercase 'L') Hour in 12-hour format, with a space preceding single digits | " 1 " through "12 " |
Time | %M | Two digit representation of the minute | 00 through 59 |
Time | %p | UPPER-CASE 'AM' or 'PM' based on the given time | AM for 00:31, PM for 22:23 |
Time | %P | lower-case 'am' or 'pm' based on the given time | am for 00:31, pm for 22:23 |
Time | %r | Same as "%I:%M:%S %p " | 09:34:17 PM for 21:34:17 |
Time | %R | Same as "%H:%M " | 00:35 for 12:35 AM, 16:44 for 4:44 PM |
Time | %S | Two digit representation of the second | 00 through 59 |
Time | %T | Same as "%H:%M:%S " | 21:34:17 for 09:34:17 PM |
Time | %X | Preferred time representation based on locale, without the date | 03:59:16 or 15:59:16 |
Time | %z | The time zone offset. | -0500 for US Eastern Time |
Time | %Z | The time zone abbreviation. | EST for Eastern Time |
Date-Time | %c | Preferred date and time stamp based on locale | Tue Feb 5 00:45:10 2009 for February 5, 2009 at 12:45:10 AM |
Date-Time | %D | Same as "%m/%d/%y " | 02/05/09 for February 5, 2009 |
Date-Time | %F | Same as "%Y-%m-%d " | 2009-02-05 for February 5, 2009 |
Date-Time | %s | Unix Epoch Time timestamp | 305815200 for September 10, 1979 08:40:00 AM |
Date-Time | %x | Preferred date representation based on locale, without the time | 02/05/09 for February 5, 2009 |
Date-Time | %L | Human-readable UTC timestamp which will be displayed formatted either as $TimeFmt or in the local time zone of the visitor per $EnableLocalTimes (PmWiki-specific) | @2022-09-25T11:49:08Z |
Miscellaneous | %n | A newline character ("\n ") | -- |
Miscellaneous | %t | A Tab character ("\t ") | -- |
Miscellaneous | %% | A literal percentage character ("% ") | -- |
The "preferred" shortcut formats may show different values depending on whether the IntlDateFormatter PHP functions are enabled, or not -- for consistent results, use the full format you need. The names of months and days may be translated with the locale=
argument, if that locale is installed on the server.
Note that if you use an undefined format string such as %q
or %E
, it will be left unchanged by ftime, but later PmWiki may process it as a WikiStyle which may be unexpected.
The when parameter understands many different date formats. The when parameter is whatever is given by "when=", or whatever parameter remains after determining the format parameter. Some examples:
2007-04-11 # ISO-8601 dates 20070411 # dates without hyphens, slashes, or dots 2007-03 # months @1176304315 # Unix timestamps (seconds since 1-Jan-1970 00:00 UTC) now # the current time today # today @ 00:00:00 yesterday # yesterday @ 00:00:00 "next Monday" # relative dates "last Thursday" # relative dates "-3 days" # three days ago "+2 weeks" # two weeks from now
Note: If you want to convert a Unix timestamp you must prefix with the @. Thus, "{(ftime "%A, %B %d, %Y" @1231116927)}"
.
The when parameter uses PHP's strtotime function to convert date strings according to the GNU date input formats; as of this writing it only understands English phrases in date specifications.
The variable $FTimeFmt
can be used to override the default date format used by the "ftime" function. The default $FTimeFmt
is $TimeFmt
.
strlen
The "strlen" expression returns the length of a string. The first argument is the string to be measured.
{(strlen "{$:Summary}")} | 32 |
rand
The "rand" expression returns a random integer. The first argument is the minimum number to be returned and the second argument is the maximum number to be returned. If called without the optional min, max arguments rand() returns a pseudo-random integer between 0 and RAND_MAX. If you want a random number between 5 and 15 (inclusive), for example, use (rand 5 15).
{(rand)} {(rand 1 99)} | 763890619 49 |
mod
The advanced "mod" expression returns the modulo (remainder) of the division of two numbers. It may be used in advanced PageList templates together with {$$PageCount}
to insert markup every (modulo) entries, for example to create alternate styled "zebra" table rows, or to insert a line/row break. (See also PageLists, WikiStyles and ConditionalMarkup.)
>>comment<< %define=bg1 item bgcolor=#f88% %define=bg2 item bgcolor=#ff8% %define=bg0 item bgcolor=#8f8%[= [[#altrows]] * %bg{(mod {$$PageCount} 3)}% {=$Name} ({$$PageCount}) [[#altrowsend]]=] >><< (:pagelist fmt=#altrows group=PmWiki count=10:) |
|
toupper / tolower
The "toupper" and "tolower" expressions convert a string into uppercase or lowercase. The first argument is the string to be processed.
{(toupper "{$:Summary}")} {(tolower "{$:Summary}")} | STRING AND FORMATTING OPERATIONS string and formatting operations |
ucfirst / ucwords
The "ucfirst" expression converts to uppercase the first character of the string, and "ucwords", the first character of each word. The first argument is the string to be processed.
{(ucfirst "{$:Summary}")} {(ucwords "{$:Summary}")} | String and formatting operations String And Formatting Operations |
pagename
The "pagename" expression builds a pagename from a string. The first argument is the string to be processed.
{(pagename "{$:Summary}")} | PmWiki.StringAndFormattingOperations |
asspaced
The "asspaced" expression formats wikiwords. The first argument is the string to be processed.
{(asspaced "{$FullName}")} | Pm Wiki.Markup Expressions |
Nesting expressions
Markup expressions can be nested. Omit the curly braces for the inner expressions:
{(tolower (substr "Hello World" 2))} | llo world |
Notes
- For PmWikis version 2.2.33 or older, the string-processing expressions may not work properly on multibyte UTF-8 characters. Newer versions should work fine.
See also
- ConditionalMarkup The if directive allows portions of a page to be included or excluded from rendering
- PageTextVariables Page variables automatically made available through natural or explicit page markup
- Page specific variables variables that are associated with pages
- Cookbook:MarkupExpressionSamples — custom markup expression samples
- Cookbook:MarkupExprPlus
This page may have a more recent version on pmwiki.org: PmWiki:MarkupExpressions, and a talk page: PmWiki:MarkupExpressions-Talk.