Actions

Difference between revisions of "JDocumentHTML/addHeadLink"

From Joomla! Documentation

m (Added cross-reference to JDocument/addStyleSheet.)
(Experimental use of DPL for code samples.)
Line 30: Line 30:
 
|
 
|
 
|}
 
|}
===Example 1===
+
===Examples===
To add a link for an RSS feed to the document, you could use:
+
<dpl>
<source lang="php">
+
category=JDocument
$doc =& JFactory::getDocument();
+
category=addHeadLink
$href = '/joomla/index.php?format=feed&amp;type=rss';
+
category=Code examples
$attribs = array('type' => 'application/rss+xml', 'title' => 'RSS 2.0');
+
include=*
$doc->addHeadLink( $href, 'alternate', 'rel', $attribs );
+
format=,\n====%TITLE%====\n,,
</source>
+
</dpl>
When the document is rendered this will produce the following in the HTML <head> section:
+
<source lang="html4strict">
+
<link href="/joomla/index.php?format=feed&amp;type=rss" rel="alternate" type="application/rss+xml" title="RSS 2.0" />
+
</source>
+
===Example 2===
+
To add a link to a CSS file to the document, you could use:
+
<source lang="php">
+
$doc =& JFactory::getDocument();
+
$href = 'index.php/other/css/mycss.css';
+
$attribs = array('type' => 'text/css');
+
$doc->addHeadLink( $href, 'stylesheet', 'rel', $attribs );
+
</source>
+
When the document is rendered this will produce the following in the HTML <head> section:
+
<source lang="html4strict">
+
<link href="/index.php/other/css/mycss.css" rel="stylesheet" type="text/css" />
+
</source>
+
This is an alternative to using [[JDocument/addStyleSheet|JDocument->addStyleSheet]].
+
 
===See also===
 
===See also===
 
* [http://api.joomla.org/Joomla-Framework/Document/JDocumentHTML.html#addHeadLink JDocumentHTML->addHeadLink on api.joomla.org]
 
* [http://api.joomla.org/Joomla-Framework/Document/JDocumentHTML.html#addHeadLink JDocumentHTML->addHeadLink on api.joomla.org]
 
<noinclude>[[Category:Development]][[Category:Framework]][[Category:JDocumentHTML]]</noinclude>
 
<noinclude>[[Category:Development]][[Category:Framework]][[Category:JDocumentHTML]]</noinclude>

Revision as of 20:01, 14 November 2009

Adds a link tag to the document head.

Syntax

void addHeadLink( $href, $relation, $relType, $attribs )

where:

Argument Data type Description Default
$href strng URL of the linked resource.
$relation string Relation
$relType string Relation type. May be 'rel' indicating a forward relation, or 'rev' for a reverse relation. 'rel'
$attribs array Associative array of remaining attributes.

Examples

Adding a link for an RSS feed to the document

To add a link for an RSS feed to the document, you could use:

$doc =& JFactory::getDocument();
$href = '/joomla/index.php?format=feed&amp;type=rss'; 
$attribs = array('type' => 'application/rss+xml', 'title' => 'RSS 2.0'); 
$doc->addHeadLink( $href, 'alternate', 'rel', $attribs );

When the document is rendered this will produce the following in the HTML <head> section:

<link href="/joomla/index.php?format=feed&amp;type=rss" rel="alternate" type="application/rss+xml" title="RSS 2.0" />

Adding a link to a CSS file to the document

To add a link to a CSS file to the document, you could use:

$doc =& JFactory::getDocument();
$href = 'index.php/other/css/mycss.css'; 
$attribs = array('type' => 'text/css'); 
$doc->addHeadLink( $href, 'stylesheet', 'rel', $attribs );

When the document is rendered this will produce the following in the HTML <head> section:

<link href="/index.php/other/css/mycss.css" rel="stylesheet" type="text/css" />

This is an alternative to using JDocument->addStyleSheet.

See also