Context Rules

From bemoko developer wiki

Jump to: navigation, search

What are context rules?

Context rules allow a site development to define rules which can be triggered from a URL request to change how the context is resolved. For example this allows you to define a keyword such as "320", which when included in the URL, will force the rendering of the site to be what would be delivered to a 320px device.

We use this feature on our website to provide live demos of mobile sites.

How do I enabled context rules?

In your site-config.xml include configuration as follows:

  <context>
    <rules>
      <rule name="iphone">
        <param action="replace" name="user-agent">Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16</param>
      </rule>
      <rule name="320">
        <param action="replace" name="user-agent">Mozilla/5.0 (SymbianOS/9.1; U; en-us) AppleWebKit/413 (KHTML, like Gecko) Safari/413 es61i</param>
      </rule>
    </rules>  
  </context>

If you include these rules in a site called test' with an intent called i, then you can activate the iphone rule by including the iphone rule in the URL as follows:

http://mysite/test/iphone/i

Notice that the iphone rule is included in the URL after the site name and before the intent name.

Advanced Usage

Essentially the context rule allows you to trigger alternative HTTP headers by providing URLs to the user with the context rule in. The example above shows you how you can change the user-agent HTTP header - the user-agent is the primary mechanism by which a device identifies itself.

You could however control other parameters, for example to emulate a request from a blackberry without CSS enabled you can include the following rule

<rule name="bb8310-nocss">
  <param action="replace" name="user-agent">BlackBerry8330/4.5.0.66 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/-1</param>   
  <param action="replace" name="accept">text/html</param>   
</rule>

Version History

This feature is available from v1.2.5 of bemokoLive.