• Join Now
  • Sign In
  • Cart
  • Instant Setup
  • Download
Logo
  • About
    • Try it Out
    • For Developers
    • For Agencies
    • For Designers
    • For Anyone
    • Testimonials
    • Showcase
    • History
    • Our Philosophy
    • Credits
    • What does free mean?
    • Blog
  • Community
    • Members
    • Forums
    • Chat
    • Karma
    • International
    • Jobs
    • eNewsletters
  • Developers
    • Download concrete5
    • Join Beta Team
    • Translate concrete5
    • Bug Tracker
    • Submit to Marketplace
    • Code Submissions
    • News
    • Community Leaders
  • Marketplace
    • Add-Ons
    • Themes
    • Add-on Installation
    • Deal Of The Day
    • Swag
    • Theme Contest 2012
  • Services
    • Hosting
    • Support Options
    • Consulting
    • Training
    • Enterprise
  • Documentation
    • Getting Started
    • Editor's Guide
    • Developers Guide
    • How-Tos
  • How-Tos

Building an iPhone-Optimized Version of a concrete5 Theme

Posted byfrz in Designers on Jan 16, 2010.
4 people like this.

Originally posted by andrew at andrewembler.com

 

In the world of smartphones, the iPhone was a game-changer. We can debate the relative merits of the various players, but it can't be denied that, when it debuted, the iPhone brought a lot to the table. Nowhere is this more apparent than when using Mobile Safari. The world finally had a usable, standards-compliant mobile browser.

However, something interesting occurred almost immediatley after the iPhone was released. Web designers began creating iPhone-specific websites. These websites use stylesheets (and sometimes entire images and content) to tailor their form factor and presentation specifically to Mobile Safari. They will also frequently ape the iPhone aesthetic. 

Initially, this puzzled me. If web designers are building standards-compliant websites with decent liquid layouts, Mobile Safari displays them quite well, and pretty quickly. For example, this site, when viewed on Mobile Safari, displays impressively well, with no tailoring toward Mobile Safari in particular. 

However, as I started thinking about it, I realized that most websites aren't exactly like your garden variety blog. They have grown to include lots of JavaScript, images, and extraneous cruft that can slow down rendering on the iPhone's more lightweight processor. In these cases a mobile version can make a noticeable difference – even though the regular website could still easily be viewed. 

Enter c5touch.

With all of this in mind, I present to you c5touch, a Mobile-Safari-optimized concrete5 theme. 

footer image

c5touch is based on an impressive Wordpress theme named WPTouch. If you run a Wordpress blog, I highly recommend it.  c5touch doesn't do half of what WPTouch does. This is partially because Wordpress, by its very nature, is a much more structured system than concrete5. But it's also because c5touch is young, and is being made available more as a simple exercise in what's possible in concrete5 with a little bit of tweaking. I would love for some enterprising soul to extend it in further directions. Consider this initial offering a proof of concept.

Download, Installation and Setup Instructions

concrete5 makes it easy to install and activate themes. c5touch is a little bit different, however, for one reason: it's not the only theme you're going to have on your site. c5touch compliments your site's main theme. You install c5touch, but you leave your site's main theme (or themes) activated, and add some code to your site to enable the usage of c5touch when concrete5 detects your site is being requested by Mobile Safari.

Before You Start

1. Download c5touch

Grab c5touch from here.

2. Install c5touch on your site.

FTP c5touch to the local themes directory on your site. Unzip it and install it through the dashboard. Don't activate it! (For more information on how to install a theme in your concrete5 site, check out this link)

3. Modify concrete/libraries/view.php

As of this writing, the latest stable release of concrete5 is 5.3.3.1. This tutorial requires a change to the core which have not yet been released. You can, however, add the change to the core file directly, confident that it will eventually be released.

First, locate the comment that says

// Extract controller information from the view, and put it in the current context

And add the following line above, so your source looks like:

Events::fire('on_start', $this);
 
// Extract controller information from the view, and put it in the current context
 


4. Enable application events for your site.

Enabling support for concrete5's application events allows site developers to hook custom application and website code into common concrete5 actions, without having to modify the core. These must be enabled first, however. Add the following line to config/site.php:

  1. define('ENABLE_APPLICATION_EVENTS', true);


5. Hook into the global "on_start" event.

The on_start event (which we added to older concrete5 versions in step 3) is run by the view library at the very beginning of a page rendering session. This is before a theme or controller is loaded. It is here that we wish to check which browser is requesting our site and, if necessary, switch to our iPhone-optimized theme.

First, create a file named site_events.php in the config/ directory. Then, paste the following code into this file.

Events::extend('on_start', 'ThemeSwitcher', 'checkForIphone', 'libraries/theme_switcher.php');

This code may look complex, but it's fairly simple. The first argument, "on_start" is simply the name of the concrete5 application event we'd like to hook into (note, this is separate from controller-level or page-type-level events.) The second argument, "ThemeSwitcher" is the name of a class we're going to call a method in. The third argument, "checkForIphone" is the method in the ThemeSwitcher class that we're going to call. Finally, the fourth argument tells us where we can find the ThemeSwitcher class.

6. Create the ThemeSwitcher class at the proper location.

Now we need to create the proper class file at the location specified in step 5. Create an empty file in your local libraries directory, named "theme_switcher.php" Then, paste these contents into the file (minus the line numbers.)

  1. <?
  2.  
  3. class ThemeSwitcher {
  4.  
  5. public function checkForIphone($view) {
  6. if (strstr($_SERVER['HTTP_USER_AGENT'], 'iPhone') ||
  7. strstr($_SERVER['HTTP_USER_AGENT'], 'Android') ||
  8. strstr($_SERVER['HTTP_USER_AGENT'], 'webOS')) {
  9. $iphone = PageTheme::getByHandle('c5touch');
  10. $view->setTheme($iphone);
  11. }
  12. }
  13.  
  14. }

This is code is quite simple. It simply checks to the current request's user agent to see if it contains keywords that indicate the user is using a mobile webkit device. (Update: This now includes Android and Palm's webOS.) If so, we grab the theme object for the c5touch theme and render the view (which is passed as an argument to the method) using that theme.

And that's it! Bask in the glow of iPhone compatibility. 

Additional Notes and Other Helpful Tips

Now, you'll probably notice that there is still a bit of work to be done to get your site to look top-notch on the iPhone. c5touch is a starter theme, but you'll still likely have to create page types and some custom style rules to really get everything to look great. Here are some things I've done on andrewembler.com to go the extra mile.

1. Add some of your main site's css to the iPhone optimized theme by using c5touch/site.css.

Your site may look somewhat drab. Copy some of the block-level and reusable styles that you're using throughout your site from your main theme, into the c5touch site, using the empty site.css stylesheet. For example, all my masthead images have drop shadows and some other effects applied to them. I copied this from my main theme into my iPhone-optimized theme.

2. Scale images

While my text looked great when I started using c5touch, the images within the content space were much too large, and looked silly. So I'm using the following custom code in my image block view template to scale them down.

In your site's empty root-level blocks/ folder, create a folder named image, and copy concrete/blocks/image/view.php into blocks/image/. Then, open it in a text editor and make the changes below:

  1. <div class="main-image">
  2. <?
  3. $v = View::getInstance();
  4. if ($v->getThemeHandle() == 'c5touch') {
  5. $ih = Loader::helper('image');
  6. $ih->outputThumbnail($f, 275, 1000);
  7. ?>
  8. <? } else { ?>
  9. <div><img src="<?=$f->getRelativePath()?>" class="footer-image"
  10. width="<?=$f->getAttribute('width')?>" height="<?=$f->getAttribute('height')?>"
  11. alt="footer image" /></div>
  12. <? } ?>
  13.  
  14. <? if ($altText) { ?>
  15. <div class="main-image-caption"><?=$altText?></div>
  16. <? } ?>
  17.  
  18. </div>

This code ensures that, when images are displayed in the c5touch theme, they will be scaled down, using concrete5's autoresize algorithm (which takes care of caching, transparency, etc...)

3. Experiment!

This tutorial is meant as a starting point. Currently, c5touch is by no means a drop-in solution. If there's enough interest, I'll investigate expanding this into a concrete5 package. This would be ideal, as the package format could automate all of the various steps that currently have to be performed by a developer. But in the meantime, I'd love feedback from anyone, as well as suggestions. I hope this proves useful. 

Recent Discussions on this Topic

  • View More
  • Ask a Question

images within a content block

My question is posted here: http://www.concrete5.org/index.php?cID=30241&bID=713&arHandle=Comments&ccm_token=1271224207:434e255987ed827060010da21aa20aea&btask=passthru&method=form_save_entry#guestBookForm-713
Started by medhatmsm on 04/14/10 at 1:44 AM

1

reply

Anyone followed the iphone tutorial and have it working?

Wondered if anyone had followed thiscand got it working? Not sure what i am doing wrong but get this error: Events::extend('on_start', 'ThemeSwitcher', 'checkForIphone', 'libraries/theme_switcher.php');
Started by pixelfish on 03/28/10 at 11:04 AM

6

replies

Complete list of Mobile User Agents for c5touch

I have compiled a list of mobile browser user agents for andrews tutorial. It is more complete, so will increase the amount of visitors that get correctly redirected. if you have any trouble pm me. you can put this in theme switcher.php in the /libraries …
Started by 12345j on 10/03/10 at 11:45 AM

7

replies

Mobile them works great as long as full page caching is not enabled.

As described in the tutorial by Andrew there is an event trigger setup to detect for mobile browsers and then change the theme on the fly. This works only when the page caching is not enabled. For example, when a page has not yet been cached and is vis…
Started by ppcc on 01/20/11 at 2:38 PM

16

replies

mobile theme

how do I go about making a mobile image gallery?
Started by bryanlewis on 03/18/10 at 11:49 AM

4

replies

How-To Tags

iPhone, mobile, android, iPod, phone

Related How-Tos

None.

  • Documentation
  • How-Tos
  • Designers
  • Building an iPhone-Optimized Version of a concrete5 Theme

Do you have questions

  • What are users saying?
  • Who is using concrete5?
  • What makes concrete5 easy?
  • Why develop on concrete5?
 

We’re on “The Twitter”

管理画面のJobsはどこにいったかか分かりますか ( #concrete5 live at http://t.co/W3tCUvlA)

Follow concrete5

About

  • Try it Out
  • For Developers
  • For Agencies
  • For Designers
  • For Anyone
  • Testimonials
  • Showcase
  • History
  • Our Philosophy
  • Credits
  • What does free mean?
  • Blog

Community

  • Members
  • Forums
  • Chat
  • International
  • Jobs
  • eNewsletters

Developers

  • Download concrete5
  • Join Beta Team
  • Translate concrete5
  • Bug Tracker
  • Beta
  • Submit to Marketplace
  • Code Submissions
  • News
  • Community Leaders
  • User Doc Group

Marketplace

  • Add-Ons
  • Themes
  • Add-on Installation
  • Deal Of The Day
  • Swag

Services

  • Hosting
  • Support Options
  • Consulting
  • Training
  • Enterprise

Documentation

  • Getting Started
  • Editor's Guide
  • Developers Guide
  • How-Tos

Legal

  • Privacy Policy
  • Terms of Use
  • Refund Policy
  • Contact Us
© 2008 to 2012 Concrete CMS Inc. All Rights Reserved.

Sign In?

You must have a user account and be signed to perform this action.

  • Sign In
  • Register