Tuesday, March 4, 2008

Dynamic Tree Control for Blogger

A tree control (i.e. the GUI element found in the left pane of Windows Explorer) provides a useful way of organizing a set of hierarchical items. This article will present a method to incorporate such a tree control in to Blogger blogs.

Before we proceed, let's take a look at how our final tree will look like.


Now let's see how to include a tree like the above in your blog.

Step 1
First step is to include two resources, a JavaScript file and a CSS file, in to the Blogger template. Copy the following two lines of code and insert them in to the head section of your template.


<link href='http://idssl.fileave.com/tbg/dtree.css' rel='StyleSheet' type='text/css'/>
<script src='http://idssl.fileave.com/tbg/dtree.js' type='text/javascript'/>

You can copy it immediately under the <head> tag as shown in the figure below. (These two resources are hosted on a free file hosting service. Don't worry, there is no tracking code or any thing of that sort there.)


Save your template and exit Edit HTML mode.

Step 2
Next step is to write the code necessary to create the tree. I will first present a sample code and then explain it.

<div class="dtree">

<p><a href="javascript: d.openAll();">open all</a> | <a href="javascript: d.closeAll();">close all</a></p>

<script type="text/javascript">
<!--

d = new dTree('d');

d.add(0,-1,'Favorite Articles');
d.add(1,0,'3 Columns');
d.add(2,1,'3 Columns Explained','http://bguide.blogspot.com/2008/02/three-column-templates-explained.html');
d.add(3,1,'Skeleton of a Template','http://...');
d.add(4,1,'Adding a Third Column','http://...');
d.add(5,1,'Layout Wire Frame Editor','http://...');
d.add(6,1,'3 Columns with LR Sidebars','http://...');
d.add(7,0,'Blog Traffic');
d.add(8,7,'How to Monitor Visitors','http://...');
d.add(9,7,'Feedjit','http://...');
d.add(10,7,'Exclude Your Traffic from GA','http://...');
d.add(11,0,'Customizations');
d.add(12,11,'Add a Custom CSS class','http://...');
d.add(13,11,'Related Posts Table','http://...');
d.add(14,11,'Limit Widgets to Specific Pages','http://...');
d.add(15,1,'3 Column Step by Step Guides','http://...');

document.write(d);

//-->
</script>
</div>

The first line defines a generic div element and applies the class dtree in to that. This CSS class is defined in the dtree.css file which we included in Step 1 above. Next line inserts two buttons (in fact, links) to Expand and Collapse all the nodes in the tree. Thirdly, you find a Javascript enclosed within a <script> element. The first line in the script, d = new dTree('d'), creates a new tree object to which we can insert nodes. The following lines which start with d.add(...) are the statements that insert all the nodes in to the tree. Then the last line of the script, document.write(d), renders the created tree on to the browser.

The add() function of the dTree takes in several parameters. The first 3 parameters are required (i.e. you must provide values for them) and the other parameters are optional. The above example uses the following 4 parameters.
  • Node ID - A unique numerical ID number. Assign a sequentially increasing number to each new node.
  • Parent Node ID - If you want to create a child node, then the ID of the parent node should be given here. This will be '-1' for the root node. (See the first d.add(...) line in the example)
  • Node Name - A textual description for the node
  • Node URL - If you want the node to link to some resource (e.g. an HTML page or some online image), provide the URL of the that resource as the 4th parameter.
For a full description of the add() function see here.

You don't have to provide consecutive (or adjacent) IDs for all the child nodes at a given level. For example, node 15 of the above snippet is attached as a child of node 1 called '3 Columns'. You can have the nodes anywhere as long as you maintain the uniqueness of the node IDs and provide the correct parent node IDs.

Step 3
Once you finish adding the nodes, copy the entire code starting from the <div class="dtree"> to the corresponding </div>. Then insert the entire code using an 'Add HTML/Javascript Element' widget of the 'Add Page Element' window.

Place the widget where you want the tree to appear. Save the template and view your blog. You will have the tree sitting nicely in your blog!

If the entry names are longer than the width of the widget, it will get an automatic horizontal scroll bar. To stop having to scroll horizontally give names that fit withing the width of the containing sidebar (or whatever location). Let me know what you think of it and also if you come across any problems when installing it.

Acknowledgment:
I have used the free tree control offered by Geir Landrö at Destroydrop. The original files have been slightly modified to make them available online, to be used inside Blogger.

Note:
You are free to use the resources used in Step 1. But if you do use, please link to this article from your blog.

Saturday, March 1, 2008

Donations

Thank you very much for your consideration. Please click on the Donate button below to make your donation.







Thanks,
ids.

Limiting Widgets to Specific Pages

This article will explain how you can limit the appearance of a widget to a specific type of page on your blog.

For the purpose of explanation, I will use a widget from Feedjit, which we can use to guide visitors to popular pages on a blog. This particular widget will display the other pages read by previous visitors to a given page in your blog. It works similar to the Amazon’s “Customers Who Bought This Item Also Bought” recommendation. It is clear that this type of a widget is meaningless on your blog's homepage unless otherwise you have limited the number of posts on the homepage to one.

So let's see how we can limit this widget to only individual post pages or item pages. We use the conditional element or the <b:if>...</b:if> element for this purpose. The conditional element allows us to instruct Blogger to display some other element only if a given condition is true. Here's the general format of this element.

<b:if cond="the condition to be checked" >

<!-- The element to be conditionally included goes here -->

</b:if>


The opening tag of the <b:if> element has an attribute called cond, meaning condition. The value that goes in to this attribute is a Boolean expression (don't worry, in simple English that means a check that turns out to be either true or false). If this expression evaluates to 'true', the element inside the <b:if> element will be included in the page rendered on a browser.

First step, obviously, is to add the widget as you would normally do. This widget will be installed using an HTML/Javascript element from the Layout editor. (Tip: Provide a Title for this widget. For example, I gave the title "Popular Pages" to the widget I used for this article. This is useful to locate the installed widget in our next step. You can remove the title later if you don't want it.)

Then we switch to the Edit HTML mode of the Layout editor and click on the Expand Widget Templates check box. (Always remember to back up your template, to restore if something goes wrong). Don't be overwhelmed by the expanded template; it's just like the unexpanded one but with more HTML. Now do a Find (Ctrl+F) for the Title we gave in the above step, inside the expanded template and locate the widget you just added.

This widget will be included as a <b:widget> element in the template. The widget has two parts; its title and the content. Locate the content part of the widget element and encompass that whole section inside a <b:if>...</b:if> element.

The important thing is the condition we provide here. We give the condition as cond='data:blog.pageType == "item"' instructing Blogger to display the content of this widget only if the pageType is equal to 'item'.

The final code snippet will look as follows.

<b:widget id='HTML10' locked='false' title='Popular Pages' type='HTML'>
<b:includable id='main'>
<!-- only display title if it's non-empty -->
<b:if cond='data:title != ""'>
<h2 class='title'><data:title/></h2>
</b:if>
<b:if cond='data:blog.pageType == "item"'>
<div class='widget-content'>
<data:content/>
</div>
</b:if>
<b:include name='quickedit'/>
</b:includable>
</b:widget>


See the figure below for an illustration of this step.


The blue rectangle in the above figure shows the content section of the widget. The two red rectangles show the <b:if> element that we place enclosing the content section.

The same method can be used with the pageType set to 'index', to limit a widget to your blog's homepage.