There are two ways you can modify what is written to your head section.
- Use the JDocument methods to insert meta and script tags in the head section.
- Hard code the meta and script tags directly in the template index.php file.
This article is about option number one. You first need to get an instance of the current document and call the methods to modify the head. The head section tags will be added before the page is rendered.
//First get the current document object
$doc = &JFactory::getDocument();
//Will create a empty generator meta tag. If you dont want
//everyone to know site is running on Joomla.
$doc->setGenerator();
//Sets the description meta tag
$doc->setDescription('some desc');
//sets the title tag
$doc->setTitle('Some Title');
//create a meta tag
$doc->setMetaData($name,$content);
//adds a linked style sheet
$doc->addStyleSheet('/path/to/file')
//adds a linked javascript or other type of script file
$doc->addScript('/path/to/file')
//add a custom tag. Use to add any kind of tag to the head section.
$doc->addCustomTag();
//Add custom javascript code snippet. Pass in javascript code and
//will add the <script> tags for you. Joomla places these snippets
//after placement of addScript() scripts.
$doc->addScriptDeclaration()
//Add custom css style snippet. Pass in css styles and will add
//the <style> tags for you. Joomla will place these styles after
//placement of addStyleSheet() styles.
$doc->addStyleDeclaration()
Can also include external javascript and css files in the head using JHTML object
//This will create a script tag in the head loading a jquery
//file located in script/js/ folder.
JHTML::script('jquery-1.4.2.min.js','script/js/');
//This will create a link tag in the head loading a style sheet.
JHTML::stylesheet('template.css', 'script/css/');
Here is the api document for JDocument and this is the doc for jDocumentHTML which inherits from JDocument and used to generate the normal html output.
Very good info, i was searching for this from last week, saved a lot of time, thank you
Thanks! i was going crazy looking for something like this, this is exactly what i needed. Two thumbs way up!
This is what I need, but I’m new at Joomla. Where do I put this code? And maybe how, what menu should I go through?
Thanks
You can place this code just about anywhere in the controller, model, view or the template index.php.
Still not clear where these stuff should be placed!
You can place this code in the controller display() method.
You can put this code in any view view.html.php file in the display() method.
You can place this code in a template override. e.g. /templates/sometemplate/html/com_mycom/someview/default.php
Those are the places I put it in.
Hi Andy:
Can you please explain in layman’s terms, where would I locate the following.
You can place this code in the controller display() method.
You can place this code in the controller display() method. You state it can be placed in the template index.php is that correct?
Where is the controller display() method?
Is it so hard to write exact path to the file where i can put this code?
If you search online and read the basic of the joomla mvc file structure you can get a much better understanding. Basically every component should have a controller.php file right in the root of the component folder. e.g. /components/com_mycomponent/controller.php
http://docs.joomla.org/Developing_a_Model-View-Controller_Component/1.5/Introduction