The degree of one's emotions varies inversely with one's knowledge of the facts: the less you know the hotter you get. —Bertrand Russell

Using a Sub-Blog to hold common code

Posted by Justin under Technoblogging View recent posts with the tag Technoblogging on Technorati 

I love PHP. My server has generously offered to execute any PHP files I put on it (and that’s the good kind of execution, mind you). Not sure what I mean? If you have a MovableType site, you can probably use PHP. Here’s a great trick to make it easier to update your bookroll, links, common header, etc.

You can use MT’s multiple blog feature to create pages that have content that is pulled into other pages. For example, I’m no longer using Blogrolling.com for my blogroll; it’s stored in a file called blogroll.php (my sub-blog for such things is called cms, for content management system).

Why in the world, you ask, would I want to add this level of complexity to my website? Well, one reason is that it’s a pain to edit an enormous template in MT and try to find the one line of code you want to manipulate. So here’s what I do: Create the file as an MT entry and use PHP to include it in all the pages I want it to show up in. Here are step by step instructions:

1. Create a new blog. I’ll call it “cms” for “content management system.”
2. In Core Configuration, set the various path settings to (whatever).com/cms or public_html/cms (depending on your server configuration)
3. In Preferences (under Weblog Config), under “Default Text Formatting for new entries”, select None. This is important - if you set it to convert line breaks, you’ll have tons of extra
tags.
4. Under “File extension for archive files:” enter php (no period)
5. Leave all the rest of the boxes unchecked. You won’t need any of them.
6. Under “Archiving”, delete/uncheck all but the Individual archive. In the Archive File Template box, enter <$MTEntryTitle$>.php - this will save your entries in an easy-to-remember format. They must be in PHP for the server to pull them dynamically (or rather, so they can pull other files dynamically).
7. Delete all the templates except Main Index and Individual Archive. You won’t need the rest.
8. In the Main Index template (which you probably will never need), enter the following (changing the obvious parts to reflect your domain):





4. Make sure your main blog outputs PHP files, not HTML. There are two places to check: In each of your templates, make sure the output file ends in PHP; and in the “Preferences” section of Weblog Config, there’s a box to type the extension you want your pages to have. Make sure this box says PHP.
5. Use your FTP app to delete any old HTML files that have the same name as existing files if you’re just now switching to PHP, because, when you’re server is given the choice between index.html (the old file) and index.php (the new file), you probably won’t get the right one displayed.
6. Republish your blog. Viola! Oh, wait, something probably went wrong. Troubleshoot, and eventually you’ll have a voila! moment.
Enjoy. Email me if you want help.

By the way, in case you’re wondering how I made this entry display all those < and >’s instead of trying to render them as HTML - I just copied & pasted the text of this entry into the comment box, selected all, and hit the “decode” button, then recopied it into the blog entry. Cool, huh?

Now comes the part where this makes your life easier. Whenever you want to edit your blogroll or other elements you include this way, simply go to the cms blog and select “edit entries” (this is even easier if you use W.Bloggar - just select CMS in the drop-down blog selection box). Edit the one you want and hit save. That’s it - blogroll updated with no republishing. Since this file is called in freshly every time a page that uses it is loaded, there’s no need to update any other pages.

5 Responses to “Using a Sub-Blog to hold common code”


You have discovered the power of web programming with one very powerful but simle function…the include! It gets even more fun when you develop your own components. With the way you are going it looks like you might start making your own stuff.

1

Justin-
I would find a good tutorial/reference online. Here are the two I use most frequently:
W3Schools: This is a site with good high-level tutorials and LOTS of examples (which I find most helpful).
DevGuru: This is more of a reference site which I use for the specifics of ASP, VBScript, and JavaScript.

I haven’t done much PHP and for my Java apps I use a big, fat JSP book plus the Java APIs.

5

Leave a Reply

You can track future comments on this post via this RSS feed. You can trackback this post by pinging this URL. Allowed HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Shrink comment box | Expand comment box



Get RC Via Email



Put Obama at 1600 PA Ave

Obama fundraising meter

    Tagegories

    Browse by category:

    Explore by tag:

    Recent Posts

  • Blogroll

  • Archives


    Use the calendar below to find posts by day (mouseover a day on the calendar to see all posts from that day). If you're looking for a specific post, it's much faster to use the search box above.

    February 2004
    S M T W T F S
    « Jan   Mar »
    1234567
    891011121314
    15161718192021
    22232425262728
    29  

      Recent Comments


      Creative Commons License
      We aren't very into all that copyright stuff. Creative Commons licenses are better, so RC is licensed under this one.
      Quote Radical Congruency at will. Inbound links are appreciated, and required for direct quotations.