Update: I tweaked the code after posting this. It’s been updated to reflect my working solution!
One of the great things about Semantic MediaWiki (SMW) is the auto-generated fact box at the bottom of any wiki page that has any values for semantic properties.
It’s also relatively easy to create a list of all defined values for a given property. In fact, this idea uses this template as a basis for what follows.
However, I found that it’s not very intuitive to navigate to a list of all values of a property and see what other articles have that same property value. In order to see a list of all articles that have a particular property value you’d have to navigate to an existing page with that property and then perform a semantic search from there. Or know that there’s a page called Special:SearchByProperty that you could use.
I wasn’t satisfied with that, so I made a fancy list that’s actually really easy to implement and can be used across an entire SMW for other property values too.
The result is a list of all values for a given property that provide hyperlinks to a list of all wiki articles with that property value. It uses the Special:SearchByProperty page for displaying results.
First, I created a template called Property Values Columns. You can use it like so:
{{Property Values Columns|<em>Name of Property Here</em>}}
Here’s the contents of the “Property Values Columns” template1:
<noinclude>Use this template to list all the unique values for a given property. == Template code == </noinclude><includeonly> *{{#arraymap: {{#arraydefine: valued | {{#ask: [[{{{1|}}}::+]] | mainlabel =- | headers = hide |? {{{1|}}} | limit = 10000|searchlabel= }} |,| print=list, sort=asc, unique}} |,|@@|[[Special:SearchByProperty/{{Space|{{{1}}}/@@}}{{!}}@@]]| <ul> <li>}}</li> </ul> </includeonly>
This is what it does.
1. Gets a comma separated list of property values for whatever property you’ve requested.
2. Parses the name of the property and each unique value to build a URL.
Example: http://wikiname.com/wiki/index.php/Special:SearchByProperty/Property Name/Property Value
3. The secret sauce is another template called ‘Space’ which uses the #replace function to replace all spaces ” ” with the “-20” syntax used in the Special:SearchByProperty query.
3a. The “Space” template contains the following:
{{#replace:{{{1}}}| |-20}}
4. Return the results as a series of list items.
The result is an list of all unique property values that when clicked will query the wiki for all pages containing that property value. You can then use some custom styling to format the results. In the example below I wrapped the template in a div creating two columns.2
<div class="twoColumns">{{Property Values Columns|Accounting Unit}}</div>
Pretty cool huh?