So once again I’ve become the guy who invented the site but never posts updates to it. Sorry about that.
A combination of complex work projects and the odd family drama hasn’t left a lot of time for web learnin’, although I achieved more with Node during March and April than I had a chance to write about. Mainly creating a new site using Node and the Express framework – What the Framework – intended as a bluffer’s guide to all those fancy new technologies the cool kids are talking about. And recreating the previously PHP-driven fulljames.net as another Node site – kind of because I could. I still hope to write up the basics of how these sites work but until then you could do worse than check out Jack Franklin’s excellent tutorial on getting started with Node and Express.
Then at the end of April Paul and I flew to Poland for the excellent Front Trends conference, which did a lot to get my motivation going again. Most of the talks across the two days were excellent, but the highlights for me in terms of picking up new things to get into were Lea Verou on CSS animations and Jina Bolton on SASS, and for general inspiration and provocation Alex Russell, Bartosz Szopka, Chris Coyier, John Schultz and Rachel Andrew.
And this week in Brighton saw the second Points mini conference, which in comparison may have “only” comprised of four talks and a raffle of top quality web-related prizes, but was an equally inspirational and thought provoking evening in the company of friends and new acquaintances (some of whom have also posted here!).
So with a couple of weeks of May still to go I’m going to carry on with my exploration of Node, particularly as I’m now getting to the point where I need to do stuff with data, and there are various options for storing it including MongoDB, Redis and even good old MySQL. I’m also going to continue a talk outline I started putting together on the train home from Points, perhaps I’ll find an opportunity to try it out on a few people as the year goes on…
Wow, it’s been a while. Last post I made on here was back in March where I’d said I had been reading up on jQuery. Well, funnily enough, I still am (it’s a big subject) but I’m proud of what I’ve learnt. I’m still reading Sitepoints ‘jQuery: Novice to Ninja’ (it’s hard finding time to read with a newborn) but I’ve picked up a lot in the past few months and now pretty confident with the basics of jQuery (even if I do forget the (document).ready declaration a lot of the time). The last client project I handed over had a .js file with around 150 lines of jQuery I had written! That’s pretty epic from where I’m standing and that’s the whole idea behind the 12412project!
So what’s next? jQuery is faaar from finished but I figured I’ll pick up more of that throughout the year. I’ve had a real flurry of client projects take hold the last couple of months and I’m pretty booked up for the foreseeable few months too, so that should help get the jQuery knowledge cemented.
CSS Animations are next on my list, and I’ve played with those once or twice on recent projects. I’ve got a domain name with my name on it (literally!) which I want to make into a cool introduction page with portfolio and contacts. A kinda flashy way of saying ‘Hi, I can do awesome stuff – hire me why dontcha’! I’ve got some ideas in my head which I want to flesh out first and I’ll post my brief introduction to CSS animations in a week or so!
So Its May and I am writing about my April explortion so I guess posting late is my thing then.
Getting the Fonts a client wants to see into a webpage has been fraught with technical issues which tend towards the obscure from a clients point of view. From their perspective the graphics designer has created a document that they can see on a screen with no problem so why cant a web page do just that ? When these conversations raise their head these are the moments which lead a developer to beat their head against the brick wall of customer obstinacy as they attempt to explain why text embeded in graphics becomes problematic in terms of search, in terms of design and in terms of portability.
Google web fonts has been a good start in answering the how and the why as to the design issue but it has lacked a certain portability in terms of choices available to the designer. Cufon appears to fill the gap in the ‘pick your font and show it’ market but my experiences in the last few weeks demonstrate that the market for purchasing and implementing cufonts is tenuous and unsatisfactorily delivered.
Both technologies require javascript implementations in their preload and this can lead to limitations in site interactivity and functionality from a users perspective as well as the usual hassle of playing browser support limitations with delivery. They are well supported with plugins for WordPress and plenty of sample code abounds on the net to demonstrate the how to implement requirements.
Finally the End User License requirements of managing Fonts ( an aspect the copyright industry rarely seems to chase after as efficaciously as say Music or Video ) has been a constant source of delay and investigation and at the end of an hours deliberation over font texture and styles the lack of clarity on permission is a straw that breaks a back of a decision.
For now I am sticking with Google Fonts until such time as CuFon marketplace is mature enough to ensure simple delivery of Font content for a developer and webdesigner.
As for May; Well I want to take a detour back to Jquery and get a deeper appreciation of creating Ajax calls with live feedback such as the type required in status updates and scrolling completion status bars.
If I were to be graded for March then I would get a partial fail for this month. Whilst I did goto PhoneGap and download and install the extensions for both iPhone and Android development I failed to actually follow the tutorials and create some form of Hello World application for any platform; which on the whole is not a very impressive affair.
The lesson to be learnt here is that “Failure Is An Option” and that whilst March may not have been the most productive month for my 12412 Experiences it did not leave me utterly devoid of experience or understanding. Not only did I re-aqaint myself with The Eclipse development environment on Ubuntu and Windows but I refreshed my Macboook Pro Xcode installation and updated the machines OS to Lion. All good procrastination activities to be done when trying to complete any module and all good pointers towards the fact that I have an abundance of kit to code on and I could be trying harder.
I had hoped to gain some understanding of developing using Phonegap instead what I learned was that March was a little too busy and that I dont feel harsh on myself for not completing a task.
I hope this post can encourage all of you taking part in 12412 to keep up the focus month to month and to have fun and feel guilt free about some goals.
I chose Node.js as my March project because, for someone working mainly in Javascript development, the thought of being able to use the same technology and ways of working on both client and server is pretty exciting. I’ve used it a little bit already, on prototyping projects for clients, but I wanted to step back and start from the fundamentals to really understand how to do it properly. A big part of this was taking myself to Remy Sharp’s Node workshop in Brighton, which was a brilliant introduction to the concepts behind the language and an opportunity to spend a couple of days immersed in asynchronous thinking and the wonders of new real-time tools such as websockets which Node is perfect for.
I’m not going to cover setting up Node on a local system for development, because for Mac and Windows users there’s now a simple package installer for the latest versions. And if you’re on Linux you’ll already be used to whistling your own TCP/IP packets down an ethernet cable so it should be a snap. Just head to the Node.js downloads to get going.
However I also wanted to make sure that it would be possible to run Node sites on my hosting service of choice – Webfaction. There are a number of other more immediately compatible services out there, such as Heroku and Linode, but as Webfaction allows you free access to install software and there are a few guides out there on the web I thought I would give it a go. At best it would let me keep all my sites in once place and avoid paying out for something else, at worst… at least I tried!
Onwards!
The first guide I tried made it sound pretty straightforward. And indeed it is. Open a SSH connection to your hosting account, download the package. Build it. Job done.
The catch, in my case, was that my hosting account was on an older server at Webfaction which doesn’t support…. actually I had no idea why it didn’t do what it needed to, but this is a known problem and they offer free migration to a new server if you need it. So I spent a week moving over and reconfiguring old sites and a few WordPress installations (including this one, did you notice?) and then set back to getting Node up and running.
Once this was done, the actual Node install process ran without any further glitches. Broadly speaking I followed the answer in this error report, although its worth mentioning that more recent versions of Node have NPM (the module manager) built so there’s no need to do the extra step listed in the first guide. And of course you are getting the current version of Node – which at the time of writing was 0.6.12.
Forwards!
With a working Node installation it was time to test some code out. The specifics of this I’ll cover in another post, but there is a further step to get to a running application on Webfaction hosting. Within the service’s control panel you manage your URLs, create applications and then associate them together into websites. By default, applications are chosen from a list of pre-installed frameworks – Rails, WordPress, etc – but with a manually installed language like Node things are a little more complicated.
One of the options is ‘custom application – listening on port’, which assigns a port number that it will forward traffic from. So if you create one of these, assign it to a URL and then start a Node instance running on that port – website!
As a test of all this I decided to recreate my very simple personal homepage – fulljames.net – as a Node application, and I’ll blog more about doing that soon.
So I’ve decided to join in on the 12412 project. I’m a little late, but to be fair, I only found out about it a few weeks ago.
I’ve been involved in the web development industry since 2004 when I had 2 weeks work experience at a local firm. That lead to a summer job, which lead to a part time job, that eventually led to me working full time as a web developer nearly 2 years ago. I like to think I am pretty up to date with the current web technologies — I’ve been using HTML5 and CSS3 on all my projects for the last few months, I use jQuery on most projects, and I have been using PHP (with MySQL where appropriate) since I became familiar with it a few years ago. I don’t claim to be a graphic designer, but I would say I have keen eye for design and love creating beautiful hand crafted websites.
However, I am always interested in furthering my knowledge of web technologies, so when I discovered the 12412 project, I was keen to get on board. I am currently having a bit of trouble even thinking of 12 things to learn over the year, but I have a few in mind to get me started, and I can add more to the list as the year goes on. I have already learnt something new this year, which I am going to cheat and include as one of the twelve so I can catch up. Here’s what I have so far:
HTML5 geolocation API (I’ve learnt this already — Clive Walker has very nicely linked to my article in his own about geolocation)
HTML5 canvas
LESS dynamic stylesheet language
WordPress
iPhone application development
OK, so the last one isn’t strictly a web technology. I’ve experimented with iPhone web app development over the last couple of months, but having an understanding of native app development is something I think will be useful.
If anyone has any suggestions of things to learn, I would be happy to hear them.
With three of us working on the core WordPress theme which makes up the 12412 site, we wanted to be sure we could all easily make updates to it – but without conflicting with each other and in a way that made the theme open and available for anyone to look at.
The second part of this requirement was simple: we work with the theme in a Git repository, and are using Github both as the “origin” repo and a place to host and give access to the theme files for anyone who is interested in seeing them. The theme itself is based upon Paul’s Slim Starkers, which is itself a further cut-down fork of Elliot Jay Stocks’ Starkers and provides a nice, clean starting point for your own layouts. Between us we have varying experience of Git as a source control system, so working in this way is also helping to build up the skills of the whole team.
The first part, allowing us all to push theme updates to the 12412 site, required a little more thought.
One great feature of Github is its facility for post-commit hooks, which allows you to trigger a HTTP post to an external service whenever you push to the repository. Typically this is used a signal to the service that you want it to fetch the new commit from the repo and do something with it – in our case it would simply be to deploy the files. There are a couple of documented ways of doing this tailored to WordPress, requiring Git to be installed on your web server, and ultimately we’ve evaluated this method as being one to try in the future.
Back in the now, we still needed a simple but effective way to get our updates live. Of course you could manually copy files up after making a change in your local repo, but there is a risk of conflict here and over-writing other work if you’re not careful about pushing and fetching code from the origin. Perhaps there was something out there a bit neater.
If you’re familiar with WordPress’s theming system, you’ll know that they can be versioned through meta data in the style.css file. This is what triggers updates in your site’s admin panel when new versions of publicly hosted themes are released but it doesn’t work if the theme is hosted on Github. However a bit of searching turned up the Theme Updater plugin, which promises to do exactly that.
Now in our style.css file there is a new piece of metadata, Github Theme URI, which should be set to the location of the Github repo.
The Version of the theme is then incremented for each version we want to update with, using SemVer principles, and that commit in the repo is tagged with the same version number.
Finally, in the admin interface, the plugin polls for updates from Github in exactly the same way as a regular theme and – when a new tagged commit is seen – prompts for the update to be applied.
In this way we get to hook into WordPress’s own theme updater which means all the niceties such as switching to maintenance mode are taken care of.
So far it seems to work brilliantly. We may later on look at a post-commit hook to automate our updates, but for the time being the plugin is doing exactly what we need it to.
There is a slight caveat in that it only works with public Github repos, but I’m sure it wouldn’t be impossible to put some authentication in the plugin to access private ones – should you need it.
For my March 12412 project, I wanted to investigate ways to improve how one may provide user feedback in Objective C, from both the user’s and my (the programmer’s) perspective.
First, let me tell you one thing I dearly love about creating web forms: it’s so easy to provide salient user feedback where appropriate, and to connect that feedback to the related input. In fact it’s so easy that there really is no excuse to not do this. None. Even the most basic implementation will help increase conversion rates. Better feedback will increase rates further. Fundamentally, if a website doesn’t bother to tell a user what they’re doing wrong, they don’t bother trying to do it right!
A great example of one way feedback can be provided is on the PANmedia site’s contact form – PANMedia. If the user’s flailing attempts at entering their email address fail, the form will let them know. A user who knows what to do is one that might actually do it. This user feedback increases the likelihood that PAN will receive enquiries, which is one of the goals of that page.
None of us is without flaws
My secret love, Cocoa doesn’t make providing this feedback as easy for me. The paradigm is different: users on a website are accustomed to colours changing, forms morphing, elements popping in below input fields. Users of a desktop application, not so much. On top of that, websites are free to impose whatever colour scheme they like, while desktop applications have these imposed upon them.
In Cocoa, compared to an HTML form:
UI element positions are less flexible
The colour scheme is dictated by the OS. Therefore most applications will conform. It follows that changing text input background colours or related styling is generally a horrible idea
The GUI isn’t a DOM, there is no CSS & we don’t have jQuery, so popping labels in & out isn’t really feasible
The user is accustomed to all applications behaving in a certain way, and is alienated when something too wild happens, no matter how wonderful the feed back may be
Having covered what we can’t do, how do we politely let the user know that their sausage fingers have done it again, and “joe@gmailcom” isn’t a valid email address? Until now the programmer has had variations of the following options:
Use a modal alert (aka a popup)
Use an alert attached to the window (aka a modal sheet)
Write your own “ideal” solution
Do something broken, like change the input’s background colour
None of these are really ideal – the basic modal breaks the user out of the input view’s context, the sheet modal (although cool), doesn’t give targeted feedback, and finally, the “ideal” solution, well… I’ve never had time to write one, and if I did, I doubt it would be applicable to all situations (or as good as what you’re about to see).
He comes bearing gifts
Happily, and with great fanfare, along with OS X 10.7 Apple introduced the NSPopover, a way to display information related to specific items to the user. For me, this is the perfect solution – the popover allows one to display a message right next to the input in question. The popover will be consistent across applications, even those I don’t control (yes those exist). The popover is customizeable and may be filled with anything, anything I desire. Another popover? Don’t be silly.
My only issue after learning to use it was the amount of code / XIB work required.
If all I want to do is show a short snippet of text next to an input field, I dont really want to add 3-4 items to my XIB. If I didn’t do this, and opted instead for a completely programatic popover, I was looking at 10-12 lines of code. Neither was really good enough for me. In programming, the less one has to write, the less painful the code will be to debug in 3 months when all memory of it has vanished, and the code has transformed from the pinnacle of human achievement to something that appears to have been hacked together by a monkey on PCP.
One line of code would be perfect.
Enter the dragon
In the form of the Objective C category, one of my favourite features of the language. Categories allow the programmer to add methods to a class without being required to subclass it. I could, for example, add a category to NSPopover that allows me to initialise, configure and show a popover in one line. And so added, it was.
I christened the category “NSPopover+Message”, and it was good. It has reduced the complexity of creating and showing a text-filled popover from 10-12 lines of code or 3+ XIB items + a line of code to one line. Objective C, come here, let me kiss you.
Or, seeing as we’re not alone, let’s just let them see how good you look:
[NSPopover showRelativeToRect:[theInput frame]
ofView:[theInput superview]
preferredEdge:NSMaxXEdge // Show the popover on the right edgestring:@"Once upon a time, in a land far, far away, there was a little cake. It didn't last long."
maxWidth:250.0];
Conclusion
The NSPopover was something I’ve wanted to investigate for awhile, but until recently couldn’t think of a reason that wasn’t absolutely contrived. After using it in the recommended way: adding the NSPopoverController, NSPopover, an NSView & NSLabel to the XIB then showing it in my controller, I felt this was too much work. Doing the above in code was less work, but still not idea. Ultimately, I created a category of NSPopover that does this all for me. All one has to do is provide a view, a position, a string and a willingness to experiment. Cocoa, that pretty little thing, will do the rest.
Special treats for those who made it all this way
I’ve taken the liberty of making an example application showing this popover in action. It’s available on GitHub here: 12412 NSPopover+Message Example.
This is post about a CSS preprocessor; a controversial subject it seems that divides the web design community. I’m not going to go into the details of why you should or should not use a CSS preprocessor–plenty has already been written on that subject, including on this very site–and I’d suggest that you try one for yourself and make your own mind up either way. Personally I’ve been using Sass for a few months now and I’m totally sold on it, but one of the major selling points for Sass is its mysterious companion: Compass.
OK, so what is Compass?
Up until a few days back, I only had a vague idea of what Compass actually is. Sure, when I’d installed Sass via the command line (in those crazy times before CodeKit) Compass had also come along for the ride, and I’d seen tweets from various respected commentators singing its praises, but I’d never really known what it was intended for. So I set about delving into it and seeing what’s what. Here’s what I’ve found out so far…
As a simplified explanation, Compass is a framework of ready-made mixins (patterns of common and useful CSS) that you can integrate into your Sass projects. This cuts down on a lot of repetitive work that you would otherwise end up doing time and again across various web projects. Importantly, you are not locked into the framework – you can use as many or as few of the mixins as you wish, alongside your own mixins and CSS. And since Sass is compiled locally before you upload the final vanilla CSS files to your site, it will only include code from mixins that you have actually used in the project–any other used mixins will be ignored and omitted from the CSS, keeping things lean.
You may be thinking: but I can already find a huge number of Sass mixins for public consumption on the internet, why is this any different? Well for one thing, Compass is developed and curated by Chris Eppstein, one of the core Sass team, so there’s probably none better placed to understand what makes a good Sass mixin than he. Chris also encourages contributors to suggest additions and improvements to the core mixins, as well as integrating patterns from other popular frameworks such as Blueprint and H5BP.
Vendor prefixes, the stuff of nightmares?
Another huge plus for Compass is on the thorny subject of vendor prefixes, a subject that almost eclipses CSS preprocessors in it’s ability to induce nerd-rage (and rightly so). CSS3 is still a moving target and browser vendors are implementing new features, changing existing features and ‘embracing and extending’ the spec on what seems like a constant basis. The gradient syntax has been a perfect example of that.
Consider my current Sass mixin for a linear-gradient:
Which I can then invoke in my Sass-flavoured stylesheet like so:
.gradient-example{@include gradient;}
All well and good, but that relies solely on me keeping up-to-date on where we currently are with vendor prefixes for at least 7 browsers–possibly more in the future–and then modifying my mixin across all my projects if and when a change in the syntax takes place. It’s also not that flexible–I’ll have to write another mixin if I want the gradient to flow differently, or if I want radial gradients.
With my project running under Compass, I can instead use this rule in my stylesheet:
Compass does all the heavy lifting for me, I won’t go into detail on the mixin code it uses, but suffice to say it’s a lot more clever than what I’ve been using. If any of the prefixes or gradient syntax changes in the future, no problem–as long as I’m running the latest release of Compass all I’ll need to do is recompile the project again (a matter of seconds) and be happy in the knowledge that minds immeasurably superior to mine have already included this change in the framework… Compass has got my back, basically.
Are there any disadvantages?
As with the debate about CSS preprocessors in general, Compass is not a magic bullet that means you don’t need to learn and understand CSS in order to use it. There can be the temptation for users to lazily nest selectors where there really isn’t any need to do so, introducing potential specificity issues and unwieldy final code. Not really a fault of preprocessors per se, but they do make it easier for you to be sloppy.
There’s also the valid argument that you’re losing that fine-grained control of your own CSS; take this simple Compass mixin example that can be used to hide text (a bit like the older text-indent:-9999px rule):
Looks fine, but what if my text never had a text-shadow rule applied to it in the first place? We’ve just introduced a new rule to nullify a rule that was never there. A needless extra line of CSS.
That’s the rub I’m afraid. You need to be selective over the mixins you choose and make decisions on whether a slight bit of extra code is acceptable in any given task. That example above is a pretty minor case but since the Compass mixins have to be all things to all people, there will undoubtably be occasions where a number of rules within a mixin will not be directly related to the CSS code you have written. Of course, going back to my earlier point, you can use as many of the Compass supplied mixins as you want and still use your own mixins too so that can be negated somewhat.
How to start using Compass
Until recently you’d need to install Ruby and some Ruby gems, then run the framework from the command line. You can still do that of course. However, as I’m a long-term Mac user and scared of anything without a GUI layer, I’d recommend Compass.app by Handlino which works on PC, Mac or Linux, or CodeKit by Bryan Jones which is only available on a Mac. Both make it incredibly easy to get a Compass project up and running. There’s probably other apps available that fulfill the same task, and I apologise in advance for not mentioning them, but those are the two I have used myself.
The reference for all current core Compass mixins are listed on the Compass website, as are mixins for Blueprint. It may take you a while to learn all the available mixins but it’s well worth the time to scour those reference pages. However, I did find it easier to clone the Compass GitHub repo and peruse the /frameworks/compass/stylesheets directory in order to find all the mixins quickly.
Further investigations
I’m sure I’ve only scratched the surface of what Compass can do, and I’m certainly no expert on it–that’s why I’m writing about it here on 12412. If and when I find out more I’ll post another article. In the meantime, feel free to leave any comments you have on Compass. And no, I don’t care whether Less or Stylus are better than Sass. There, I said it (but I’ve provided links to both of them here for balance)!
February’s project for me turned out to be something completely different to what I had originally intended. In fact it wasn’t until I was most of the way through it that I realised it even was a 12412 project. It’s maybe skirting around the edges of learning a little, as it was working with two things I was already familiar with, but combining them in a new way has turned out to be a useful experience.
What faced me a couple of weeks ago was the build of a new site for the Sidekick Studios project League of Meals, supporting its pilot phase in establishing ‘cook clubs’ to bring older people together in a communal, social kitchen with the potential to support local communities and get people eating more healthily.
Working from UX sketches and an overall brand design we initially started to put the site together in WordPress but, as the type and layout of content we were going to be adding was still in a state of flux, I wanted something with a little more short-notice flexibility. Ideally, I wanted Perch.
What is Perch?
Perch is a lightweight content management system, running in PHP, which makes it really easy to make pages editable without giving over control of layout or templating. Unlike most CMSes, which use a page-based model, Perch works through defining content regions in positions the developer chooses which are then reflected in an admin view.
I’ve used it on a couple of sites in the last year and I really like its minimal approach – I’ve never been totally comfortable with the way that the likes of WordPress are contingent on creating a “theme” when what you actually wanted to build was a “website”. I understand why it does it the way it does, and for sites that are in that mindset it works well, but the smaller-scale projects I tend to do more often than not have minimal content management requirements or a design that is far enough away from the WordPress model to make it easier to start from scratch and wire in Perch.
However as the CMS doesn’t generate its own pages from a template*, you’re still stuck with the task of development of actual pages to put content regions in to. It might sound like a slightly backwards way of working but I wanted a way to eliminate, if not all, then at least most of the rigmarole of doing this. The Slim framework for PHP seemed like a good bet.
What is Slim?
The Slim framework is something I discovered during last year, after working on an ASP.net site with a RESTful HTTP router and wanting something similar in PHP. It is exactly that, the idea being that you define ‘routes’ which are available via GET, POST, etc and then a .htaccess rule rewrites these via an index.php file in the route of the website to display whatever templates you require.
A very simple Slim app defining the homepage route for our site. When the user hits the root of the site (/) the templates listed in the callback function are rendered as that page.
In this way you can add and change routes quickly and easily without needing to maintain a directory structure of ‘real’ pages and chop and change files between them as the site grows more complex. With the front controller pattern, your page components live in the ‘templates’ directory and nowhere else.
The file structure of the project. Everything that will be output as HTML sits in the ‘template’ directory regardless of its eventual location.
This conflicts with the way that Perch works out of the box, which relies on picking up the location of the page a content region is in to give it a meaningful label in the admin view. However I was determined to use a HTTP router pattern given the fluid nature of the site’s content and set to trying to make them work nicely together.
Wiring them up
Actually I needn’t have worried too much. Perch worked immediately with the Slim-powered site – the only slight weirdness being that it couldn’t work out what page a particular content region was on due to all requests running through that root index.php file, and so lumped them into the ‘home page’.
The developers of Perch had in fact already anticipated this scenario, and there is a special work around for sites that use front controllers. By simply explicitly telling Perch, in each Slim route, what ‘page’ it is supposed to be on, the admin view corrected itself and harmony was restored. It’s necessary to give this page setting an index.php file, even though that file doesn’t technically exist – this is simply a quirk of Perch.
One other problem was that the .htaccess file which makes Slim work in the first place was intercepting requests to the /perch directory where the admin interface lives and returning a 404 error – there was no route for it so Slim didn’t know what to do. To get around that I looked a few ways to modify the .htaccess file to ignore the Perch directory, but in the end found the simplest method was to add a further .htaccess file in that directory with the single rule ‘RewriteEngine Off’. Once this was done the Perch admin interface was back in business, and individual and shared content regions in the pages worked perfectly.
The League of Meals site
So that brings us to the League of Meals site. As well as the initial content pages, there was also a requirement for a simple blog for which the relevant Perch add-on was used. This does work more in the traditional page-based model, and to get this running in the Slim application I needed to both define the routes for the blog archive and posts – using a route parameter for the post slug – and also edit the default templates to link to these routes rather than Perch’s query string style navigation.
The blog post route, using a route parameter – “:slug”, which becomes $slug in the template – to ensure the correct post is fetched from the database.
The final thing I wanted was a dynamic navigation which could understand the route of the page it was on, with a selected state where appropriate. To achieve this I defined, in the root index.php file, an array of URLs and labels which is looped through in the page head template to render the navigation. Also in the head template we’re checking the page route from the Slim class and doing some comparison to check if that item should be selected – i.e. if we are on its page. This took a little wrangling, as I couldn’t do an exact comparison due to the presence of blog post slugs, so I had to use PHP’s strstr function to try and find the nav item key (e.g. /blog/) in the page route (e.g. /blog/2012-02-21-see-what-happened-at-our-first-cooking-club). A side effect of this was that the home route (/) would always match so there needed to be a little extra logic for this case.
Rendering the navigation and checking selected state against the route
Conclusions
Matching Slim and Perch together is something I’d intended to try for a while now, and I’m glad it turned out to be fairly straightforward given the short duration of the project and that I had banked on this way being easier and quicker than WordPress. In fact we came in one day under budget on development, and had time to make sure the site design was implemented in a responsive way.
Would I do it this way again? On a site that needed to be built in PHP, absolutely! However I am hoping that a focus on Node during March will lead me towards a utopian land of Javascript everywhere, and my days building sites in this way are numbered.
* Yes there is a ‘pages’ app add-on for Perch, but this didn’t seem appropriate for the very different page layouts across the site and its frankly simple enough to not need it.