|
Cookbook /
MoreCustomPageVariablesVersion: n/a
Prerequisites:
Status:
Maintainer: Petko
Discussion: MoreCustomPageVariables-Talk
Categories: PageVariables
Questions answered by this recipe
DescriptionYou can add to a local customisation (config.php) those sample commands :
and then use e.g. Please contribute here with some interesting implementations that you have, may be useful for other users. Date related variablesHere is a set of date related variables I add to all my PmWiki's
## set up custom page variables
$FmtPV['$ThisYear'] = 'date("Y")'; # 2006
$FmtPV['$ThisDay'] = 'date("d")'; # 02 (for April 2), 20 (for April 20)
$FmtPV['$ThisWeek'] = 'date("W")'; # ISO-8601 week number of the year
$FmtPV['$ThisMonth'] = 'date("m")'; # 04 (for April), 12 (for December)
$FmtPV['$ThisMonthName'] = 'date("F")'; # January to December
$FmtPV['$Today'] = 'date("Y-m-d")'; # 2006-03-29
$FmtPV['$Tomorrow'] = 'date("Y-m-d", time()+60*60*24)'; # {$Tomorrow} will output '2006-03-29'
$FmtPV['$Yesterday'] = 'date("Y-m-d", time()-60*60*24)'; # {$Yesterday} will output '2006-03-29'
$FmtPV['$LastYear'] = 'date("Y")-1'; # 2006
$FmtPV['$LastDay'] = 'date("d", time()-60*60*24)'; # 02 (for April 2), 20 (for April 20)
$FmtPV['$LastWeek'] = 'date("W", time()-60*60*24*7)'; # ISO-8601 week number of the year
$FmtPV['$LastMonth'] = 'date("m", time()-60*60*24*date("d"))'; # 04 (for April), 12 (for December)
$FmtPV['$LastMonthName'] = 'date("F", time()-60*60*24*date("d"))'; # January to December
$FmtPV['$NextYear'] = 'date("Y")+1'; # 2006
$FmtPV['$NextDay'] = 'date("d", time()+60*60*24)'; # 02 (for April 2), 20 (for April 20)
$FmtPV['$NextWeek'] = 'date("W", time()+60*60*24*7)'; # ISO-8601 week number of the year
$FmtPV['$NextMonth'] = 'date("m", time()+60*60*24*(32-date("d")))'; # 04 (for April), 12 (for December)
$FmtPV['$NextMonthName'] = 'date("F", time()+60*60*24*(32-date("d")))'; # January to December
Page creation dateTo create a variable which shows the page creation time
## add page variable {$PageCreationDate} in format yyyy-mm-dd
$FmtPV['$PageCreationDate'] = 'strftime("%Y-%m-%d", $page["ctime"])';
## to use the same format that you define in config.php with $TimeFmt use
$FmtPV['$Created'] = "strftime(\$GLOBALS['TimeFmt'], \$page['ctime'])";
For pagelist ordering(from Cookbook.CustomPagelistSortOrderFunctions). Sorting by title, disregarding leading A/An/TheAssumes pages named or titled as in "A Title" or "The Title" and you wish to disregard the leading A/An/The.
$FmtPV['$TitleNoArticle'] =
'preg_replace("/^ *(?:The|An?) /i", "", (@$page["title"] ? $page["title"] : $AsSpacedFunction($name)), 1)';
(:pagelist ... order=$TitleNoArticle ...:) Sorting with last name firstAssumes pages named or titled as ZachAble, JohnSmith, AaronZimmerman and the desire is to have it sort in that order, by last name.
$FmtPV['$TitleLastFirst'] =
'preg_replace("/^(.*?)([A-Z][a-z0-9_]*)$/", "\\\\2, \\\\1", (@$page["title"] ? $page["title"] : $AsSpacedFunction($name)))';
(:pagelist ... order=$TitleLastFirst ...:) $ProfileLinksCount and $ProfileTargetsThis page variable is used in the Cookbook listings here on PmWiki.org, to count the number of profile links in a recipe or in a talk page. This number could be an indication for the popularity of the recipe.
$FmtPV['$ProfileLinksCount'] = 'ProfileLinksCount($page["targets"])';
function ProfileLinksCount($targets) {
$cnt = substr_count($targets, "Profiles.")-1; # without the maintainer
return $cnt>0? "+$cnt" : '';
}
Note that newer recipes usually have fewer comments than older ones, and this number does not generally depend on the Quality, the Stability, and the Ease of use of a recipe. New version (2009-09-01)
function ProfileTargets($targets) {
return implode(' ', str_replace("Profiles.","",
preg_grep('/Profiles\\./',explode(',',$targets))));
}
$FmtPV['$ProfileTargets'] = 'ProfileTargets($page["targets"])';
$MarkupExpr['unique_count'] = 'count(array_unique($args))';
Notes
Release Notes
CommentsYou can write all the FmtPV definitions using the php date function a bit simpler like $FmtPV['$CurrentYear'] = 'date("Y")';
Using the php date function you are restricted to dates in English. To make your dates international use the php strftime function, for instance # add page variable , formats today's date as yyyy-mm-dd
$FmtPV['$Today'] = 'strftime("%Y-%m-%d", time() )';
You can write a SpecialFunction function and call it like $FmtPV['$SpecialVar'] = 'SpecialFunction($pagename)';
Thank you Hans for the simplification tip - I changed it above. About the localized dates, one should probably first call something like setlocale("fr_FR.utf8");
As I've never had luck with these (really), eventually suggested See Also
Contributors
CommentsSee discussion at MoreCustomPageVariables-Talk User notes +2: If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki. |