Skip to content

Commit

Permalink
Item11833: standardise use of $value(display), clean up templates usi…
Browse files Browse the repository at this point in the history
…ng it and simplify templates using complex context IFs where TMPL:P can be used instead

git-svn-id: http://svn.foswiki.org/trunk@17467 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
CrawfordCurrie authored and CrawfordCurrie committed Apr 2, 2014
1 parent ea39af3 commit b95b9e0
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 86 deletions.
18 changes: 15 additions & 3 deletions core/data/System/VarFORMFIELD.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
%META:TOPICINFO{author="ProjectContributor" date="1394718137" format="1.1" version="1"}%
%META:TOPICINFO{author="ProjectContributor" date="1396462202" format="1.1" version="1"}%
%META:TOPICPARENT{name="Macros"}%
#VarFORMFIELD
---+++ FORMFIELD{"fieldname"} -- renders a field in the form attached to some topic
* Syntax: =%<nop>FORMFIELD{"fieldname"}%=
* Supported parameters:
| *Parameter:* | *Description:* | *Default:* |
| ="fieldname"= | The name of a [[DataForms][Data form]] field | required |
| ="name"= | The name of a [[DataForms][Data form]] field | required |
| =topic="..."= | Topic where form data is located. May be of the form =Web.<nop>TopicName= | Current topic |
| =format="..."= | Format string. =$value= expands to the field value, and =$name= expands to the field name, =$title= to the field title, =$form= to the name of the form the field is in. The [[FormatTokens][standard format tokens]] are also expanded. | ="$value"= |
| =format="..."= | Format string. See Tokens expanded in =format= below. | ="$value"= |
| =default="..."= | Text shown if the field is defined in the topic, but the field value is empty. For example, a text field for which all the content has been deleted. | =""= |
| =alttext="..."= | Text shown if the field is not defined in the topic (even if it is specified in the form definition). For example, this is used when a field exists in the form definition, but the referring topic hasn't been edited since it was added. | =""= |
| =rev="n"= | Specifiy a revision of the topic. If not specified, defaults to the most recent rev (or the viewed rev if viewing an old rev of the same topic) |
* Tokens expanded in =format=:
* =$value= expands to the *raw* field value
* =$value(display)= is the form field value _after_ mapping the stored value to the display value (use with =+values= form fields). If the field type does not support value mapping, renders the same as =$value=
* =$name= is the field name
* =$title= expands to the field title
* =$formname= gives the name of the form the field is in. =$form= is maintained for compatibility, but is deprecated
* =$attributes= - from the field definition
* =$type= - from the field definition
* =$size= - from the field definition
* =$definingTopic= - topic in which the field is defined

* The [[FormatTokens][standard format tokens]] are also expanded
* Example: <verbatim class="tml"> %FORMFIELD{"ProjectName"
topic="Projects.SushiProject"
default="(no project name given)"
Expand Down
16 changes: 8 additions & 8 deletions core/lib/Foswiki/Form.pm
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,7 @@ sub renderForDisplay {
$templates->readTemplate('formtables');

my $text = $templates->expandTemplate('FORM:display:header');
$text =~ s/\$title/$this->getPath()/ge;

my $rowTemplate = $templates->expandTemplate('FORM:display:row');
my $hasAllFieldsHidden = 1;
Expand All @@ -674,21 +675,20 @@ sub renderForDisplay {
$hasAllFieldsHidden = 0;
my $row = $rowTemplate;

# Legacy; was %A_TITLE% before it was $title
# Legacy; handle special macros
$row =~ s/%A_TITLE%/\$title/g;
$row =~ s/%A_VALUE%/\$value/g; # Legacy
$row =~ s/%A_VALUE%/\$value(display)/g; # Legacy

# display => 1 gets mapped values (rather than raw)
$text .=
$fieldDef->renderForDisplay( $row, $fm->{value},
{ display => 1 } );
$text .= $fieldDef->renderForDisplay( $row, $fm->{value} );
}
}
return '' if $hasAllFieldsHidden;

$text .= $templates->expandTemplate('FORM:display:footer');
my $footer = $templates->expandTemplate('FORM:display:footer');
$footer =~ s/\$title/$this->getPath()/ge;
$text .= $footer;

# substitute remaining placeholders in footer and header
# Legacy: substitute remaining placeholders in header and footer
$text =~ s/%A_TITLE%/$this->getPath()/ge;

return $text;
Expand Down
27 changes: 12 additions & 15 deletions core/lib/Foswiki/Form/Checkbox.pm
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,22 @@ sub isMultiValued { return 1; }

sub isValueMapped { return shift->{type} =~ /\+values/; }

sub renderForDisplay {
my ( $this, $format, $value, $attrs ) = @_;
sub getDisplayValue {
my ( $this, $value ) = @_;

$this->getOptions();
return $value unless $this->isValueMapped();

if ( $attrs->{display} && $this->isValueMapped() ) {
my @vals = ();
foreach my $val ( split( /\s*,\s*/, $value ) ) {
if ( defined( $this->{valueMap}{$val} ) ) {
push @vals, $this->{valueMap}{$val};
}
else {
push @vals, $val;
}
$this->getOptions();
my @vals = ();
foreach my $val ( split( /\s*,\s*/, $value ) ) {
if ( defined( $this->{valueMap}{$val} ) ) {
push @vals, $this->{valueMap}{$val};
}
else {
push @vals, $val;
}
$value = join( ", ", @vals );
}

return $this->SUPER::renderForDisplay( $format, $value, $attrs );
return join( ", ", @vals );
}

sub renderForEdit {
Expand Down
49 changes: 34 additions & 15 deletions core/lib/Foswiki/Form/FieldDefinition.pm
Original file line number Diff line number Diff line change
Expand Up @@ -350,16 +350,27 @@ sub createMetaKeyValues {

=begin TML
---++ ObjectMethod renderForDisplay($format, $attrs) -> $html
---++ ObjectMethod renderForDisplay($format, $value, $attrs) -> $html
Render the field for display, under the control of $attrs.
The following vars in $format are expanded:
$title - title of the form field
$value - expanded to the *protected* value of the form field
The value is protected by Foswiki::Render::protectFormFieldValue.
=$attrs= interpreted are:
* =showhidden= - set to override H attribute
* =newline= - replace newlines with this (default &lt;br>)
* =bar= - replace vbar with this (default &amp;#124)
* =break= - boolean, set to hyphenate
* =protectdollar= - set to escape $
The following tokens in =$format= are expanded:
* =$title= - title of the form field
* =$value= - expanded to the *protected* value of the form field
* =$attributes= - from the field definition
* =$type= - from the field definition
* =$size= - from the field definition
* =$definingTopic= - topic in which the field is defined
=cut

sub renderForDisplay {
Expand All @@ -373,10 +384,16 @@ sub renderForDisplay {
}

require Foswiki::Render;
$value = Foswiki::Render::protectFormFieldValue( $value, $attrs );

$format =~ s/\$title/$this->{title}/g;
$format =~ s/\$value/$this->renderValueForDisplay($value)/ge;
if ( $format =~ /\$value\(display\)/ ) {
my $vd = Foswiki::Render::protectFormFieldValue(
$this->getDisplayValue($value), $attrs );
$format =~ s/\$value\(display\)/$vd/g;
}
if ( $format =~ /\$value/ ) {
my $v = Foswiki::Render::protectFormFieldValue( $value, $attrs );
$format =~ s/\$value/$v/g;
}
$format =~ s/\$name/$this->{name}/g;
$format =~ s/\$attributes/$this->{attributes}/g;
$format =~ s/\$type/$this->{type}/g;
Expand All @@ -389,18 +406,20 @@ sub renderForDisplay {

=begin TML
---++ ObjectMethod renderValueForDisplay($value) -> $html
---++ ObjectMethod getDisplayValue($value) -> $html
Render the field value for display, to be overridden by subclasses.
Default implementation passes the value unchanged.
Given a value for this form field, return the *mapped* value suitable for
display. This is used when a form field must be displayed using a different
format to the way the value is stored.
=cut
The default does nothing.
sub renderValueForDisplay {
=cut

# my ( $this, $value ) = @_;
sub getDisplayValue {
my ( $this, $value ) = @_;

return $_[1];
return $value;
}

# Debug
Expand Down
7 changes: 4 additions & 3 deletions core/lib/Foswiki/Form/Radio.pm
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,16 @@ sub getOptions {
return $vals;
}

sub renderForDisplay {
my ( $this, $format, $value, $attrs ) = @_;
sub getDisplayValue {
my ( $this, $value ) = @_;

$this->getOptions();

# SMELL: shouldn't this be in getDisplayValue?
if ( defined( $this->{valueMap}{$value} ) ) {
$value = $this->{valueMap}{$value};
}
return $this->SUPER::renderForDisplay( $format, $value, $attrs );
return $value;
}

sub renderForEdit {
Expand Down
26 changes: 12 additions & 14 deletions core/lib/Foswiki/Form/Select.pm
Original file line number Diff line number Diff line change
Expand Up @@ -110,25 +110,23 @@ sub isMultiValued { return ( shift->{type} =~ /\+multi/ ); }

sub isValueMapped { return ( shift->{type} =~ /\+values/ ); }

sub renderForDisplay {
my ( $this, $format, $value, $attrs ) = @_;
sub getDisplayValue {
my ( $this, $value ) = @_;

return $value unless $this->isValueMapped();

$this->getOptions();

if ( $attrs->{display} && $this->isValueMapped() ) {
my @vals = ();
foreach my $val ( split( /\s*,\s*/, $value ) ) {
if ( defined( $this->{valueMap}{$val} ) ) {
push @vals, $this->{valueMap}{$val};
}
else {
push @vals, $val;
}
my @vals = ();
foreach my $val ( split( /\s*,\s*/, $value ) ) {
if ( defined( $this->{valueMap}{$val} ) ) {
push @vals, $this->{valueMap}{$val};
}
else {
push @vals, $val;
}
$value = join( ", ", @vals );
}

return $this->SUPER::renderForDisplay( $format, $value, $attrs );
return join( ", ", @vals );
}

sub renderForEdit {
Expand Down
9 changes: 7 additions & 2 deletions core/lib/Foswiki/Macros/META.pm
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ sub META {
elsif ( $option eq 'formfield' ) {

# a formfield from within topic text
return $topicObject->renderFormFieldForDisplay( $params->get('name'),
'$value', $params );
return $topicObject->renderFormFieldForDisplay(
$params->get('name'),
Foswiki::isTrue( $params->{display} )
? '$value(display)'
: '$value',
$params
);
}
elsif ( $option eq 'attachments' ) {

Expand Down
13 changes: 9 additions & 4 deletions core/lib/Foswiki/Render/FormField.pm
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,19 @@ sub render {
$found = 1;
my $value = $field->{value};
$text = $default if !length($value);
$text =~ s/\$title/$title/go;
$text =~ s/\$value/$value/go;
$text =~ s/\$title/$title/g;
$text =~ s/\$value(display)//go;

$text =~ s/\$value/$value/g;
$text =~ s/\$name/$name/g;
if ( $text =~ m/\$form/ ) {

# $formname is correct. $form works but is deprecated for
# compatibility with SEARCH{format}
if ( $text =~ m/\$form(name)?/ ) {
my @defform = $formTopicObject->find('FORM');
my $form = $defform[0]; # only one form per topic
my $fname = $form->{name};
$text =~ s/\$form/$fname/g;
$text =~ s/\$form(name)?/$fname/g;
}

last; # one hit suffices
Expand Down
11 changes: 5 additions & 6 deletions core/lib/Foswiki/Search.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1334,13 +1334,13 @@ sub displayFormField {
my ( $meta, $args ) = @_;

my ( $name, @params ) = split( /,\s*/, $args );
my $displayedVal = 0; # default is to show the unmapped value
my $breakArgs = '';
my $format = '$value'; # default is to show the unmapped value
my $breakArgs = '';
if (@params) {
if ( $params[0] eq 'display' ) {

# The displayed value is required
$displayedVal = 1;
$format = '$value(display)';
shift @params;
}
$breakArgs = join( ',', @params );
Expand All @@ -1351,12 +1351,11 @@ sub displayFormField {
# can't be repaired by the options on %SEARCH. 'nomap' goes some
# way to curing the problem, but it's still not ideal :-(
return $meta->renderFormFieldForDisplay(
$name, '$value',
$name, $format,
{
break => $breakArgs,
protectdollar => 1,
showhidden => 1,
display => $displayedVal
showhidden => 1
}
);
}
Expand Down
22 changes: 14 additions & 8 deletions core/templates/formtables.tmpl
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
%{ This is a default template }%%{ These templates define the form attached at the bottom of viewed page. }%
%{ This is a default template }%

%{ Header definition for topic attachments and top table in attach screen }%
%{These templates define the form attached at the bottom of viewed page. }%

%TMPL:DEF{noal}%<noautolink>%TMPL:END%
%TMPL:DEF{nonoal}%</noautolink>%TMPL:END%

%{ Header definition for topic attachments and top table in attach screen.
The $title token will be expanded }%
%TMPL:DEF{FORM:display:header}%
<div class="foswikiForm">
<table class='foswikiFormTable' border='1' summary='%MAKETEXT{"Form data"}%'><tr><th class='foswikiFormTableHRow foswikiFirstCol' colspan='2'>%IF{"context preview" then="%TMPL:P{FORM:display:titlename}%" else="%TMPL:P{FORM:display:titlelink}%"}% </th></tr>%TMPL:END%
<table class='foswikiFormTable' border='1' summary='%MAKETEXT{"Form data"}%'><tr><th class='foswikiFormTableHRow foswikiFirstCol' colspan='2'>%TMPL:P{context="preview" then="FORM:display:titlename" else="FORM:display:titlelink"}% </th></tr>%TMPL:END%

%{ Each row }%
%TMPL:DEF{FORM:display:row}%%IF{"context preview" then="<noautolink>"}%<tr valign='top'><td class='foswikiFormTableRow foswikiFirstCol' align='right'> %A_TITLE%</td><td> %A_VALUE% </td></tr>%IF{"context preview" then="</noautolink>"}%%TMPL:END%
%{ Each row, all field tokens will be expanded }%
%TMPL:DEF{FORM:display:row}%%TMPL:P{context="preview" then="noal"}%<tr valign='top'><td class='foswikiFormTableRow foswikiFirstCol' align='right'> $title</td><td> $value(display) </td></tr>%TMPL:P{context="preview" then="nonoal"}%%TMPL:END%

%{ Footer }%
%{ Footer. The $title token will be expanded }%
%TMPL:DEF{FORM:display:footer}%</table></div>%TMPL:END%


%TMPL:DEF{FORM:display:titlelink}% %A_TITLE% %TMPL:P{FORM:display:editlink}%%TMPL:END%
%TMPL:DEF{FORM:display:titlelink}% $title %TMPL:P{FORM:display:editlink}%%TMPL:END%


%TMPL:DEF{FORM:display:titlename}%<noautolink> %A_TITLE% </noautolink>%TMPL:END%
%TMPL:DEF{FORM:display:titlename}%<noautolink> $title </noautolink>%TMPL:END%


%TMPL:DEF{FORM:display:editlink}%<span class='foswikiSmall'><a href='%SCRIPTURL{edit}%/%WEB%/%TOPIC%?t=%GMTIME{$epoch}%;action=form'>%MAKETEXT{"edit"}%</a></span>%TMPL:END%
8 changes: 7 additions & 1 deletion core/templates/foswiki.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,13 @@ Container displayed just below the topic text: }%
---------------------------------------------------
}%%TMPL:DEF{"login"}%%TMPL:P{context="can_login" then="login_actions"}%%TMPL:END%%{
---------------------------------------------------
}%%TMPL:DEF{"login_actions"}%%IF{"context authenticated" then='<strong>%MAKETEXT{"Hello [_1]!" args="[[%WIKIUSERNAME%][%SPACEOUT{%WIKINAME%}%%IF{"context isadmin" then=" (admin)"}%]]"}%</strong> %LOGOUT%' else='%LOGIN% %MAKETEXT{"or"}% [[%SYSTEMWEB%.UserRegistration][%MAKETEXT{"Register"}%]]'}%%TMPL:END%%{
}%%TMPL:DEF{"admin_logged_in"}% (admin)%TMPL:P%%{
---------------------------------------------------
}%%TMPL:DEF{"logged_in"}%<strong>%MAKETEXT{"Hello [_1]!" args="[[%WIKIUSERNAME%][%SPACEOUT{%WIKINAME%}%%TMPL:P{context="isadmin" then="admin_logged_in"}%]]"}%</strong> %LOGOUT%%TMPL:END%%{
---------------------------------------------------
}%%TMPL:DEF{"not_logged_in"}%%LOGIN% %MAKETEXT{"or"}% [[%SYSTEMWEB%.UserRegistration][%MAKETEXT{"Register"}%]]%TMPL:END%%{
---------------------------------------------------
}%%TMPL:DEF{"login_actions"}%%TMPL:P{context="authenticated" then='logged_in' else='not_logged_in'}%%TMPL:END%%{
---------------------------------------------------
app-style page layout
additional style
Expand Down
11 changes: 4 additions & 7 deletions core/templates/viewtopicactionbuttons.tmpl
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
%TMPL:DEF{"topicactionbuttons"}%%TMPL:P{"action_activatable_edit_or_create"}%%TMPL:P{"action_activatable_attach"}%%TMPL:P{"action_activatable_subscribe"}%%TMPL:P{"action_printable"}%%TMPL:P{"action_revisions"}%%TMPL:P{"action_backlinks_simple"}%%TMPL:P{"action_raw_or_view"}%%TMPL:P{"action_activatable_raw_edit"}%%TMPL:P{"activatable_more"}%%TMPL:END%

%TMPL:DEF{nowysiwyg}%;nowysiwyg=1%TMPL:END%

%TMPL:DEF{"create_topic_link"}%<span class="foswikiRequiresChangePermission"><a href='%SCRIPTURL{"edit"}%/%BASEWEB%/%BASETOPIC%?t=%GMTIME{"$epoch"}%%TMPL:P{"url_param_editaction"}%%IF{"context TinyMCEPluginEnabled" else=";nowysiwyg=1"}%' rel='nofollow' %MAKETEXT{"title='Create new topic' accesskey='c'>&Create"}%</a></span>%TMPL:END%
%TMPL:DEF{"create_topic_link"}%<span class="foswikiRequiresChangePermission"><a href='%SCRIPTURL{"edit"}%/%BASEWEB%/%BASETOPIC%?t=%GMTIME{"$epoch"}%%TMPL:P{"url_param_editaction"}%%TMPL:P{context="TinyMCEPluginEnabled" else="nowysiwyg"}%' rel='nofollow' %MAKETEXT{"title='Create new topic' accesskey='c'>&Create"}%</a></span>%TMPL:END%


%TMPL:DEF{"edit_topic_link"}%<span class="foswikiRequiresChangePermission"><a href='%SCRIPTURL{"edit"}%/%BASEWEB%/%BASETOPIC%?t=%GMTIME{"$epoch"}%%TMPL:P{"url_param_editaction"}%%IF{"context TinyMCEPluginEnabled" then="" else=";nowysiwyg=1"}%' rel='nofollow' %MAKETEXT{"title='Edit this topic text' accesskey='e'>&Edit"}%</a></span>%TMPL:END%
%TMPL:DEF{"edit_topic_link"}%<span class="foswikiRequiresChangePermission"><a href='%SCRIPTURL{"edit"}%/%BASEWEB%/%BASETOPIC%?t=%GMTIME{"$epoch"}%%TMPL:P{"url_param_editaction"}%%TMPL:P{context="TinyMCEPluginEnabled" else="nowysiwyg"}%' rel='nofollow' %MAKETEXT{"title='Edit this topic text' accesskey='e'>&Edit"}%</a></span>%TMPL:END%

%TMPL:DEF{"raw_edit_link"}%<span class="foswikiRequiresChangePermission"><a href='%SCRIPTURL{"edit"}%/%BASEWEB%/%BASETOPIC%?t=%GMTIME{"$epoch"}%%TMPL:P{"url_param_editaction"}%;nowysiwyg=1' rel='nofollow' %MAKETEXT{"title='Edit Wiki text' accesskey='w'>Edit &wiki text"}%</a></span>%TMPL:END%

Expand Down Expand Up @@ -101,11 +102,7 @@

%TMPL:DEF{"backlinks_simple"}%%TMPL:P{"backlinks_link"}%%TMPL:END%

%TMPL:DEF{"action_backlinks_simple"}%%TMPL:P{"backlinks_simple"}%%TMPL:P{"sep"}%%TMPL:END%



%TMPL:DEF{"history_rdiff"}%%TMPL:P{"history_rdiff_link"}%%TMPL:END%
%TMPL:DEF{"action_backlinks_simple"}%%TMPL:P{"backlinks_simple"}%%TMPL:P{"sep"}%%TMPL:END%:%TMPL:DEF{"history_rdiff"}%%TMPL:P{"history_rdiff_link"}%%TMPL:END%


%TMPL:DEF{"history_history"}%%TMPL:P{"history_history_link"}%%TMPL:END%
Expand Down

0 comments on commit b95b9e0

Please sign in to comment.