6

Sidebar Widgets

 1 year ago
source link: https://soffes.com/blog/sidebar-widgets
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Sidebar Widgets

Posted on September 8, 2007

Today I've been messing with my sidebar a little more. I really like the new Digg Widgets, but I was having trouble styling them the way I wanted.

Solution: write my own. I figured why not. I've been messing with PHP5's xml classes and thought it would be fun. (Notice http://samsoffes.com/rss written entirely in PHP5's SimpleXML class.) After struggling to open an external file with file_get_contents(), I resorted to using cURL...

// My feed from http://digg.com/users/samsoffes
$feed = 'http://digg.com/rss/samsoffes/index2.xml';

// Use cURL
$xmlstr = shell_exec('curl '.$feed);

// Create and SimpleXML object
$xml = new SimpleXMLElement($xmlstr);

// Add the Digg XML name space
$xml->registerXPathNamespace('digg', 'http://digg.com/docs/diggrss/');

It works well. I had some problems grabbing the digg count and had to use xpath(). The xpath() method of the SimpleXML class always returns an array. This is very annoying when you would expect it not to return an array if you only get one result.

Anyway, what I love about OO PHP is that I can put

$widget->addCSS('myStyleSheet');

and it will add it to the header for me. If I remove the widget from the sidebar, all of it’s css and javascript is removed as well. This is achieved by passing $this as $widget to the widget's view. It can then call addCSS() in the widget class (that I made) which will then simply forward the request to my page class.

$this->ci->page->addCSS($stylesheet);

Where can you get these classes? I hope to release my Page, Sidebar, and Widget class sometime. There’s no demand for them now so I’m holding off for a bit. If you want them, simply email me.

The OO Page

Posted on September 4, 2007

I love objects. Programming in Cocoa has really made me understand the power of OO (object oriented) programming. I‘ll take a minute to explain objects and MVC for a minute. (If you are familiar with objects, move to the next paragraph.) An object is a variable that has multiple variables and functions (called methods when inside an object) inside it and is an instance of a class. A class is some code that tell the object what it can do. One very popular approach to using objects (or just programming in general) is the MVC method (model, view, controller). Each of the elements of a MVC are usually objects too.

In my simple web site, I use CodeIgniter, which is an open-source PHP framework that makes OO PHP easier and quicker, to run things. I have created several libraries (or classes) to take more work from me and moving it to my libraries. The ones I use the most are Page, Sidebar, and Widget. For the first time in PHP, I have used multiple instances of the same class on the same page. In CodeIgniter, this isn't done that often because you rarely have to, but I decided it would be fun, so I am.

My Page object is my page. It creates my <head> tag, page template, and assembles the rest of my views. One of it‘s views is the sidebar of course. The sidebar is also an object that contains an array (think of it as a list if you don‘ know what it is) of widgets which are also objects. After adding all of my widgets to my sidebar, the page objects asks for the final HTML version of it when it‘s ready to display the page. Inside each sidebar widget, there are several methods for manipulating how it looks and acts too.

Continue reading →

© 2006-2022 Sam Soffes


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK