Introduction
The classes that extend JDocument are intended for different response formats. The following table describes the classes and their purposes:
Class |
Format |
Default MIME type |
Purpose |
JDocumentError |
error |
text/html |
Display a fatal error |
JDocumentFeed |
feed |
application/rss+xml
or application/atom+xml |
Syndication feed, RSS, or Atom |
JDocumentHTML |
html |
text/html |
Default document used for all typical Joomla! responses |
JDocumentPDF |
|
application/pdf |
Adobe PDF representation |
JDocumentRAW |
raw* |
text/html |
All other circumstances |
So what exactly does the format column represent? When a request is made, Joomla! uses the value of the request variable format to determine which document type to use. We really see this only when we retrieve something other than an HTML document, because this always defaults to html. For example, when we request an article as a PDF, we use a URI similar to this:
http://example.org/index.php?option=com_content&view=article&id=5&
format=pdf
The JDocumentError class is slightly different from the others. We should never really need to interact directly with this. We can, of course, invoke this document indirectly by raising a fatal error. For more information, refer to Error handling and reporting.
The Joomla! document object and views in Joomla! MVC components are closely related. For each format that we provide a view, we create a new JView subclass. For example, when we examine the content component, we can see that the article view supports HTML and PDF simply by the presence of the view.html.php and view.pdf.php files.
This article also deals with the static JResponse class. This class is used to define the HTTP response, including the HTTP headers.
The separation between JResponse and the JDocument object is not always as clear as one would hope. However, this is somewhat inevitable because the two are inextricably linked—the response describes and includes the document output. For example, outputting an HTML response will require the response Content-Type header field to be set accordingly, that is, as text/html.
Setting the document title
This recipe explains how to set the title of the current document. The exact meaning of title will depend on the type of document. For example, in an HTML document, this is the value encapsulated in the <head> tag.
Getting ready
Before we do anything, we need the global document object.
$document =& JFactory::getDocument();
How to do it…
To set the title of the document, we use the JDocument::setTitle() method
$document->setTitle('My Unique Title');
If we are outputting an HTML document, this should generate something like this:
<title>My Unique Title</title>
There’s more…
Menu items can also define page titles. Thus, the actual title we use should not necessarily be the title of whatever we are viewing. To deal with this, we should use something along these lines:
// get the component and page parameters
$application =& JFactory::getApplication();
$params =& $application->getParams();
// get the page title
$pageTitle = $params->get('page_title', $defaultTitle);
// set the document title
$document->setTitle($pageTitle);
Setting the document generator
This recipe explains how to set the name of the piece of software that generated the page. The exact meaning of generator will depend on the type of document. For example, in an HTML document, this value is used in a tag.
Getting ready
Before we do anything, we need the global document object.
$document =& JFactory::getDocument();