0

I have an upcoming project where it may be necessary to have a mobile site and a desktop site as the desktop design will not work well responsively.

I built a similar site recently in ExpressionEngine and used Multiple Site Manager (MSM) which allowed me to have separate templates with the same url structure pulling content all from the main site. Since that build I've starting moving new projects to Craft and would like to find a solution to this.

Since Craft does not have a feature similar to MSM, at this point, what would be the best way to approach this. Urls need to be the same on both domain.com and m.domain.com with all content being sharing the same channels.

2

To use separate templates I created plugin and within init() method I overwritten templatesPath variable. Seems to be working although you need to test it with other plugins as I have tested it with fresh Craft instance only.

public function init() {
    $templatesPath = craft()->templates->getTemplatesPath();

    if(craft()->request->isMobileBrowser()){
        $templatesPath .= 'mobile/';
    } else {
        $templatesPath .= 'desktop/';
    }

    craft()->templates->setTemplatesPath($templatesPath);
}

isMobileBrowser()

This example assumes that templates folder contains mobile and desktop folders. Both have a mirror template files structure. Also you don't need m.domain.com anymore.


But it you need m.domain.com you can use multi environment config variables (environmentVariables) or stick to the previous solution.

return array(
    '*' => array(
        'templatesPath' => 'desktop',
    ),
    'm.domain.com' => array(
        'templatesPath' => 'mobile',
    ),
);

public function init() {
    $templatesPath = craft()->templates->getTemplatesPath();

    if(craft()->config->get('environmentVariables')['templatesPath']){ //this line changed
        $templatesPath .= 'mobile/';
    } else {
        $templatesPath .= 'desktop/';
    }

    craft()->templates->setTemplatesPath($templatesPath);
}

To force mobile URL insert this code to desktop base layout file:

if(craft.request.isMobileBrowser()) {
    {% redirect 'http://m.domain.com' %}
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.