I have very little php knowledge. I know I'm doing something wrong, but what?
I'm adding the sass compiler from Joomla Master Bootstrap template into my own Joomla template. It works, but not as it should. I can't turn it on or off in the backend. The code in Masterbootstrap is this:
include 'includes/params.php';
if ($params->get('compile_sass', '0') === '1') {
require_once "includes/sass.php";
}
In my template I don't have params.php, but /functions/tpl-init.php. I added the variable $compile_sass there. My code looks like this:
require_once __DIR__ . '/functions/tpl-init.php';
if ($params->get('compile_sass', '0') === '1') {
require_once "includes/html/sass.php";
}
Doesn't work. I get the error: Call to a member function get() on null
So I changed this into:
require_once __DIR__ . '/functions/tpl-init.php';
require_once "includes/html/sass.php";
That works, but now the compiler is permanently on.
What should I do to correct this?
To get template params from outside the template you need to do this
Template parameters from outside a template
$app = JFactory::getApplication('site');
$temp = $app->getTemplate(true);
$param = $temp->params->get('compile_sass', '0');
Remember the Masterbootstrap template should be active.
To get any template param you need to do this
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query ->select('params')
->from('#__template_styles')
->where('`template` = ' . $db->q('masterbootstrap')) // Check Spelling of template
->where('client_id = 0'); // client_id = 0 for Site and client_id = 1 for Admin
$db->setQuery($query);
$params = json_decode($db->loadResult());
echo $params->compile_sass;
Better is to create a similar param in your own template using your templateDetails.xml file rather than depending on the external template.
I got help from a friend. He solved it in a minute. It's so simple... it usually is, after you found the solution:
require_once __DIR__ . '/functions/tpl-init.php';
if ($compile_sass === '1')
{
require_once "includes/html/sass.php";
}