Recent Changes - Search:

Cookbook

PmWiki

pmwiki.org

MoreCustomPageVariables

Summary: Additional custom page variables for use in pages, forms, includes and conditional markup.
Version:
Prerequisites:
Status:
Maintainer: Petko (first draft)
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['$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"]';

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.

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

Edit - History - Print - Recent Changes - Search
Page last modified on November 19, 2007, at 05:29 AM