-- DavidMcNicol
There are a few standard substitutions that can be performed on the templates:
The following sections of code should be grafted into your wiki script.
This should be added at the end of the configuration section, before the "you should not have to change anything below this line" comment.
# == WikiTemplates patch ================================================= use vars qw( $WTName $WTOverwriteExisting ); $WTName = "WikiTemplates"; # name of the page where templates are stored. $WTOverwriteExisting = 1; # 1 = templates can replace existing content. # ========================================================================
$WTName
variable specifies the page that will be searched for template subpages. If it is left blank, the template functionality will be switched off.
$WTOverwriteExisting
variable controls whether the user can overwrite existing content with a template. If it is set to 0, the template option will only appear when new pages are edited.
These subroutines can be added anywhere. I put it after the DoEdit
subroutine, to keep everything in the one place:
# == WikiTemplates patch ================================================= sub GetTemplateForm { my $id = shift; my @templates = &GetTemplateList(); my $result = &GetFormStart() . "\n"; $result .= $q->popup_menu(-name=>'template', -values=>\@templates); $result .= &GetHiddenValue("action", "edit") . "\n"; $result .= &GetHiddenValue("id", $id) . "\n"; $result .= $q->submit(-name=>'use', -value=>T('Use template')) . "\n"; $result .= $q->endform(); return $result; } sub GetTemplateList { my @list = T( "None" ); my $dir = $PageDir . "/" . &GetPageDirectory($WTName) . "/" . $WTName; opendir(TEMPLATES, $dir); my @files = readdir(TEMPLATES); closedir(TEMPLATES); foreach my $file ( @files ) { next if $file =~ /^\./; push( @list, $file ) if $file =~ s/\.db$//; } return @list; } sub GetTemplateText { my $template = shift; my $id = shift; return unless $template; return if $template eq T("None"); my $page = "$WTName/$template"; &OpenPage( $page ); &OpenDefaultText(); return &PerformTemplateSubstitutions( $Text{'text'}, $id ); } sub PerformTemplateSubstitutions { my $text = shift; my $id = shift; my $today = &CalcShortDayNow(); my $username = &GetParam("username", ""); $text =~ s/%%%\s*date\s*%%%/$today/gi; $text =~ s/%%%\s*this\s*%%%/$id/gi; $text =~ s/%%%\s*user\s*%%%/$username/gi; return $text; } sub CalcShortDay { my ($ts) = @_; $ts += $TimeZoneOffset; my ($sec, $min, $hour, $mday, $mon, $year) = localtime($ts); return sprintf( "%d-%0.2d-%0.2d", $year + 1900, $mon + 1, $mday ); } sub CalcShortDayNow { return CalcShortDay($Now); } # ========================================================================
DoEdit
These sections of code should be added to the DoEdit
subroutine. Their position is important.
This first section fetches a template if it has been requested. It should be added before the call to &OpenPage($id)
, which is around 17 lines below the start of the subroutine:
# == WikiTemplates patch ================================================= my $templateText = ""; if ( my $template = $q->param("template") ) { $templateText = &GetTemplateText( $template, $id ); } # ========================================================================
The second section replaces the text with the already fetched template text. It should be added after the $oldText = $Text{'text'}
line, which is around 34 lines below the start of the subroutine:
# == WikiTemplates patch ================================================= if ( $Page{'revision'} == 0 || $WTOverwriteExisting ) { $oldText = $templateText if $templateText; } # ========================================================================
The final section displays the template form if required. It must appear before the print &GetFormStart()
line, about 66 lines below the start of the subroutine:
# == WikiTemplates patch ================================================= if ( $Page{'revision'} == 0 || $WTOverwriteExisting ) { print &GetTemplateForm($id) if $WTName; } # ========================================================================