My studying notebook

Showing posts with label Tip. Show all posts
Showing posts with label Tip. Show all posts

2009/11/07

Rearrange Blogger Template Related Post & Add New Features

11/07/2009 10:42:00 PM Posted by Unknown , 1 comment

Today i spent few time to modify Blogger template like:

  • Rearrange related posts code.
  • prettyprint code syntax highlight.
  • Adding FancyBox lightBox and modify new.

I will keep on arranging my Blogger template and help it can more friendly. There had three parts on the above list. Second list is google code pretttify. I change my code syntax from syntaxhighligher to google-code-prettify. Google-code-prettify is using easier than syntaxhighlighter. You can just include two file(prettify.js & prettify.css) add function to execute prettify. Done!! Third list is FancyBox lightBox jQuery plugin. If you are familiar with jQuery. I think it's easy to add that plugin to your web page. About first list is what i want to share how do i arrange the Related Posts.

Related Posts

Until now, Blogger haven't support related posts in your blogger template. If you want to add that feature, you have to modify some template code and add few javascript function to do that.

Scenario

How does related posts work? The scenario is easy to understand. When you want to write new post, you will add Labels for this post on the bottom of Blogger editor. You add tags for the post you wrote. When web user visit your blog, related posts feature will list other relation posts following post content by specific tag. (you have three types can show related posts on your blog depending Blogger template pageType: 'item', 'archive', or 'index').[1]

<b:if cond='data:blog.pageType == &quot;item&quot;'>
<script expr:src='&quot;/feeds/posts/default/-/&quot; + data:label.name + &quot;?alt=json-in-script&amp;callback=RelatedLabels&amp;max-results=10&quot;'
type='text/javascript'>
</script>
</b:if>

I want to show related posts in user view specific page. So, i assign pageType is 'item'. As you can see. you can receive specific tag post content by callback function using Google Data Protocol[2]. Following is complete code to get all tage you add.

<b:loop values='data:post.labels' var='label'>
<!-- Fixed for Related Posts -->
<b:if cond='data:blog.pageType == &quot;item&quot;'>
<script expr:src='&quot;/feeds/posts/default/-/&quot; + data:label.name + &quot;?alt=json-in-script&amp;callback=RelatedLabels&amp;max-results=10&quot;'
type='text/javascript'>
</script>
</b:if>
</b:loop>

Finial, you need to render those data to web page. You can have simple function to do that like this.

After solving how receive data from Blogger template Layout data and render to web page. The key point of Related posts is parse data you need.

We receive Blogger template Layout data by callback function and return JSON data. The render data include post url, post title and post date (depending what you need). It's better to keep value in an object. Object is easier to understand and manipulate.

function RelatedLabels(json) {
var entries = json.feed.entry;
for (var key in entries) {
if (entries[key]) {
var item = {};
item.title = entries[key].title.$t;
item.url = entries[key].link[4].href;
item.date = entries[key].published.$t.substr(0, 10);
relatedPosts.push(item);
}
}
}

You might add multiple tags in different posts, and therefore might receive the same post in different tag by loop. There are many solutions to solve duplicate post in an object.

I push related post in one object. We can extend Array.prototype.push() function to detect should we add this post to our object. If you are familiar with Javascript syntax. It's simple to manipulate DOM using Javascript like document.write(). If you concern web page performance, you should decrease manipulate DOM directly[3]. Following is complete code.

var relatedPosts = [];
relatedPosts.push = function (data) {
var _push = true;
var length = this.length;
for (var i = 0; i < length; i++) {
if (this[i].url == data.url) _push = false;
}
if (_push) return Array.prototype.push.call(this, data);
else return this;
}

function RelatedLabels(json) {
var entries = json.feed.entry;
for (var key in entries) {
if (entries[key]) {
var item = {};
item.title = entries[key].title.$t;
item.url = entries[key].link[4].href;
item.date = entries[key].published.$t.substr(0, 10);
relatedPosts.push(item);
}
}
}

function ShowRelatedPosts(PostUrl) {
var count = 0;
var eleLi, link, date, fragment = document.createDocumentFragment();
for (var key in relatedPosts) {
if (typeof(relatedPosts[key].url) != 'undefined' && relatedPosts[key].url != PostUrl && count < 5) {
eleLi = document.createElement('li');
link = document.createElement('a');
date = document.createTextNode(' (' + relatedPosts[key].date + ')');
link.setAttribute('href', relatedPosts[key].url);
link.innerHTML = relatedPosts[key].title;
eleLi.appendChild(link);
eleLi.appendChild(date);
fragment.appendChild(eleLi);

count++;
}
}
var eleOl = document.createElement('ol');
eleOl.appendChild(fragment);
document.getElementById('relatedPosts').appendChild(eleOl);
}
reference

2008/10/03

Tip: Notepad++ Add ASP.NET syntax highlight

10/03/2008 10:02:00 PM Posted by Unknown , 3 comments
Notepad++ is a free source code editor and Notepad replacement that supports several languages. Running in the MS Windows environment, its use is governed by GPL Licence. Syntax Highlighting and Syntax Folding is one feature of Notepad++ and it make you read the code easy. It supported many language for example: C#, javascript, XML, HTML etc. If you are a ASP.NET developer and you would find Notepad++ isn't support ASP.NET syntax (aspx, config ect.) But, it is easy to add asp.net syntax highlight by few step.

1. Notepad++ menu bar, click "settings" / "Styler Configurator".
2. In Setting dialog, Under "Language", select "XML" item.
3. Add user defined extension textbox, type in "aspx config master .." (ASP.NET support file extension and base XML well form).
4.Click "Save & Close".
5.Try it.


Ps. Notepad++(link)

2008/09/15

Tip: Visual Studio 2005 Server Explorer Crash

9/15/2008 08:29:00 PM Posted by Unknown , 1 comment

Today. When I decide to use Visual Studio 2005. I find that I cannot open Server Explorer and View the Data Base in Visual Studio 2005 Toolbar View\Server Explorer. And it doesn’t fix the error in Toolbar Window\Reset Window Layout.


And What can i do? After my research, i use the Devenv Command Line Switches to solve this problem and run “devenv /setup” in Visual Studio 2005 Command Prompt.

2008/09/05

Tip:How append different master page by browser

9/05/2008 08:03:00 PM Posted by Unknown , No comments
We know that we can use master page to reate a consistent layout for the pages in your application. When you assign a master page to the cotent page, you can modify page directive to append different browser.

Usually we assing one master page to cotent page. Now we assign two master page to same content page. And modify MasterPageFile directive

For example:
<%@ Page Language="C#" Mozilla:MasterPageFile="~/FFMasterPage.master"
ie:MasterPageFile="~/IEMasterPage.master" AutoEventWireup="true"
CodeFile="DefaultContent.aspx.cs" Inherits="DefaultContent" %>

2008/08/27

Tip: How To Get Exception Stack in MOSS

8/27/2008 08:38:00 PM Posted by Unknown , 1 comment
Sometime we might get error message when we run moss pages or webparts of we add.
How could we to get more debug messages. We can modify config setting to enable this feature.





Step
  1. Open the Web Application Web.config file
    C:\Inetpub\wwwroot\wss\VirtualDirectories\{Web:Port}
  2. Find the SafeMode section and modify CallStack="ture"
  3. Find the customErrors section and modify mode="Off"
  4. IISReset
We can get Exception Stack when moss page occur error next time.