Is it possible to do basic transformation of text in a MediaWiki template

mediawikimediawiki-templates

I have created a template on my MediaWiki wiki which shortcuts linking to some frequently used book titles. One of the parameters controls whether the shortcut template points at my wiki or another one, via a switch case. I'd like to add another option which points to a non-wiki website, where a page for the book "Book Title" has a URL ending in "/book-title/".

To avoid having to specify alternate text or the full link in every individual case, I'd like to transform the title of the book (which is already passed to the template as a parameter) into lower case and replace spaces in the title with dashes, so it can just be inserted at the end of the URL.

Is this possible? How would you do that? I've tried searching the MediaWiki help pages for templates but haven't found anything that seems to do this.

Best Answer

For lower case use {{lc:...}} parser function.

For replacement in the string, use {{#replace:(string)|<nowiki> </nowiki>|-}}. Note that the space as parser function parametre has to be wrapped with <nowiki></nowiki>.

Example: {{#replace:{{lc:{{{title|}}}}}|<nowiki> </nowiki>|-}}.

(Per Guybrush McKenzie's comment) This requires enabling in LocalSettings.php the extension ParserFunctions, bundled with MediaWiki, and additionally enabling string functions of that extension:

wfLoadExtension( 'ParserFunctions' );
$wgPFEnableStringFunctions = true;