Using MOBILE_THEME_IS_ACTIVE for custom block views

Permalink
Hello,

I am working on a mobile theme of my site using the built in theme switcher. The proper theme is being loaded based on device, however I want to use MOBILE_THEME_IS_ACTIVE with an if/else statement to create different views for blocks depending on which version of the site a user is seeing.

Within the view.php file of affected blocks, I am using
<?php 
if (defined('MOBILE_THEME_IS_ACTIVE')){
   //layout for mobile here
}
else {
  //layout for non-mobile here
} 
?>


Unfortunately both mobile devices and desktops are using the mobile version of my view.php file. Please help!

cramaekers
 
mkly replied on at Permalink Reply
mkly
Hello,
Two things that might help.

1. You can count on MOBILE_THEME_IS_ACTIVE already being set and in fact it is always defined(either true or false). By default it is false. So you can safely do this.
if(MOBILE_THEME_IS_ACTIVE) {
  // mobile stuff
} else {
  // desktoppie stuff
}

If you want to be super safe you can always just do
if(
  defined('MOBILE_THEME_IS_ACTIVE')
  &&
  MOBILE_THEME_IS_ACTIVE
) {
  // mobile stuff
} else {
  // not so mobile stuff
}


2. Be careful about Block Caching when doing mobile/desktop views. You should place
protected $btCacheBlockRecord = true;
protected $btCacheBlockOutput = false;

in the top of your block's controller.php BlockController class. This will prevent sites with Basic Cache enabled from only serving one or the other(first one displayed after a cleared cache) to everyone who visits your site.

Best Wishes,
Mike
cramaekers replied on at Permalink Reply
cramaekers
Mike,

Thank you so much. Such an easy fix... that's why I love the community here! You rock!
mkly replied on at Permalink Reply
mkly
Great to hear.