https://bugzilla.wikimedia.org/show_bug.cgi?id=22701

           Summary: Add {{#wrap ...}} parser function
           Product: MediaWiki extensions
           Version: any
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: Normal
         Component: ParserFunctions
        AssignedTo: wikibugs-l@lists.wikimedia.org
        ReportedBy: j...@johncardinal.com


A common pattern in en.wikipedia.org templates is using #if to determine if a
parameter is non-empty before wrapping the value with a prefix and/or suffix
and emitting it. Here's one example from the Citation/core template:

{{#if: {{{format|}}} ({{{format}}})}}

In the example above, if the format parameter is not empty, the template emits
a space and an open paren (a prefix), the value of the format parameter, and a
closing paren (the suffix). This could be replaced by a new #wrap function that
accepts three parameters: prefix, value, and suffix. 

{{#wrap: (|{{{format|}}}|)}}

If value is empty, there is no output. If value is not-empty, the function
emits the concatenation of the prefix, value, and suffix. prefix or suffix may
be empty; if both are empty, the function should work, but using it is
unnecessary in that case. Also, if the suffix parameter was omitted, the suffix
should default to the empty string. Omitting the prefix or value parameter is
invalid.

Building the test-for-empty into #wrap simplifies the code and reduces the
parameter references by one. I suspect that's a marginal improvement, but this
pattern is very common in templates and a small saving in lots of places would
add up. In template Citation/core alone there are probably dozens of places
where #wrap could replace #if.

There are more involved use cases that follow this pattern but reference
multiple parameters in the #if test. In those cases, the syntax load is
considerable as there are either concatenated or nested parameter references. A
#wrap function would reduce the source code, and as the number of parameters
used increases, the reduction gets closer and closer to 50% per call. In the
long run, that may be a better reason to implement #wrap than a performance
improvement.

I am not wedded to the name, so if someone suggested a better one that would be
fine with me.

-- 
Configure bugmail: https://bugzilla.wikimedia.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.
You are on the CC list for the bug.

_______________________________________________
Wikibugs-l mailing list
Wikibugs-l@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/wikibugs-l

Reply via email to