Recent Changes - Search:

Cookbook

PmWiki

pmwiki.org

MoreCustomPageVariables

Summary: Additional custom page variables for use in pages, forms, includes and conditional markup.
Version: n/a
Prerequisites:
Status:
Maintainer: Petko
Categories: PageVariables

Questions answered by this recipe

  • How to print the current date?
  • How to include a page or a section depending on the current date? For example
    • (:include Birthdays.{$CurrentMonthName}#{$CurrentDay}:)
    • or (:include ToDo.{$CurrentWeek}:)
    • or [[Blog.{$Today} | Today's articles]]

Description

You can add to a local customisation (config.php) those sample commands :

  • $FmtPV['$CurrentYear'] = 'date("Y")'; // 2006
  • $FmtPV['$CurrentDay'] = 'date("d")'; // 02 (for April 2), 20 (for April 20)
  • $FmtPV['$CurrentWeek'] = 'date("W")'; // ISO-8601 week number of the year
  • $FmtPV['$CurrentMonth'] = 'date("m")'; // 04 (for April), 12 (for December)
  • $FmtPV['$Today'] = 'date("Y-m-d")'; // 2006-03-29
  • $FmtPV['$Tomorrow'] = 'date("Y-m-d", time()+60*60*24 )'; // {$Tomorrow} will output something like '2006-03-29'
  • $FmtPV['$CurrentTime'] = 'date("H:i:s")'; // 14:29:32
  • $FmtPV['$CurrentTimeC'] = 'date("h:i:s A")'; // 02:29:32 PM
  • $FmtPV['$CurrentDayLong'] = 'date("l")'; // Monday through Sunday
  • $FmtPV['$CurrentDayShort'] = 'date("D.")'; // Mon. through Sun.
  • $FmtPV['$CurrentMonthLong'] = 'date("F")'; // April, December, etc.
  • $FmtPV['$TodayLong'] = 'date("F d, Y")'; // June 25, 2010
  • $FmtPV['$TodayLongAlt'] = 'date("d F Y")'; // 25 June 2010
  • $FmtPV['$TodayShort'] = 'date("Ymd")'; // 20060329
  • $FmtPV['$VisitorIP'] = '$_SERVER["REMOTE_ADDR"]'; // may be used in forms or cond. markup;
    on Mac OS X you should replace the latter by $_SERVER["HTTP_PC_REMOTE_ADDR"]. Cf. http://trac.wordpress.org/ticket/1443
  • $FmtPV['$CurrentSkin'] = '$GLOBALS["Skin"]';
  • $FmtPV['$WikiTitle'] = '$GLOBALS["WikiTitle"]';
  • $FmtPV['$RevCount'] = '$page["rev"]';// to get the revision number of a page ; use {$RevCount} in any page you want the rev number to be displayed.

and then use e.g. {$Today} in a page markup.

Please contribute here with some interesting implementations that you have, may be useful for other users.

Date related variables

Here is a set of date related variables I add to all my PmWiki's config.php

## 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)'; // will output something like '2006-03-29' $FmtPV['$Yesterday'] = 'date("Y-m-d", time()-60*60*24)'; // will output something like '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

Simon

Page creation date

To create a variable which shows the page creation time

## add page variable in format yyyy-mm-dd

$FmtPV['$PageCreationDate'] = 'strftime("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/The

Assumes 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 first

Assumes pages named or titled as ZachAble, JohnSmith, AaronZimmerman and the desire is to have it sort in that order, by last name.
Note that McBain or other last names with an embedded capital will not work in this setup.

$FmtPV['$TitleLastFirst'] =
   'preg_replace("/^(.*?)([A-Z][a-z0-9_]*)$/", "\\\\2, \\\\1", (@$page["title"] ? $page["title"] : $AsSpacedFunction($name)))';
(:pagelist ... order=$TitleLastFirst ...:)

$ProfileLinksCount and $ProfileTargets

This 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

  • Note that concerning times and dates, now it is better and much more flexible to use ftime from the new Markup expressions: for the blog example, try [[Blog.{(ftime fmt="%Y-%m-%d")} | Today's articles]]. See Markup expressions. (This page was written when Markup Expressions didn't exist yet.)
  • For invariable strings, use the format $FmtPV['$MyVariable'] = "'some text'"; (quotes and inside, nested apostrophes).
  • For PmWiki variables, i.e. defined in config.php, use the format $FmtPV['$MyVariable'] = '$GLOBALS["PmwikiVariable"]';.
  • For some kind of arithmetics or other more complicated output, use
    quote-apostrophe-quote-dot something dot-quote-apostrophe-quote, i.e.
    $FmtPV['$MyVariable'] = "'". complicated_function() ."'"; //i.e.
    $FmtPV['$NextMonth'] = "'". date("n", mktime(0, 0, 0, date("m")+1, date("d"), date("Y")) ) ."'";

Release Notes

If the recipe has multiple releases, then release notes can be placed here. Note that it's often easier for people to work with "release dates" instead of "version numbers".

Comments

You 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)';

HansB

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 $[April] and PmWiki's Internationalizations capabilities. --Petko April 02, 2006, at 07:51 AM

See Also

Contributors

  • All users listed on the page

Questions

Hi, is there a way to make a part of page name a variable in local config file. Some like with

 
{(substr "PmWiki" 0 1)}

function.

Thanks, Teukka

Sure. Use something like this after any scripts and recipes that you include:
# Get the group and page name
$pagename = ResolvePageName($pagename);
$name = PageVar($pagename, '$Name');
$my_special_config_variable = substr($name, 0, 1);
If you wish to use such a value as a custom PageVariable (in wiki pages), use this instead:
$FmtPV['$Substr01'] = 'substr($name, 0, 1)';
and use in the page {*$Substr01}. The variables $name and $group will be replaced on runtime. This also works with PageLists, use {=$Substr01} inside a pagelist template. --Petko October 11, 2007, at 09:48 AM

Thank You so much. This was great help for me.
Teukka


I have a template with an image I want to modify. Specifically, in the template file I have <img src="{ $ CustomImage}">. I want to modify this variable in the wiki edit mode so that I can set a unique "splash" image for each page. Is there anything like:

{SetVar CustomImage test.jpg}

that I can include in the bottom of my wiki code.

  chris 

I think many don't understand wiki templates. Consider the following:

       <!--wiki:{$Group}-{$Name}.Logo {$Group}.Logo {$SiteGroup}.Logo-->

Inside of a page template this will look for Group-Name.Logo first (where Group is the Group and Name is the Name), then Group.Logo (for the current Group), then ultimately fall back to the Site.Logo file. These turn into includes of the page... so to have a custom Logo per page, just define Group-Name.Logo files for each page.


I have a question. Is it possible to store a variable from a cookbook using FmtPV and call it in the wiki? It works perfectly when I store a variable in config.php, but it doesnt when I write it in a cookbook. I suppose I should be clearly specifying in which php cookbook the variable is stored. I wrote :

 $FmtPV['$TestVariable'] = "'SelectQuery is outputting'";  

in the selectquery.php cookbook

and in a page using this cookbook, i tried to call it using {$TestVariable}. But got no result.

Thanks for the support... Chris

In your script, place global $FmtPV; before defining $FmtPV['$TestVariable']. Also make sure you include your cookbook script from config.php or from Group.Page.php. --Petko August 20, 2009, at 04:34 PM

User notes +1: 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.

Edit - History - Print - Recent Changes - Search
Page last modified on November 24, 2011, at 05:54 PM