I use the following plugin for the highlighted code on this blog:
http://wordpress.org/extend/plugins/syntaxhighlighter/
However, I found code was never indented properly. After rummaging around and reading a few other blogs it seems the answer is to wrap your Syntax Highlighting tags with the HTML ‘pre’ tags.
In my case that is like so:
<pre>[/sourcecode]</pre>
Here’s a basic jQuery sorting plugin for generic parent child relationships I wrote.
Wherein ‘element’ is the html you wish to insert and ‘value’ is what the associated weight of the element I’m inserting. For now it compares the weight value to the DOM text within the other children.
$.fn.insertSort = function(element, value) {
selector = this;
if ($(selector).length > 0) {
$(selector).attr("inserted", "false");
$(selector).children().each(function() {
if (parseInt($(this).text()) > parseInt(value)) {
$(this).before(element);
$(selector).attr("inserted", "true");
return false;
}
});
if ($(selector).attr("inserted") == "false") {
$(selector).append(element);
}
} else {
$(selector).append(element);
}
$(selector).removeAttr("inserted");
};
It should be noted that this is an insertion sort. It’d be good to extend the plugin to be able to sort an existing piece of html for any parent/child relationship. Possibly with the ability to specify a particular level of depth?
Once and a while you may want to get a collection of the regions that are associated with a country in Magento. For example, the United States is of course a collection of… states. In Magento these are referred to as regions (Japan’s prefectures would hence be referred to as regions as well.)
So for a given country (and that country’s ID or country code) you might want to find all the regions in that country. Which are usually used to populate a dropdown menu on the address selection pages or shipping tax pages.
$collection = Mage::getModel('directory/region')->getResourceCollection()
->addCountryFilter($this->getCountryId())
->load();
It’s great when ORM works for a developer. The problem with Magento I find is the complete lack of documentation. I only discovered the above technique after 20 minutes of grepping the app/code/core directory…
And remember to use Magento’s log method:
foreach($collection as $region) {
Mage::log(print_r($region, true));
}
Note some of the more useful attributes of that $region object are:
- $region->default_name
- $region->code
Which give responses along the lines of South Australia and SA, respectively.

Picked up a 500GB 2.5″ drive and an Astone enclosure today. Pretty sweet. Unfortunately it was about $30 more expensive getting it from iStore than from MSY but convenience of getting it on your lunch break is a big trump card.
Posted in Programming
|
Tagged hardware
|
So I needed to have a regular form checkbox toggle the visibility of a div in my webpage today. It turned out jQuery supports the concept of toggling visibility via .toggle(), which I didn’t realise until I looked it up. And so you can support clicking or even keyboard changing of the checkbox via the change event:
$("#checkbox").change(function() {
$("#areaToToggle").toggle();
});
Pretty sweet, obviously the div should be hidden to begin with. We could add another line after the change event:
$("#areaToToggle").hide();
Too easy
Posted in Programming
|
Tagged jQuery
|