Using Tags In Umbraco

By leen3o on Dec 22 2009 | 0 Comments

On one of my current projects we needed to create a custom article feature which revolved around using the TAGS DataType which ships with Umbraco, in this little blog post I’ll show you how to use the TAGS DataType to create your own mini article system and give you some useful XSLT snippets to save you the time I had to take to get it working.

TAGS are a way to to categorise your pages/content so you can pull through certain content based on a TAG, you can also have TAG groups so within one installation you could have the same TAG names but associated to different groups.  For example you could have a TAG group called ‘FAQ’ and ‘News’ but both groups have a tag called ‘general’ – Using TAG groups mean you could pull through all content tagged with General but differentiate between FAQ and News items.

For this blog post we just want to create little article system, so lets create a new TAG group and call it ‘Articles’ go to Developer > Data Types In your Umbraco UI and create a new DataType called ‘Article Tags’ like below

Capture

Notice the render control is ‘Tags’, and the Tag Group is ‘Articles’ – We will use this group name in the XSLT so we know we are pulling through the correct group in case we decide to add another group at a later date.  Once created you should see your ‘Article Tags’ DataType in the list of DataTypes

Capture

Now we have this DataType created we can now add this to any document type so the users can add TAGS to the created page – For this example I’ll create a DocType called ‘Article’ and added some of the usual properties I add as well as the ‘ArticleTags’ DataType we created above.

Capture

Now I have this DocType the users can add tags to their Documents in the Umbraco UI (This is how it looks in the UI)

Capture

Great, now we have a TAGGING system in place we need to look at how we pull out the data using XSLT in the front end – Well firstly Umbraco comes with a TAGS Library already built in, its what the original Blog4Umbraco uses to categorise the blogs (And I suspect the new Blog will also use it)

So we just need to implement the XSLT tagsLib: Extension that Blog4Umbraco uses – This is SO simple, all you need to do is open up the XSLT Extension config file found in the ‘config’ folder in the root of your Umbraco install

Capture

And just add the following line in the file and save it

  1. <ext assembly="/bin/umbraco.editorControls" type="umbraco.editorControls.tags.library" alias="tagsLib" />

What this does is adds the tagsLib namespace into your XSLT files when you create a new XSLT file in the Umbraco backend, so you can use all the TAGS helper methods

  1. xmlns:tagsLib="urn:tagsLib"

Now we have this in place lets look at the XSLT that we can use – All the below is taken from either the blog4Umbraco package or the WIKI

Get A List Of All Tags

To get a list of all tags we can use the tagsLib:getAllTagsInGroup() method – All we need to do is pass in the group name, which we know is ‘Articles’ then loop through the results like so

  1. <ul>
  2.     <xsl:for-each select="tagsLib:getAllTagsInGroup('Articles')/tags/tag">
  3.             <li>
  4.                 <a href="{umbraco.library:NiceUrl(@id)}?filterby={current()}"><xsl:value-of select="current()"/></a> (<xsl:value-of select="@nodesTagged"/>)
  5.             </li>
  6.     </xsl:for-each>
  7. </ul>

This example uses the current page and appends the tag name as a QueryString value of ‘filterby’ – This is so we can select Articles that are tagged with a specific TAG.

Getting Documents TAGGED With A Specific TAG

You can either use the querystring method above, or I create a macro where I pass in the TAG on a page by page basis so I can even insert the list of articles into the editor where ever I want them to appear

  1. <!-- Here I pass in the TAG name to filter via a property on the macro -->
  2. <xsl:variable name="filter" select="/macro/tag"/>
  3. <!-- I use a content picker to choose the parent node for all my articles -->
  4. <xsl:variable name="source" select="/macro/source"/>
  5. <xsl:for-each select="umbraco.library:GetXmlNodeById(number($source))/descendant::node [@nodeTypeAlias = 'Article' and contains(Exslt.ExsltStrings:lowercase(./data [@alias='ArticleTags']), Exslt.ExsltStrings:lowercase(string($filter)))]">
  6.   <xsl:sort select="@createDate" order="descending" />
  7.     <div class="article">
  8.     <h4><a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName"/></a></h4>
  9.     <p class="readmore"><a href="{umbraco.library:NiceUrl(@id)}">Read Full Article</a></p>
  10.     </div>
  11. </xsl:for-each>

This example will spin through all the articles that have a TAG that I pass into the select statement via the $filter variable.

Creating A Tag Cloud

This is taken from the WIKI and I haven’t used it myself yet – But its what most people want when they start using TAGS and is seen to be a very Web 2.0 thing ;)

  1. <xsl:variable name="TagFactor" select="6 div Exslt.ExsltMath:max(tagsLib:getAllTagsInGroup('Articles')/tags/tag/@nodesTagged)"/>
  2. <div class="tags">
  3.   <xsl:for-each select="tagsLib:getAllTagsInGroup('Articles')/tags/tag">
  4.     <a class="tag{round($TagFactor * @nodesTagged)}x" href="/?tag={.}">
  5.       <xsl:value-of select="."/>
  6.     </a>
  7.   </xsl:for-each>
  8. </div>

Then all you need to do is add the styles to your stylesheet to control the size of the text

/* Tag Styles */
.tags { line-height: 150%; }
.tag6x { text-decoration: none; font-size: 22px; }
.tag5x { text-decoration: none; font-size: 19px; }
.tag4x { text-decoration: none; font-size: 16px; }
.tag3x { text-decoration: none; font-size: 13px; }
.tag2x { text-decoration: none; font-size: 10px; }
.tag1x { text-decoration: none; font-size: 7px; }
/* End Tag Styles */

I hope this post will be of some use to someone .. lol ..

Post info

Tags: ,
Categories: Umbraco

Comments