Chris Sedlmayr2011-12-09T08:06:14-08:00http://chris.sedlmayr.co.uk/Chris Sedlmayrchris@sedlmayr.co.ukEssential JavaScript and jQuery design patterns, free kindle book2011-10-10T00:00:00-07:00http://chris.sedlmayr.co.uk/2011-10-10/essential-javascript-and-jquery-design-patterns-free-kindle-book<p>For those of you familiar with the book, quite simply here is a Kindle compatible version.<br/>
For those of you unfamiliar, It's an excellent JavaScript and jQuery Design Patterns book written by Addy Osmani and provided for free.<br/>
You can read all about the book over at <a href="http://addyosmani.com/blog/essentialjsdesignpatterns/">addyosmani.com</a></p>
<p>I've converted it for Kindle compatibility, and it's provided here as a download (with permission from Addy of course).</p>
<p><a href="/downloads/Essential JavaScript Design Patterns For Beginners - Addy Osmani.mobi" onClick="_gaq.push(['_trackEvent','PDF','Download','AddyOsmaniJavaScriptKindleEBook']); _ictt.push(['_customTrigger', 'DOWNLOAD', {'t':'Addy Osmani JavaScript Kindle eBook'}]);" target="_blank">Download Essential JavaScript and jQuery design patterns for Kindle</a></p>
<p>It's always worth keeping an eye on Addy's site as the book is frequently updated there, also it's usually a good source of information anyway
and Addy has published some interesting article of late.<br/>
As Addy releases new versions of the book i'll endeavour to keep the kindle version up to date here.</p>
<p>Kindle Version Last Updated: 18th July 2011 (1.0)</p>
Auto persistent url params in symfony 1.42011-07-18T00:00:00-07:00http://chris.sedlmayr.co.uk/2011-07-18/auto-persistent-url-params-in-symfony-1-4<p>Imagine you have various pages and routes within your application, maybe you are displaying
data based on some URL parameters (filters for example).</p>
<p>If you want to persist your filters between pages then you have a few choices.</p>
<p>You could store the filters in the user's session making it easy, but they are not visible as a URL parameter.<br/>
You could modify or create a new link_to/url_for function then modify your templates to use the new function.</p>
<p>Neither of these appealed to me, I wanted the filters to be in the URL, and I didn't want to modify each link_to usage.</p>
<p>What I have done is created a new custom routing class.<br/>
The class is invoked by configuring a route to use it, and you also tell it which URL params you want to persist.</p>
<div class="highlight"><pre><code class="yaml"><span class="l-Scalar-Plain">dashboard</span><span class="p-Indicator">:</span>
<span class="l-Scalar-Plain">url</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">/dashboard</span>
<span class="l-Scalar-Plain">class</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">ictPersistentParamRoute</span>
<span class="l-Scalar-Plain">options</span><span class="p-Indicator">:</span>
<span class="l-Scalar-Plain">persist</span><span class="p-Indicator">:</span> <span class="p-Indicator">[</span><span class="nv">param1</span><span class="p-Indicator">,</span> <span class="nv">param2</span><span class="p-Indicator">]</span>
<span class="l-Scalar-Plain">param</span><span class="p-Indicator">:</span> <span class="p-Indicator">{</span> <span class="nv">module</span><span class="p-Indicator">:</span> <span class="nv">dashboard</span><span class="p-Indicator">,</span> <span class="nv">action</span><span class="p-Indicator">:</span> <span class="nv">show</span> <span class="p-Indicator">}</span>
</code></pre>
</div>
<p>The class extends sfRoute and overrides the generate() method.<br/>
At URL generation time (usage of url_for/link_to etc) the class checks if the route
has persist options defined, and looks at sfWebRequest to see if these parameters are present.
If they are it will retrieve their values and merge the values to those defined in the (link_to) params.</p>
<p>This makes it very easy to add persistent params to your application without much effort!</p>
<div class="highlight"><pre><code class="php"><span class="cp"><?php</span>
<span class="k">class</span> <span class="nc">ictPersistentParamRoute</span> <span class="k">extends</span> <span class="nx">sfRoute</span>
<span class="p">{</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">generate</span><span class="p">(</span><span class="nv">$params</span><span class="p">,</span> <span class="nv">$context</span> <span class="o">=</span> <span class="k">array</span><span class="p">(),</span> <span class="nv">$absolute</span> <span class="o">=</span> <span class="k">false</span><span class="p">)</span>
<span class="p">{</span>
<span class="c1">// our work happens here</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre>
</div>
New sfNavBuilderPlugin for symfony 1.42011-06-07T00:00:00-07:00http://chris.sedlmayr.co.uk/2011-06-07/new-sfNavBuilderPlugin-for-symfony-1-4<p>I released this plugin yesterday, which offers a simple interface with which to define and render your symfony 1.4 menu's</p>
<p>The plugin allows you to create a hierarchical menu, with each item having it's active state defined by;</p>
<ul>
<li>Current module</li>
<li>Current action</li>
<li>Parameter=value</li>
</ul>
<p>This therefore allows you to say you want an item to be active if;</p>
<pre><code>module = blogPost
action = show
a paramName of post_id = a paramValue of 1
</code></pre>
<h3>Basic usage example</h3>
<p>One main menu item, with 2 child items</p>
<div class="highlight"><pre><code class="php"><span class="x"> </span><span class="cp"><?php</span>
<span class="c1">// create a menu item for the about page</span>
<span class="nv">$homepage</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">sfNavBuilderItem</span><span class="p">();</span>
<span class="nv">$homepage</span>
<span class="o">-></span><span class="na">setDisplayName</span><span class="p">(</span><span class="s1">'About Us'</span><span class="p">)</span>
<span class="o">-></span><span class="na">setUrl</span><span class="p">(</span><span class="nx">url_for</span><span class="p">(</span><span class="s1">'about'</span><span class="p">))</span>
<span class="o">-></span><span class="na">addActivateWhen</span><span class="p">(</span><span class="k">array</span><span class="p">(</span>
<span class="s1">'module'</span> <span class="o">=></span> <span class="k">array</span><span class="p">(</span><span class="s1">'about'</span><span class="p">),</span>
<span class="s1">'action'</span> <span class="o">=></span> <span class="k">array</span><span class="p">(</span><span class="s1">'index'</span><span class="p">)</span>
<span class="p">));</span>
<span class="c1">// create a menu item for the about the team page and define the parent</span>
<span class="nv">$team</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">sfNavBuilderItem</span><span class="p">();</span>
<span class="nv">$team</span>
<span class="o">-></span><span class="na">setDisplayName</span><span class="p">(</span><span class="s1">'About the Team'</span><span class="p">)</span>
<span class="o">-></span><span class="na">setUrl</span><span class="p">(</span><span class="nx">url_for</span><span class="p">(</span><span class="s1">'aboutTeam'</span><span class="p">))</span>
<span class="o">-></span><span class="na">addActivateWhen</span><span class="p">(</span><span class="k">array</span><span class="p">(</span>
<span class="s1">'module'</span> <span class="o">=></span> <span class="k">array</span><span class="p">(</span><span class="s1">'about'</span><span class="p">),</span>
<span class="s1">'action'</span> <span class="o">=></span> <span class="k">array</span><span class="p">(</span><span class="s1">'team'</span><span class="p">)</span>
<span class="p">))</span>
<span class="o">-></span><span class="na">setParent</span><span class="p">(</span><span class="nv">$homepage</span><span class="p">);</span>
<span class="c1">// create a menu item for the about the company page and define the parent</span>
<span class="nv">$company</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">sfNavBuilderItem</span><span class="p">();</span>
<span class="nv">$company</span>
<span class="o">-></span><span class="na">setDisplayName</span><span class="p">(</span><span class="s1">'About the Company'</span><span class="p">)</span>
<span class="o">-></span><span class="na">setUrl</span><span class="p">(</span><span class="nx">url_for</span><span class="p">(</span><span class="s1">'aboutCompany'</span><span class="p">))</span>
<span class="o">-></span><span class="na">addActivateWhen</span><span class="p">(</span><span class="k">array</span><span class="p">(</span>
<span class="s1">'module'</span> <span class="o">=></span> <span class="k">array</span><span class="p">(</span><span class="s1">'about'</span><span class="p">),</span>
<span class="s1">'action'</span> <span class="o">=></span> <span class="k">array</span><span class="p">(</span><span class="s1">'company'</span><span class="p">)</span>
<span class="p">))</span>
<span class="o">-></span><span class="na">setParent</span><span class="p">(</span><span class="nv">$homepage</span><span class="p">);</span>
<span class="c1">// put the items into a menu instance and provide the required info</span>
<span class="nv">$this</span><span class="o">-></span><span class="na">menu</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">sfNavBuilder</span><span class="p">();</span>
<span class="nv">$this</span><span class="o">-></span><span class="na">menu</span>
<span class="o">-></span><span class="na">setRequest</span><span class="p">(</span><span class="nv">$request</span><span class="p">)</span>
<span class="o">-></span><span class="na">setModule</span><span class="p">(</span><span class="nv">$this</span><span class="o">-></span><span class="na">getContext</span><span class="p">()</span><span class="o">-></span><span class="na">getModuleName</span><span class="p">())</span>
<span class="o">-></span><span class="na">setAction</span><span class="p">(</span><span class="nv">$this</span><span class="o">-></span><span class="na">getContext</span><span class="p">()</span><span class="o">-></span><span class="na">getActionName</span><span class="p">())</span>
<span class="o">-></span><span class="na">addItem</span><span class="p">(</span><span class="nv">$homepage</span><span class="p">);</span>
<span class="c1">// in your template you can then call the renderer</span>
<span class="o"><?</span><span class="nx">php</span> <span class="k">echo</span> <span class="nv">$menu</span><span class="o">-></span><span class="na">render</span><span class="p">();</span> <span class="cp">?></span><span class="x"></span>
<span class="x"> // check out the sfNavBuilderRenderer class to see what this does</span>
</code></pre>
</div>
<p>I have included some fuller examples of use within the readme of the project, but am happy to answer question you have when using it.</p>
<p><a href="https://github.com/catchamonkey/sfNavBuilderPlugin" title="Project page on github">Project page on github</a><br/>
<a href="http://catchamonkey.github.com/sfNavBuilderPlugin" title="sfNavBuilderPlugin Project site">sfNavBuilderPlugin Project site</a></p>
<p>Hope you find it useful!</p>
Curl and xmllint play nicely together2011-05-05T00:00:00-07:00http://chris.sedlmayr.co.uk/2011-05-05/curl-and-xmllint-play-nicely-together<p>If you work a lot with XML API's (writing or consuming), and have ever called one directly using curl to test/debug some output, you may find that the output isn't very friendly, and for larger responses it's almost unreadable.</p>
<p>By using curl and xmllint together you can have your response nicely formatted.</p>
<p>To do so, invoke curl, and parse the response through xmllint, giving xmllint the --format option and the - option which tells it to read from stdin</p>
<div class="highlight"><pre><code class="sh">curl -sL <span class="s2">"http://api.example.com/assets.xml"</span> | xmllint --format -
</code></pre>
</div>
<p>The curl options used here are -s to invoke silent mode, and -L so it follows redirects.</p>
<p><a href="http://xmlsoft.org/xmllint.html" title="Read more about xmllint here">Read more about xmllint here</a> or view the man pages directly if you have it installed.</p>
Textmate tip, Automatic PHP spl_autoload class name from file path2010-06-30T00:00:00-07:00http://chris.sedlmayr.co.uk/2010-06-30/textmate-tip-automatic-php-spl_autoload-class-name-from-file-path<p>The title says it all so i'll just get on with it.</p>
<p>Create a new textmate command, and set the output to 'Insert as snippet'<br/>
Paste in the following.</p>
<div class="highlight"><pre><code class="php"><span class="x">#!/usr/bin/php</span>
<span class="cp"><?php</span>
<span class="nv">$path</span> <span class="o">=</span> <span class="nv">$_ENV</span><span class="p">[</span><span class="s1">'TM_FILEPATH'</span><span class="p">];</span>
<span class="nv">$path</span> <span class="o">=</span> <span class="nb">trim</span><span class="p">(</span><span class="nv">$path</span><span class="p">,</span> <span class="s1">'/'</span><span class="p">);</span>
<span class="nv">$path</span> <span class="o">=</span> <span class="nb">trim</span><span class="p">(</span><span class="nv">$path</span><span class="p">,</span> <span class="s1">'.php'</span><span class="p">);</span>
<span class="nv">$parts</span> <span class="o">=</span> <span class="nb">explode</span><span class="p">(</span><span class="s1">'/'</span><span class="p">,</span> <span class="nv">$path</span><span class="p">);</span>
<span class="nv">$lastPart</span> <span class="o">=</span> <span class="nb">end</span><span class="p">(</span><span class="nv">$parts</span><span class="p">);</span>
<span class="k">echo</span> <span class="s1">'class '</span><span class="p">;</span>
<span class="k">foreach</span> <span class="p">(</span><span class="nv">$parts</span> <span class="k">as</span> <span class="nv">$id</span> <span class="o">=></span> <span class="nv">$part</span><span class="p">)</span> <span class="p">{</span>
<span class="c1">// textmate placeholders start at 1</span>
<span class="nv">$id</span> <span class="o">=</span> <span class="nv">$id</span><span class="o">+</span><span class="m">1</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="nv">$lastPart</span> <span class="o">==</span> <span class="nv">$part</span><span class="p">)</span> <span class="p">{</span>
<span class="k">echo</span> <span class="s1">'${'</span><span class="o">.</span><span class="nv">$id</span><span class="o">.</span><span class="s1">':'</span><span class="o">.</span><span class="nv">$part</span><span class="o">.</span><span class="s1">'}'</span><span class="p">;</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="k">echo</span> <span class="s1">'${'</span><span class="o">.</span><span class="nv">$id</span><span class="o">.</span><span class="s1">':'</span><span class="o">.</span><span class="nv">$part</span><span class="o">.</span><span class="s1">'_}'</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="cp">?></span><span class="x"></span>
</code></pre>
</div>
<p>And bind to a tab trigger.<br/>
When you execute from within a file it performs the following.</p>
<p>Retrieves the full path of the file you are in.<br/>
Removes any forward slashes from the beginning and end of the path.<br/>
Removes the .php extension from the end of the file.<br/>
Explodes each part of the path, seperated by a slash (/) into an array.<br/>
Gets the pointer for the last item in the array.<br/>
echo's 'class'<br/>
Then iterates through the array, creating a textmate snippet placeholder for each one, all of which have the required underscore (_) apart from the last one.</p>
<p>So for a file that is in the path /The/Full/Path/Filename.php<br/>
We end up with the resulting snippet of</p>
<div class="highlight"><pre><code class="php"><span class="x">${1:The_}${2:Full_}${3:Path_}${4:Filename}</span>
</code></pre>
</div>
<p>Which then enables you to tab through them and remove those from the start that you don't need.<br/>
You could go a step further and only spit out the last 3 parts as that is generally how many are needed for a class name. But it really depends on your app structure so I've left it as is for now.</p>
<p>Check out the image for the options<br/>
<a href="/images/textmateCommand.jpg"><img src="/images/textmateCommand-300x205.jpg" alt="Textmate Command Options" /></a></p>
<h3>Update:</h3>
<p>I've made a version that gives you just the last 3 parts of the path as mentioned above.<br/>
I have both running on the 'class' tab command so can choose between them as required.</p>
<div class="highlight"><pre><code class="php"><span class="x">#!/usr/bin/php</span>
<span class="cp"><?php</span>
<span class="nv">$path</span> <span class="o">=</span> <span class="nv">$_ENV</span><span class="p">[</span><span class="s1">'TM_FILEPATH'</span><span class="p">];</span>
<span class="nv">$path</span> <span class="o">=</span> <span class="nb">trim</span><span class="p">(</span><span class="nv">$path</span><span class="p">,</span> <span class="s1">'/'</span><span class="p">);</span>
<span class="nv">$path</span> <span class="o">=</span> <span class="nb">trim</span><span class="p">(</span><span class="nv">$path</span><span class="p">,</span> <span class="s1">'.php'</span><span class="p">);</span>
<span class="nv">$parts</span> <span class="o">=</span> <span class="nb">explode</span><span class="p">(</span><span class="s1">'/'</span><span class="p">,</span> <span class="nv">$path</span><span class="p">);</span>
<span class="k">echo</span> <span class="s1">'class '</span><span class="p">;</span>
<span class="c1">// do this 3 times</span>
<span class="k">for</span><span class="p">(</span><span class="nv">$i</span> <span class="o">=</span> <span class="m">1</span><span class="p">;</span> <span class="nv">$i</span> <span class="o"><=</span> <span class="m">3</span><span class="p">;</span> <span class="nv">$i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="nv">$varName</span> <span class="o">=</span> <span class="s1">'part_'</span><span class="o">.</span><span class="nv">$i</span><span class="p">;</span>
<span class="nv">$part</span> <span class="o">=</span> <span class="nb">array_pop</span><span class="p">(</span><span class="nv">$parts</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="nv">$i</span> <span class="o">==</span> <span class="m">1</span><span class="p">)</span> <span class="p">{</span>
<span class="nv">$$varName</span> <span class="o">=</span> <span class="s1">'${'</span><span class="o">.</span><span class="nv">$i</span><span class="o">.</span><span class="s1">':'</span><span class="o">.</span><span class="nv">$part</span><span class="o">.</span><span class="s1">'}'</span><span class="p">;</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="nv">$$varName</span> <span class="o">=</span> <span class="s1">'${'</span><span class="o">.</span><span class="nv">$i</span><span class="o">.</span><span class="s1">':'</span><span class="o">.</span><span class="nv">$part</span><span class="o">.</span><span class="s1">'_}'</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="c1">// pass the snippet to the file</span>
<span class="k">echo</span> <span class="nv">$part_3</span><span class="o">.</span><span class="nv">$part_2</span><span class="o">.</span><span class="nv">$part_1</span><span class="p">;</span>
<span class="cp">?></span><span class="x"></span>
</code></pre>
</div>
Automatically renaming multiple files by rule2010-05-16T00:00:00-07:00http://chris.sedlmayr.co.uk/2010-05-16/automatically-renaming-multiple-files-by-rule<p>I recently had the need to rename a bunch of files according to a simple rule.<br/>
In the directory, any files that had a prefix of "Gee<em>" should have the prefix changed to "Kif</em>"<br/>
After a little digging I found a simple but quite tasty solution.<br/>
Simply run this at your command line.</p>
<div class="highlight"><pre><code class="bash"><span class="k">for </span>f in Gee_*; <span class="k">do </span>mv <span class="s2">"$f"</span> <span class="s2">"Kif_${f#Gee_}"</span>; <span class="k">done</span>
</code></pre>
</div>
<p>Job done. It finds files matching the pattern supplied, then loops through renaming them appropriately.</p>
<p>It's worth noting this isn't recursive so will only complete for files in your current directory.</p>
Git pull and git merge between developers2010-04-15T00:00:00-07:00http://chris.sedlmayr.co.uk/2010-04-15/git-pull-and-git-merge-between-developers<p>I use git locally, but the main upstream repo is an SVN one, there are also a couple of other devs that also use git locally.<br/>
We are using git svn as the bridge between these 2.</p>
<p>If we want to share code or update each other it was generally the case that we would have to commit everything back to svn (git svn dcommit), then the others would have to rebase.<br/>
It felt like I was abusing git and not making the most of it.</p>
<h3>Enter git pull</h3>
<p>Git pull allows you to grab code from another git repo and store it in your local repo.</p>
<h3>Git pull</h3>
<p>I'll presume you already have a git repo locally, and that there is another git repo somewhere you have access to.</p>
<p>First of all we add the remote location.</p>
<div class="highlight"><pre><code class="bash">git remote add mp git.user@mpdevbox:/www/cm/
</code></pre>
</div>
<p>Let me break that command down</p>
<div class="highlight"><pre><code class="bash">git remote <span class="nv">add</span> <span class="o">=</span> The git <span class="nb">command</span>
<span class="nv">mp</span> <span class="o">=</span> A friendly name you use <span class="k">for </span>this remote location
git.user@mpdevbox <span class="o">=</span> the remote user and the remote location
:/www/cm <span class="o">=</span> The path to the remote git repo, <span class="o">(</span>note the colon<span class="o">)</span>
</code></pre>
</div>
<p>Having completed that command you now have a reference to a remote git repo, If you want to pull the code down from it, it's simple (you may want to create and checkout a branch before you do so, depending on your needs. I won't so will just pull)</p>
<div class="highlight"><pre><code class="bash">git pull mp master
</code></pre>
</div>
<p>Where master is the name of your local branch you want to pull into.</p>
<p>This will pull down the code from the remote branch and merge it into your branch, if that all goes OK it will then perform a commit, if you don't wan the commit then just add --no-commit to the end of the pull command</p>
<p>If there are conflicts that git fails to resolve automatically then you will need to resolve them manually.<br/>
This can be easily done using mergetool</p>
<div class="highlight"><pre><code class="bash">git mergetool
</code></pre>
</div>
<p>and follow the instructions.</p>
<p>On Mac this will open a merge/diff client which lets you easily choose from your local and the pulled down repo. Once done you can commit if you want, there is even a pre-filled commit message for you about the merge.</p>
<p>It's as simple as that really, this approach also makes it much easier to develop on multiple machines, without having to get rid of SVN as your main repo (which depending on circumstances, requirements, or the people that make those decisions, may not be an easy sell).</p>
Git Manuals, Docs and Handy Links2010-04-06T00:00:00-07:00http://chris.sedlmayr.co.uk/2010-04-06/git-manuals-docs-and-handy-links<p>As i am a recent git convert I find myself reading a lot of guides etc on the best way to use git and how my workflow can change now i'm using a DVCS.</p>
<p>This is an attempt to collate my more useful findings, partly for myself, but it may prove useful for you too.</p>
<h3>Git Books</h3>
<p><a href="http://book.git-scm.com/index.html">http://book.git-scm.com/index.html</a><br/>
<a href="http://progit.org/book/">http://progit.org/book/</a></p>
<h3>Git-SVN Resources</h3>
<p><a href="http://orestis.gr/blog/2008/08/23/git-svn-tutorial/">http://orestis.gr/blog/2008/08/23/git-svn-tutorial/</a></p>
<h3>Full Git online manual</h3>
<p><a href="http://www.kernel.org/pub/software/scm/git/docs/v1.7.0.4/git.html">http://www.kernel.org/pub/software/scm/git/docs/v1.7.0.4/git.html</a></p>
<h3>Combining multiple git commits into 1 SVN commit</h3>
<p>In some cases I use git as a local repo, but the main repo upstream is SVN. This sample enables me to combine multiple local git commits into a single commit back to SVN.
This was taken from <a href="http://stackoverflow.com/questions/1408381/combine-local-git-commits-into-one-commit-with-git-svn" title="Stack Overflow git commit squashing">Stack Overflow</a></p>
<div class="highlight"><pre><code class="bash">git tag <span class="nb">local</span> <span class="c"># create a temporary tag</span>
git reset --hard trunk
git merge --squash <span class="nb">local</span>
git commit <span class="c"># write your single commit message here</span>
git svn dcommit
git tag -d <span class="nb">local</span> <span class="c"># delete the temporary tag named local</code></span>
</code></pre>
</div>
<p>Delicious
I also tend to bookmark a lot of stuff over at Delicious so you can check out my git stuff there too. Items tagged 'Git' at my Delicious</p>
Zebra Striping with PHP the Easy Way2010-03-16T00:00:00-07:00http://chris.sedlmayr.co.uk/2010-03-16/zebra-striping-with-php-the-easy-way<p>Using one of the built in Maths functions of PHP we can easily create zebra striped lists.<br/>
Let's say we have an array of entries stored within the $comments variable.<br/>
All we have to do with this is to use the fmod function and assign a css class that alternates per entry.</p>
<div class="highlight"><pre><code class="php"><span class="x"><ul></span>
<span class="x"> </span><span class="cp"><?php</span> <span class="k">foreach</span> <span class="p">(</span><span class="nv">$comments</span> <span class="k">as</span> <span class="nv">$int</span> <span class="o">=></span> <span class="nv">$comment</span><span class="p">)</span><span class="o">:</span> <span class="cp">?></span><span class="x"></span>
<span class="x"> <li class="</span><span class="cp"><?php</span> <span class="k">echo</span> <span class="nb">fmod</span><span class="p">(</span><span class="nv">$int</span><span class="p">,</span> <span class="m">2</span><span class="p">)</span> <span class="o">?</span> <span class="s1">'even'</span> <span class="o">:</span> <span class="s1">'odd'</span> <span class="cp">?></span><span class="x">"></span>
<span class="x"> </span><span class="cp"><?php</span> <span class="k">endforeach</span><span class="p">;</span> <span class="cp">?></span><span class="x"></span>
<span class="x"></ul></span>
</code></pre>
</div>
<p>Then just give the even and odd classes different colours to suit your layout.</p>
<div class="highlight"><pre><code class="css"><span class="nt">li</span><span class="nc">.even</span> <span class="p">{</span>
<span class="k">background-color</span><span class="o">:</span><span class="m">#FFFFFF</span><span class="p">;</span>
<span class="k">color</span><span class="o">:</span><span class="m">#000000</span><span class="p">;</span>
<span class="p">}</span>
<span class="nt">li</span><span class="nc">.odd</span> <span class="p">{</span>
<span class="k">background-color</span><span class="o">:</span><span class="m">#000000</span><span class="p">;</span>
<span class="k">color</span><span class="o">:</span><span class="m">#FFFFFF</span><span class="p">;</span>
<span class="p">}</span>
</code></pre>
</div>
<p>And you're done!</p>
Using helpers within an action in symfony 1.02010-03-16T00:00:00-07:00http://chris.sedlmayr.co.uk/2010-03-16/using-helpers-within-an-action-in-symfony-1-0<p>If you ever need to use a helper outside a template, you can still load a helper group from anywhere by calling</p>
<div class="highlight"><pre><code class="php"><span class="cp"><?php</span>
<span class="nx">sfLoader</span><span class="o">::</span><span class="na">loadHelpers</span><span class="p">(</span><span class="nv">$helpers</span><span class="p">)</span>
</code></pre>
</div>
<p>where $helpers is a helper group name or an array of helper group names.
For instance, if you want to use simple_format_text() in an action, you need to call</p>
<div class="highlight"><pre><code class="php"><span class="cp"><?php</span>
<span class="nx">sfLoader</span><span class="o">::</span><span class="na">loadHelpers</span><span class="p">(</span><span class="s1">'Text'</span><span class="p">);</span>
</code></pre>
</div>
<p>I know this is covered in the symfony docs but it's a fairly handy thing to know so thought I would make it easier to find.</p>
The use of 301 rewrite2010-03-16T00:00:00-07:00http://chris.sedlmayr.co.uk/2010-03-16/the-use-of-301-rewrite<p>If you are hosting a domain (or multiple domains) using VirtualHost entries, it is bad practice to use individual entries for the same domain just to cover the inclusion or exclusion of the www in the address, amongst other things it is bad for SEO, but of course you still want visitors to be able to go to your site via the 2 starting addresses http://sedlmayr.co.uk and http://www.sedlmayr.co.uk</p>
<p>For example you should not have an entry for www.sedlmayr.co.uk and another for sedlmayr.co.uk
There are a few ways you can do this but have found this particular one to be effective.
Whatever url you go to; http://sedlmayr.co.uk or http://www.sedlmayr.co.uk a redirect happens on the http://sedlmayr.co.uk that sends you to http://www.sedlmayr.co.uk.</p>
<p>But what about people that go to specific pages such as http://sedlmayr.co.uk/services</p>
<p>We use a RewriteRule so that whatever is after the domain itself (in this case services)(When using http://sedlmayr.co.uk) http://sedlmayr.co.uk/services is shown as http://www.sedlmayr.co.uk/services
It is very simple to achieve this.</p>
<p>Within you VirtualHost entry you need to specify a ServerAlias. So I would have</p>
<div class="highlight"><pre><code class="apache"><span class="nb">ServerName</span> www.sedlmayr.co.uk
<span class="nb">ServerAlias</span> sedlmayr.co.uk
</code></pre>
</div>
<p>And then within the vhost file for your project you would have</p>
<div class="highlight"><pre><code class="apache"><span class="nb">RewriteCond</span> %{HTTP_HOST} ^sedlmayr\.co.uk
<span class="nb">RewriteRule</span> ^(.*)$ http://www.sedlmayr.co.uk/$1 [R=301,L]
</code></pre>
</div>
<p>Which needs to go at the top.
That's it!
Hope this is helpful to you at some point.</p>
Imagemagick sfThumbnailPlugin fix for scaling under symfony 1.02010-03-16T00:00:00-07:00http://chris.sedlmayr.co.uk/2010-03-16/imagemagick-sfthumbnail-plugin-fix-for-scaling-under-symfony-1-0<p>I've been using the sfThumbnail Plugin in symfony 1.0 for image resizing when users upload.<br/>
When I asked the plugin to resize an image with scaling there was a problem and the image came out skewed and distorted.<br/>
After a little research I found the following fix.</p>
<p>Once you have installed the plugin, edit the sfImageMagickAdapter.class.php in the plugin dir.</p>
<p>Change lines 223-237:</p>
<p>From</p>
<div class="highlight"><pre><code class="php"><span class="cp"><?php</span>
<span class="nv">$width</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-></span><span class="na">sourceWidth</span><span class="p">;</span>
<span class="nv">$height</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-></span><span class="na">sourceHeight</span><span class="p">;</span>
<span class="nv">$x</span> <span class="o">=</span> <span class="nv">$y</span> <span class="o">=</span> <span class="m">0</span><span class="p">;</span>
<span class="k">switch</span> <span class="p">(</span><span class="o">@</span><span class="nv">$this</span><span class="o">-></span><span class="na">options</span><span class="p">[</span><span class="s1">'method'</span><span class="p">])</span> <span class="p">{</span>
<span class="k">case</span> <span class="s2">"shave_all"</span><span class="o">:</span>
<span class="k">if</span> <span class="p">(</span><span class="nv">$width</span> <span class="o">></span> <span class="nv">$height</span><span class="p">)</span>
<span class="p">{</span>
<span class="nv">$x</span> <span class="o">=</span> <span class="nb">ceil</span><span class="p">((</span><span class="nv">$width</span> <span class="o">-</span> <span class="nv">$height</span><span class="p">)</span> <span class="o">/</span> <span class="m">2</span> <span class="p">);</span>
<span class="nv">$width</span> <span class="o">=</span> <span class="nv">$height</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">elseif</span> <span class="p">(</span><span class="nv">$height</span> <span class="o">></span> <span class="nv">$width</span><span class="p">)</span>
<span class="p">{</span>
<span class="nv">$y</span> <span class="o">=</span> <span class="nb">ceil</span><span class="p">((</span><span class="nv">$height</span> <span class="o">-</span> <span class="nv">$width</span><span class="p">)</span> <span class="o">/</span> <span class="m">2</span><span class="p">);</span>
<span class="nv">$height</span> <span class="o">=</span> <span class="nv">$width</span><span class="p">;</span>
<span class="p">}</span>
</code></pre>
</div>
<p>To</p>
<div class="highlight"><pre><code class="php"><span class="cp"><?php</span>
<span class="nv">$width</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-></span><span class="na">sourceWidth</span><span class="p">;</span>
<span class="nv">$height</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-></span><span class="na">sourceHeight</span><span class="p">;</span>
<span class="nv">$mWidth</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-></span><span class="na">maxWidth</span><span class="p">;</span>
<span class="nv">$mHeight</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-></span><span class="na">maxHeight</span><span class="p">;</span>
<span class="nv">$x</span> <span class="o">=</span> <span class="nv">$y</span> <span class="o">=</span> <span class="m">0</span><span class="p">;</span>
<span class="k">switch</span> <span class="p">(</span><span class="o">@</span><span class="nv">$this</span><span class="o">-></span><span class="na">options</span><span class="p">[</span><span class="s1">'method'</span><span class="p">])</span> <span class="p">{</span>
<span class="k">case</span> <span class="s2">"shave_all"</span><span class="o">:</span>
<span class="k">if</span> <span class="p">(</span><span class="nv">$width</span> <span class="o">></span> <span class="nv">$height</span><span class="p">)</span>
<span class="p">{</span>
<span class="nv">$x</span> <span class="o">=</span> <span class="nb">ceil</span><span class="p">((</span><span class="nv">$width</span> <span class="o">-</span> <span class="p">(</span><span class="nv">$height</span><span class="o">*</span><span class="nv">$mWidth</span><span class="p">)</span><span class="o">/</span><span class="nv">$mHeight</span><span class="p">)</span> <span class="o">/</span> <span class="m">2</span> <span class="p">);</span>
<span class="nv">$width</span> <span class="o">=</span> <span class="nv">$height</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">elseif</span> <span class="p">(</span><span class="nv">$height</span> <span class="o">></span> <span class="nv">$width</span><span class="p">)</span>
<span class="p">{</span>
<span class="nv">$y</span> <span class="o">=</span> <span class="nb">ceil</span><span class="p">((</span><span class="nv">$height</span> <span class="o">-</span> <span class="nv">$width</span><span class="o">*</span><span class="p">(</span><span class="nv">$mHeight</span><span class="o">/</span><span class="nv">$mWidth</span><span class="p">))</span> <span class="o">/</span> <span class="m">2</span><span class="p">);</span>
<span class="nv">$height</span> <span class="o">=</span> <span class="nv">$width</span><span class="p">;</span>
<span class="p">}</span>
</code></pre>
</div>
<p>It's a straight forward edit so you should be able to see why this simple change has the desired effect, and your images will now behave nicely when scaling.</p>
How-to collapse the web debug toolbar in symfony 1.0 by default2010-03-16T00:00:00-07:00http://chris.sedlmayr.co.uk/2010-03-16/howto-collapse-the-web-debug-toolbar-in-symfony-1-0-by-default<p>To automatically collapse, hide, shrink (or whatever you want to call it) the web debug toolbar to just the SF and close icons; add this line</p>
<div class="highlight"><pre><code class="js"><span class="nb">window</span><span class="p">.</span><span class="nx">onload</span><span class="o">=</span><span class="nx">sfWebDebugToggleMenu</span><span class="p">;</span>
</code></pre>
</div>
<p>to the end of this file</p>
<div class="highlight"><pre><code class="sh">...<span class="se">\s</span>ymfony<span class="se">\w</span>eb<span class="se">\s</span>f<span class="se">\s</span>f_web_debug<span class="se">\j</span>s<span class="se">\m</span>ain.js
</code></pre>
</div>
<p>This handy little tip courtesy of IsRobot.</p>