18 Sep 09

Using tags to make powerful apps

Over the course of the last eight months or so I’ve been working quite diligently on the DekiScript “modules” that power WhoRunsGov.com.  I have learned quite a lot about implementing largly DekiScript powered projects and I am going to share some examples of how I used tags to develop very efficient reusable DekiScript templates.

The Use Case

WhoRunsGov.com is a user generated site focused on political profiles from all facets of the government.  With well over 700 profiles they are rapidly growing their content base and have successfully utilized DekiScript to offer a fluid and intuitive form of navigation.  Their solution includes a large number of “browse by” pages that display a list political profiles based on specified tags.   To make this more challenging there are about 150 different browse by pages so  I needed to come up with a solution that was both flexible and manageable.

The Solution

To solve this issue I created one centralized DekiScript template called ProfileList.  The ProfileList template is quite complex and is responsible for:

  • selecting the profiles
  • retrieving the profile content
  • identifying the most recently uploaded profile image
  • resizing it to fit the profile

In addition to these functions I also gave the template a number of different parameters.

  • $searchquery
  • $sortby
  • $limit

Template DekiScript

By adding these parameters I was able to make this template flexible enough to be called from all 150 browse by pages.  You can see that the code below uses the parameters to build a uri for the search API.   Using the search API is the fastest and most resource friendly approach to retrieving a set of pages.  Also, because lucene is so powerful you can filter the results with extreme control.

<h1>Template:ProfileList</h1>
{{
var qsortby = $sortby ?? '-tag:id-*';
var qlimit = $limit ?? 100; //

var results = web.xml(uri.build(site.api, [ 'site', 'search' ], { limit: qlimit, sortby: qsortby, format: 'search', q: $searchquery }), _, _, 900);
}}

‘Browse By’ DekiScript

When calling this template I simply passed in the $searchquery and the page displayed the appropriate profiles.

{{
profilelist{searchquery:'tag:"Governor" AND tag:id-*'}
}}

As you can see this searchquery is identifying pages that are tagged with ‘Governor’ and pages that are tagged with a tag that starts with ‘id-’.    Here are some more examples of how I used the ProfileList template for more advanced searches.

{{
profilelist{searchquery:'(tag:"administration official" OR tag:"obama administration official") AND tag:[id-a* TO id-i*]'}
}}

This query returns all pages tagged with ‘administration official’  or ‘obama administration official’ that are between A and I.

{{
profilelist{searchquery:'tag:"House member" AND tag:democrat* AND tag:congress AND tag:id-*'}
}}

This query returns all profiles that are tagged with ‘House member’, ‘congress’ and that also have a tag that starts with ‘democrat’.

Thanks for reading,

Damien Howley
@DamienH

One Response to “Using tags to make powerful apps”

  1. travellingwithoutmoving responds:

    @DamienH
    I’m trying to use the above example on a mindtouch enterprise instance to create a template for generating search queries. I’ve copied the following into the template:

    Template:Search
    {{ var qsortby = $sortby ?? ‘-tag:id-*’; var qlimit = $limit ?? 100; // var results = web.xml(uri.build(site.api, [ 'site', 'search' ], { limit: qlimit, sortby: qsortby, format: ’search’, q: $searchquery }), _, _, 900); }}

    and I’m using the following in the script on the page where I am trying to auto-generate a link to a list of todo items:

    {{ search{searchquery:”#todo”} }}

    Not sure if I’ve misunderstood, but shouldn’t the output be a URL link to a search page? I know I could use the search ’subscribe to these results’ to create a rss feed, but I was trying this way to try and get my head into this scripting. I think I need to modify the var definitions in the template, but not sure how… Help?!

Leave a Reply