Oliver Ker Design

Hire Me

iPhone site version using Textpattern

UPDATE: I have written a new post of a newer and slightly more complicated method to display a mobile version – please use this one if it suits you better.

If you use Textpattern and wanted to create an alternative iPhone version for faster and slimed down browsing, then I think I’ve finally done it!

I have been on the quest for this on and off for the last month or two ever since I read(skimmed) over the alternative site version article by the Erskine boys which set me thinking how to replicate this in Textpattern. I am no php programer but know how to copy and paste, and have been searching for the piece of code to serve up a different site for iPhones.

I came across the javascript snippets that detect the browser and give a different stylesheet, this was ok, but I still wanted to build a second website that used the same textpattern database, so that when I ever updated the site it would update the iPhone version without any thought.

Multiple sites are available in textpattern, and even though I did not try this method, what I read about it, I couldn’t see how it would show a different site template for the same database – Maybe I missed that?

How I did it

I came across this article that uses php to detect the browser and shows a specific stylesheet.

I took this code to for the basis of my iPhone version of my site. I have simplified this below to make it easier to follow as I use many different forms in my install to get the desired output.

I placed this code at the top of a page template.

<?php
if (ereg('iPhone',$_SERVER['HTTP_USER_AGENT'])) {
$iphone = 1;
} 
elseif (ereg('iPod',$_SERVER['HTTP_USER_AGENT'])) {
$iphone = 1;
} 
else {
$iphone = 0;
}
?>
<?php if ($iphone == '1'): ?>
<![if !IE]>
<txp:output_form form="iphone-head" />
<meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
<link rel="stylesheet" type="text/css" href="iphone.css" media="only screen and (max-device-width: 480px)"
charset="utf-8" />
<txp:output_form form="iphone" />
<![endif]>
<!-- If it's NOT an iPhone, show the normal CSS and no Viewport Meta tag -->
<?php else: ?>
<txp:output_form form="head" />
<txp:output_form form="browser" />
<?php endif; ?>

I normally build my sites in one page template using if_section statement to display section specific content, so all I have to do was copy and paste all that information in to a form which I called browser and just call that in with output_form tag.

So you can see that by building a separate site in the iphone form (just like you normally would in the pages area) that it will output a different site and style but uses the same database of information in textpattern.

I have started to build the iPhone version of my site, so you can see how I’m getting on by viewing http://www.oliverker.co.uk on your iPhone.

, ,

Comments

  1. # Zander 281 days ago

    Just so I have it clear, are your output forms above the full page code with article tags etc for each version of the site?

    I run jQuery Style and currently use a jQuery to redirect iPhone users to a specially formatted subfolder for the iPhone part of the site, I think your solution would be much more lean. I would not have to load any jQuery at all. Am I correct?

    Could the code be modified to redirect the user to a subdirectory instead?

  2. # Marty 281 days ago

    Thanks for this. It’s an area I’m getting more interested in. How would you go about giving the viewer a choice as to what version of the site they see?

  3. # Oliver Ker 281 days ago

    @Zander
    You would not have to load any jQuery, uses php only. Then in my iPone form I load the basics, I take away any unnecessary jQuery libraries etc.
    I would have thought It could be redirected to a subdirectory, but Im not a backend programmer so not 100% sure how to do it.

    @Marty
    Im not sure yet, it is something I thought about, and will be looking into soon, if you find the best way, let me know.

  4. # Marty 274 days ago

    I’ve been thinking about this some more and came across this in the textpattern forums. Perhaps setting a cookie to ‘remember’ the users choice in conjunction with this plugin might be another way to approach this.

  5. # Zander 274 days ago

    Oliver, I just wanted to say a massive thanks for sharing this article, I have improved my blog making it much faster on the iPhone and also used the if statement in a few other places around the site, for example I have images that are already dynamically resized, so I needed them to be resized differently for the iPhone, I used this if statement again and tada… perfect images on my iPhone – Awesome!

    You should think about submitting a more detailed tutorial on TXP Tips because I think people will find it really useful to be able to selectively show/hide content depending on the user agent.

    Thanks again

  6. # Marie Poulin 238 days ago

    Nice work Oliver, will DEFINITELY be checking this out!
    :D

More posts

Archive