Summary: Include a section from the first available among a list of pages
Version: 20160506
Maintainer: Petko
Categories: Includes, Markup, PHP55
Users: +1 (View / Edit)
License: PD


Include a section from the first available among a list of pages.

The PmWiki directive (:include Page1 Page2 Page3 #from#to:) does not work as advertized. If Page1 exists, but does not contain a section #from#to, it returns nothing without checking the other pages.

I have a list of pages that "may" contain a specific section. I need to include this section if it exists in the first page, if not: from the second page and so on. This recipe adds a new markup that does exactly this:

   (:includesection #from#to Page1 Page2 Page3:) 
   (:includesection #section Page1 Page2 Page3:) 

Note that the section anchors are the first parameter, unlike with the (:include:) markup.


Place the following code in local/config.php :

Markup_e('includesection', '>if',
  "PRR(IncludeSection(\$pagename, \$m[1]))");
$SaveAttrPatterns['/\\(:includesection\\s.*?:\\)/i'] = ' ';

function IncludeSection($pagename, $inclspec) {
  $args = ParseArgs($inclspec);
  $anc = array_shift($args['']);
  if($anc>'' && $anc{0}!="#") return '';
  foreach($args[''] as $v) {
    $x = IncludeText($pagename, "$v$anc");
    if($x>'') return $x;

To set additional parameters such as lines= or basepage=, use such a markup (note the quotes):

   (:includesection "#from#to lines=3 self=1" Page1 Page2 Page3:) 

To specify different section names with each page, use this (note the quotes) :

   (:includesection "" Page1#sec1 Page2#sec2 Page3#sec3:) 


Release Notes

  • 20160506 : update for PHP 5.5
  • 20090228 : added $SaveAttrPatterns skip definition (suggested by Tontyna)
  • 20090112a : added way to specify different section names with each page
  • 20090112 : first release

See Also



See discussion at IncludeSection-Talk

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.