Switch error reporting to maximum so you can see which part fails (and IF it's K2) as I'm working primarily on PHP 7.2 locally and have no issues at all.
As for sub-templating, JoomlaWorks was literally the first Joomla company to launch such features in its products. That's because we understand CM in CMS. What you want to do is probably called adaptive design/development and it's really not that hard.
In your template's index.php, this is what you need to have different subtemplates per device type:
<?php
// URLs with "?m" or "&m" render mobile pages - much like Google's Blogger does / "?force" or "&force" is used to force the desktop view
if(isset($_GET['m']) && !isset($_GET['force'])){
include('index.mobile.inc.php');
} else {
include('index.desktop.inc.php');
}
Then you need a system plugin (using the onAfterInitialise event) to set a PHP constant like
define("SITE_VIEW", "mobile"); or
define("SITE_VIEW", "desktop");:
// Desktop or mobile
if(isset($_GET['m']) && !isset($_GET['force'])){
define("SITE_VIEW", "mobile");
} else {
define("SITE_VIEW", "desktop");
}
And finally, in your html overrides under /html/com_k2/ you simply check for the value of
SITE_VIEW to determine if the user visiting the site is using a mobile or desktop browser.
You would also need the JS redirect in your desktop sub-template, something like:
<!-- Browser detect and redirect -->
<script type="text/javascript">
(function() {
// Get URL query strings
function getQS(v) {
var qs = window.location.search.substring(1);
var qsArray = qs.split("&");
for (i = 0; i < qsArray.length; i++) {
var qp = qsArray[i].split("=");
if (qp[0] == v) {
return true;
}
}
}
if (getQS('force')) {
document.getElementsByTagName('a').onclick = function(el) {
if (el.href.indexOf('?') > 0) {
el.href = el.href + '&force=1';
} else {
el.href = el.href + '?force=1';
}
}
}
if (getQS('m') === undefined) {
if (getQS('force')) return;
var curUrl = window.location.href;
var userAgent = navigator.userAgent || navigator.vendor || window.opera;
var getLastChar = curUrl.substr((curUrl.length) - 1);
if (((/Android/i).test(userAgent) && (/Mobile/i).test(userAgent)) || (/BlackBerry|iPhone|iPod|Opera Mini|IEMobile/i).test(userAgent)) {
if (curUrl.indexOf('?') > 0) {
window.location.replace(curUrl + '&m=1');
} else {
window.location.replace(curUrl + '?m=1');
}
}
}
})();
</script>
The above makes sure to set ?m or &m and handle ?force or &force if there's a link to enforce the desktop view in your site.
I'm just giving you an idea on how to implement things here, it's not THE solution of course. But the above can work great with any type of caching, either Joomla-based caching or stricter server-side caching e.g. via Varnish or Nginx.
If you need more info, I could make a blog post with more details.