06. February 2014

Adding Arbitrary Code to your <head> in Magento

Magento’s built in addJs method in the Mage_Page_Block_Html_Head class assumes that files will be hosted locally. This isn’t always desirable. E.g. you may want to use Google’s CDN to host jQuery.

This can be accomplished by adding the following to your layout xml:

<reference name="head">
            <block type="core/text" name="my_head"></block>
        </reference>

Now, in your controller you can add anything into this block that you want, like so:

$google_url = 'http://google.com/jquery.js';
$this->getLayout()->getBlock('my_head')->setText("

            <script src='$google_url'></script>

        ");

09. December 2013

An implementation of the memento pattern in PHP

The memento pattern is a design pattern used to store and revert states for objects which support this capacity. It is accomplished by having a Caretaker object which manages a set of states, encoded in Memento objects. The Memento objects handle the storage of state; the implementation of this can vary, but it necessitates some level of deep-copying the object. A shallow copy will not suffice in general because it will not always capture the whole state of an object, due to the fact that most languages implement memory access for objects as references. Because of this, I use serialize and unserialize in my example below. Of course you could use other methods, like clone or just copying what you know you will need if memory is a concern.

more

04. December 2013

Weighted merging of multiple Markov Chains

Suppose you have ten text sources, and you generate a new block of text trained from each, and you want to give each its own weighting. You have to weave multiple markov chains together.

more