Crafting CSS style sheet

This is the most time consuming operation in porting any CSS template to BlogEngine. If you like your design pixel perfect - that will cost you. These are some general tips that I hope will help make job a little bit easier.

  • Move forward gradually, addressing one area at a time. Make sure your header section looks right and only then go to the post, comments, sidebar, archive etc. You don't want to go though the changes you've made in the last couple hours looking for one that totally screwed page layout or changed that color you really liked.
  • Each step verify at least in Firefox and IE. Throwing into the mix Opera and Safari would be nice, too.
  • Get a tool to work with DOM. You want to click on any element on the page and see CSS style attached to it. Firebug or Developer Toolbar will do the job.
  • First, use only style sheet that came with template. Clean it up by removing anything you not going to use. For example, if you going to use only one level in unordered list you don't need "ul li ul li" for this class. Keep it simple.
  • Start with generic html tags like headers, lists, paragraphs, images etc.
  • Once that taken care of, move through BlogEngine style sheet and take only what you'll need. Again, one block at a time and moving on only after checking changes in all browsers you going to support.

Taking advantage of asp.net

BlogEngine is typical asp.net application, you can do in your theme whatever asp.net allows you - and this is a lot!

Add core namespace to the master page to get access to BE objects:

<%@ Import Namespace="BlogEngine.Core" %>

Now you can use it anywhere in the theme if need to. For example, to get a link to home page in any language BE supports:

<a href="<%=Utils.AbsoluteWebRoot %>" rel="home"><%=Resources.labels.home %></a>

It is common for themes to implement menu where current page you are on stands out in some way. You can easily add this by creating small function in code behind that returns different class string for current page:

public string MenuClass(string menuName)
{
  if (Request.Path.Contains(menuName))
    return "class=\"current_page_item\"";
  else
    return "class=\"page_item\"";
}

And then use this function in the theme to get class for the menu element:

<li <%=MenuClass("default.aspx")%> >

If you need to show element on the page only to blogger (admin):

<% if (Page.User.Identity.IsAuthenticated){ %>
    <div>Show admin/blogger content</div>
<%} %>

This tutorial is very basic and meant just to get you started. You can go as far or as little as you wish with your design, implementing sitemap menu, adding content placeholders, user controls and more. The point is, you’ll be really hard pressed to find project that is as easy to theme as BlogEngine. If you don’t like the way your theme looks – go ahead and change it. It is that easy!

Read part one

Test-drive tutorial theme

Download tutorial theme (52.75 kb)

Signature

Cool thing about open source is sharing. You give some - you get some. When it comes to themes, there are tons of great free designs out there on the web for applications like blogs. Some of them are generic CSS templates, others specifically designed for popular open source projects like WordPress. This tutorial is about converting WordPress theme to BlogEngine, but most of it very much applied to almost any web template in the universe.

First, lets get to know what exactly we need to build BlogEngine theme. Thankfully, not a whole lot - all you really need is to create a new folder under ~/themes and add three files with few lines of code:

// site.master
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="site.master.cs" Inherits="site" %>
<html>
 <head id="Head1" runat="server"></head>
 <body>
   <form id="Form1" runat="Server">
     <asp:ContentPlaceHolder ID="cphBody" runat="server" />
   </form>
 </body>
</html>
 
// postview.ascx
<%@ Control Language="C#" AutoEventWireup="true" EnableViewState="false" Inherits="BlogEngine.Core.Web.Controls.PostViewBase" %>
<h1><a href="<%=Post.RelativeLink %>"><%=Server.HtmlEncode(Post.Title) %></a></h1>
<asp:PlaceHolder ID="BodyContent" runat="server" />
 
// commentview.ascx
<%@ Control Language="C#" EnableViewState="False" Inherits="BlogEngine.Core.Web.Controls.CommentViewBase" %>

If you run you blog now and select theme with the same name you just gave to the folder, your new "theme" will work. It won’t look pretty, but you’ll see your posts and will be able to navigate to the details page and make comments etc. just fine.

From this example you can see that only things required in the theme are couple placeholders where BlogEngine will put generated content and couple tags like "head" and "form". That means, we can pretty much take entire Html from any template, put it in the site.master, stick placeholder in the corresponding gap - and we have our custom-built theme ready to go... well, almost :)

So, lets start by downloading WP theme. As example I'll use this tutorial theme. If you open archive and look inside, there are usually lots of .php files, images and few .css files. Delete all php's - you won't need them. Instead, go to demo site and open it up in the browser, right click and select "source code". Create html page in you theme folder and copy source code there. Make another copy - you'll use it as a reference if you accidently break something . Now run your project and go to html page, make sure it looks exactly as in the demo site.

When you look at the souce code you copied, there are a lot of hard coded content in there. Make it as uncomplicated as it gets by removing all content. Also, replace all web references with local ones. You'll get something much more manageable:

<html>
<head>
 <title>Tutorial Theme</title>
 <link rel="stylesheet" href="style.css" type="text/css" media="screen" />
</head>
<body>
<div id="wrapper">
 <div id="left">
   <div id="logo"><h1><a href="#"></a></h1></div>
   <div id="navigation">
       <ul id="menu">
       <li class="current_page_item"><a href="#">Home</a></li>
       <li class="page_item"><a href="#">About</a></li>
       <li class="page_item"><a href="#">Blog Oh! Blog</a></li>
       </ul>
   </div>
   <div class="content">content</div>
 </div>
 <div id="right">Right sidebar</div>
 <div class="clear"></div>
 <div id="footer"><p><a href="#">Footer</a><br /><br /></p></div>
</div>
</body>
</html>

Now we are getting somewhere. Copy everything inside “body” tag into site.master page and replace content div with ContentPlaceHolder. Run your project and you’ll see that site looks much better. A little more patience and it will look like the picture below. In the next part I’ll show you how to reconcile CSS that comes with template and BlogEngine specific elements and we look at some tricks you can use building your custom theme.

Signature
<<  May 2008  >>
SuMoTuWeThFrSa
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567
Enhanced with Snapshots

Subscribe to Rtur.net