I am able to detect mobile. When mobile is detected i set the constant SD_IS_MOBILE = true.
In my script i display a smarty template like so:
$smarty->display("page.tpl.html");
In the template directly 2 template files exist:
page.tpl.html.d
page.tpl.html.m
d is for desktop, m is more mobile.
Smarty will not find the template i'm asking for (which is what i expect), and then it uses my default_template_handler function to load the correct one.
function __default_template_handler($resource_type, $resource_name, &$template_source, &$template_timestamp, $smarty_obj) {
if ($resource_type == 'file') {
if (SD_IS_MOBILE && file_exists(SD_TEMPLATE_ROOT.$resource_name.".m")) {
$template_source = file_get_contents(SD_TEMPLATE_ROOT.$resource_name.".m");
$template_timestamp = filemtime(SD_TEMPLATE_ROOT.$resource_name.".m");
$resource_name = $resource_name.".m";
return true;
} elseif (file_exists(SD_TEMPLATE_ROOT.$resource_name.".d")) {
$template_source = file_get_contents(SD_TEMPLATE_ROOT.$resource_name.".d");
$template_timestamp = filemtime(SD_TEMPLATE_ROOT.$resource_name.".d");
$resource_name = $resource_name.".d";
return true;
}
return false;
}
}
problem is that smarty compiles both with the original name "page.html.tpl". How can I get it to compile with the ".m" and ".d" extensions? I thought i could just change the default_template_handler functions resource type parameter to pass by value and change the resource name (as i am doing above), but smarty won't let me.
I believe i have found an answer. Override the display and fetch functions of smarty, calling the template with with an m or d cache and complile id.. like this:
function display($var) {
if (SD_IS_MOBILE) {
parent::display($var, "m","m");
} else {
parent::display($var, "d","d");
}
}
function fetch($var) {
if (SD_IS_MOBILE) {
return parent::fetch($var, "m","m");
} else {
return parent::fetch($var, "d","d");
}
}
I haven't run the entire application with this code in place yet.. but it seems to work so far.. and in combination with my above default template handler, i believe i have found a unique way to handle mobile smarty templates without the need for duplication.