The new version of Blogger includes
tags. They call them
Labels. Hosted accounts (BlogSpot) get templates which display a list of tags for quick navigation. This feature is not available to FTP accounts. This post is a hack to create a user control in ASP.NET that displays a Label List. Before I do, lets breakdown how labels look on the file system.
- Labels reside in a folder. By default that folder is called labels.
- Every label gets one file.
- The label file name is the same as the label name plus extension.
- Inside each file is a copy of each blog with that label.
- Blogger places the class name of blogger-labels before the Labels text.
For example, on this site I have a label called
Javascript.
http://digitalcolony.com/labels/Javascript.aspx
It resides in the labels folder just outside the blog root. The name of the label is Javascript. The name of the file is Javascript.aspx. As of this writing there is 1 post and therefore 1 instance of
blogger-labels in the View Source.
With the above facts laid out, this is an overview of how to create your own Label List control.
- Using server-side code to handle Files and Directories, open the labels folder.
- Collect each file name (minus the file extension).
- Open each file and count how many times blogger -labels appears
- Write label and count to screen.
Here is my .NET code for the Blogger Label List control.
On the .ASPX page that the control will be on add this line to the top to register the control.
<%@ Register Src="~/LabelList.ascx" TagName="LabelList" TagPrefix="dc" %>
And the control on the page gets placed with this line.
<dc:LabelList ID="LabelList1" runat="server" Folder="labels" DisplayCount="true" />
And here is the code for the LabelList.ascx user control.
using System;
using System.Web;
using System.IO;
using System.Text;
public partial class LabelList : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
string rootDir = Server.MapPath("~");
string labelDirectory = rootDir + folder;
StringBuilder sb = new StringBuilder();
if(Directory.Exists(labelDirectory)){
string[] fileList = Directory.GetFiles(labelDirectory,"*.aspx");
string labelName;
string labelCount;
foreach (string file in fileList)
{
FileInfo fi = new FileInfo(file);
labelName = fi.Name.Replace(fi.Extension, "");
if (displayCount)
{
StreamReader sr = new StreamReader(fi.FullName);
string webPage = sr.ReadToEnd();
sr.Close();
int webPageLen = webPage.Length;
webPage = webPage.Replace("bl0gger-labels", "bl0gger-labelsX");
int iLabelCount = webPage.Length - webPageLen;
labelCount = "(" + iLabelCount.ToString() + ")";
}
else
{
labelCount = "";
}
folder = folder.Replace("\\","/");
if (folder.Substring(folder.Length - 1, 1) == "/")
folder = folder.Substring(0, folder.Length - 1);
sb.Append("<a href=\"" + folder + "/" + fi.Name + "\">" +
labelName + labelCount + "</a><br/>");
}
}
else {
sb.Append("No labels in folder [" + labelDirectory + "] defined.");
}
Response.Write(sb.ToString());
}
private string folder;
public string Folder
{
get { return folder; }
set {
if (value.Substring(0, 1) == "/")
folder = value;
else
folder = "/" + value;
folder = folder.Replace("/","\\");
}
}
private bool displayCount;
public bool DisplayCount
{
get { return displayCount; }
set { displayCount = value; }
}
}
Note that the code above misspells
blogger-labels as to not confuse the count of the Label control that I'm using for this site. If you cut and paste the code, fix that spelling.
PHP and ASP coders should be able to hack up something similar using the above code as a framework.
UPDATE (Feb 2007): I created a version of
Blogger Label Lists using Classic ASP.
Labels: ASP.NET, Blogger