Manually control <head> markup in Joomla

狂风中的少年 提交于 2019-12-06 03:52:57

If you are planning for a template development and you need all your template data get separated from Joomla libraries or core file (the head section).

Normally the head section include will works like

<jdoc:include type="head" />

it loads the content from libraries libraries\joomla\document\html\renderer\head.php

If you want to override the content of head you can make a module for your task. Just create a module and include that module instead of this head make sure that have all required codes added to work $document Class otherwise it miss a lot off features of Joomla regarding document class

As explained by the answer from Jobin, normally, you would include the head data by using the <jdoc:include type="head" /> tag, but if you want more control over this, you can use the JDocument.

Example code in your template's PHP:

$doc = JFactory::getDocument();
$my_head_data = $doc->getHeadData();

This will give you an array of the data that JDocument would normally print, so that you can completely choose what to print and how.

19cool53

To make jQuery load from CDN and get it on top of the script list, I made a little patch just after the $doc = JFactory::getDocument(); that manipulates the header array directly inside the $this object as follows:

$my_jquery    = "//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js";
$my_jquery_ui = "//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js";
$my_jquery_cx = $this->baseurl."/media/jui/js/jquery-noconflict.js ";

foreach($this->_scripts as $k=>$v) {
// put own jquery.conflict && jquery-ui && jquery on top of list
    if( strpos($k,'jquery.min.js')) {
        unset($this->_scripts[$k]);

        $r = array( $my_jquery_cx => $v);
        $this->_scripts = $r + $this->_scripts;

        $r = array( $my_jquery_ui => $v);
        $this->_scripts = $r + $this->_scripts;

        $r = array( $my_jquery => $v);
        $this->_scripts = $r + $this->_scripts;
    }
    else if( strpos($k,'jquery.ui.min.js')) {
        unset($this->_scripts[$k]);
    }
    else if( strpos($k,'jquery-noconflict.js')) {
        unset($this->_scripts[$k]);
    }
}

Replace $my_jquery_xxx with editable config parameters in your templateDetails.xml file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!