Escolar Documentos
Profissional Documentos
Cultura Documentos
API’s
Introduction
This document will explain how to add Javascript code to your template. It will
also explain how this can be expanded to components, modules and anywhere you like
too but, also the Joomla! API guarantee us to add it in a consistent form and on the
correct place so, it doesn’t break compatibility with the XHTML standard.
PHP basics:
If you already know PHP you can skip this part.
• Variables start with dollar sign($)
• Functions are code blocks that you can call to do stuff. This line below calls the
getDocument function that is inside the JFactory class and stores a reference of it
on the $document variable.
$document =& JFactory::getDocument();
• Every statement ends with a semicolon (;)
• The equal ampersand (=&) in the middle of the variable and function says
reference that means give me the original not a copy of it.
• The arrow(->) after variables mean the variable holds and object inside it and
calls a function from that object.
• Comments are ignored by code and start with double slash (//)
• Multi line comments are encapsulated inside /* */
• Argument are values passed to function they will use the arguments to perform
their jobs.
• There’s a variable called $this it means the class you are in. Example, if you
are putting this in the template $this will give us the document.
• Code has to go inside the php tags <?php ?> for it to be executed, if not it
will display on the page whatever code youtyped as normal text.
• You can get the base folder where Joomla! is installed using JURI::base()
• The value of the $this->template variable inside a template is the template folder
name. This can be useful for adding the template name to a directory.
<?php
// no direct access
?>
If you where calling this from a component or module you will need to get the document
reference using this line of code. This line of code will store the document reference into
the document variable.
<?php
defined('_JEXEC') or die();
jimport( 'joomla.application.component.view');
{
global $mainframe, $option;
$document =& Jfactory::getDocument();
$js = "/templates/".$this->template."/hello.js";
$document->addScript(JURI::base() . $js);
<?php
// no direct access
?>
If you will like to add this Javascript from a module or a component you will do it by
getting a reference of the document using the getDocument Function:
And now you call the function using the document variable
$document->addScriptDeclaration("alert('Hello Joomla!')");
<?php
defined('_JEXEC') or die();
jimport( 'joomla.application.component.view');