elxisDocument

by Elxis Team

Class elxisDocument provides useful tools on managing page document.

The elxisDocument library is a great tool for template and extension developers as it provides easy to use but powerful methods to master the document. Add CSS and javascript into document header, set page title and META data, load modules, display the pathway, load jQuery, load lightbox, set document type and content type are only a few of the things elxisDocument is capable to do.

Get elxisDocument

To get the instance of the elxisDocument class we use eFactory getDocument method.

$eDoc = eFactory::getDocument();

Table of contents


Add CSS, javascript, links, etc, into document's head section

If you want to append css or javascript files on page's head section, or just raw javascript and css, or custom stuff, elxisDocument provides some easy to use methods. Using the built-in methods is highly recommended.

addStyleLink

public function addStyleLink($url, $type='text/css', $media='', $attribs='')

Add a link to a CSS file into the document's head section. The $url property is the full URL to the CSS file. $type is usually text/css, $media can be empty (not defined) or any of the media types HTML supports (all, screen, print, handheld, etc...). If you want to provide extra HTML attributes you can do so by providing a string value for $attribs (example: rel="alternate" charset="utf-8").

// Example:
$eDoc = eFactory::getDocument();
$link = eFactory::getElxis()->secureBase().'/something/test.css';
$eDoc->addStyleLink($link)

After that the test.css will be in the document's head section:

<head>
	<link rel="stylesheet" href="http://www.example.com/something/test.css" type="text/css" />
</head>

addStyle

public function addStyle($content, $type='text/css')

Add raw CSS into the document's head section. $content is a string containg the CSS declarations.

// Example:
$eDoc = eFactory::getDocument();
$css = 'div.test { margin:5px; }';
$eDoc->addStyle($css)

And the final HTML on the document's head section:

<head>
	<style type="text/css">div.test { margin:5px; }</style>
</head>

addScriptLink

public function addScriptLink($url, $type='text/javascript')

Add a link to a javascript file into the document's head section. The $url property is the full URL to the js file.


addScript

public function addScript($content, $type='text/javascript')

Add raw javascript into the document's head section. $content is a string containing the javascript code.


addLibrary

public function addLibrary($nane, $url, $version='1.0')

Method addScriptLink can add any javascript file but especially for javascript libraries like mootools, highslide, colorbox, etc..., it is strongly recommended to use the addLibrary method as it provides some important features. The method will make sure that the link will be added only once. If different extensions load different versions of the same library Elxis will finally load only the latest one. $name is an identification name for the library (eg. mootools) and $version the library's version.

// Example (load 3 different versions of the same library):
$elxis = eFactory::getElxis();
$eDoc = eFactory::getDocument();
$link = $elxis->secureBase().'/something/mylib15.js';
$eDoc->addLibrary('mylibrary', $link, '1.5')
$link = $elxis->secureBase().'/something/mylib12.js';
$eDoc->addLibrary('mylibrary', $link, '1.2')
$link = $elxis->secureBase().'/something/mylib14.js';
$eDoc->addLibrary('mylibrary', $link, '1.4')

Elxis will finally append into the document's header the latest version (1.5) of the javascript library:

<head>
	<script type="text/javascript" src="http://www.example.com/something/mylib15.js"></script>
</head>

addJQuery

public function addJQuery()

Especially for the jQuery library, which is built-in in Elxis, use the addJQuery method to insert jQuery into page's head.


addDocReady

public function addDocReady($content)

Add javascript code to be executed on document ready (uses jQuery)


addCustom

public function addCustom($content)

Add custom head data. The contents of $content can be any string properly formatted for the document's head.


addLink

public function addLink($href, $type='application/rss+xml', $relation='alternate', $attribs='')

Add a custom <link> tag into head. Most oftenly it is used for RSS / ATOM feeds.


Document type, content type and namespace

setDocType

public function setDocType($type)

Sets the document's DOCTYPE.


getDocType

public function getDocType()

Returns the current document type (default value <!DOCTYPE html>).


setContentType

public function setContentType($type)

Sets the document's content type.


getContentType

public function getContentType()

Returns the current content type (default value text/html).


setNamespace

public function setNamespace($ns)

Sets the document's XML name space (on XHTML doctypes).


getNamespace

public function getNamespace()

Returns the current XML namespace (the default value is empty - no XML namespace on HTML5 documents).


Page title, META data and favicon

Control page title, META description, META keywords and other META data and also the favicon.


setTitle

public function setTitle($title)

Sets the page title.

// Example:
$eDoc = eFactory::getDocument();
$eDoc->setTitle('My custom title');

getTitle

public function getTitle()

Returns the current page title.


setDescription

public function setDescription($description)

Sets the page META description.


getDescription

public function getDescription()

Returns the current META description.


setMetaTag

public function setMetaTag($name, $content, $equiv=false)

Sets any META tag (generator, description, author, etc).


setKeywords

public function setDescription($keywords)

Sets the page META keywords. $keywords should by an array of keywords or a comma separated string of keywords.

// Example:
$eDoc = eFactory::getDocument();
$mykeywords = array('elxis document', 'cms', 'keyword');
$eDoc->setKeywords($mykeywords);

setFavicon

public function setFavicon($url)

Set the document's favicon. $url is the full URL to favicon image (preferable of type ico or png).


getFavicon

public function getFavicon()

Returns the current favicon.


Lightbox and syntax highlighter

loadHighlighter

public function loadHighlighter($brushes=array(), $theme='default')

Load built-in syntax highlighter.


highlight

public function highlight($code, $brush='xml', $title='', $gutter=true, $firstLine=1, $collapse=false, $autoLinks=true)

Highlight source code. To use this method you must have first load the syntax highlighter with the loadHighlighter method.

  • (string) $code The source code to highlight
  • (string) $brush The brush to use. Leave it to the default xml to load support for the most common programming languages (html, php, css, js, perl, etc).
  • (string) $title An optional title to display above the highlighted code.
  • (boolean) $gutter Display a gutter or no (defaults to true)
  • (integer) $firstLine The line number for the first row (defaults to 1)
  • (boolean) $collapse Display source code collapsed or not (defaults to false)
  • (boolean) $autoLinks Automatically convert URLs to links (defaults to true)

loadLightbox

public function loadLightbox()

Loads the built-in lightbox javascript library (currently colorbox). Also loads jQuery as colorbox uses jQuery.

In case you use if you use colorbox with iframe on XHTML doctypes you must switch content type to text/html as application/xhtml+xml does not support iframes in XHTML doctypes.

// Example (open a link in a lightbox window):
$eDoc = eFactory::getDocument();
$eDoc->loadLightbox();
$eDoc->setContentType('text/html');
$eDoc->addDocReady('$(\'#sample\').colorbox({iframe:true, width:800, height:500});');
echo '<a href="http://www.elxis.org" id="sample">Sample link</a>';

Modules, component and pathway

The elxisDocument library provides some methods very usefull to template developers in order to import into the templates modules, component and pathway in the exact spot they want. Also the htmlAttributes method is strongly recommended to be used in all templates.

htmlAttributes

public function htmlAttributes()

Returns a string of HTML attributes to be appended into the document's <html> tag. The contents of this string is generated automatically by Elxis. All templates should use this.

// Example:
<html<?php echo $eDoc->htmlAttributes(); ?>>

countModules

public function countModules($position)

Returns the number of modules assigned to the given modules position (eg. left). This method is a replacement of mosCountModules function from the Elxis 2009.x series.

// Example (check if modules exist in position right):
$eDoc = eFactory::getDocument();
if ($eDoc->countModules('right') > 0) {
//do something
}

modules

public function modules($position, $style='')

Display the modules assigned to the given position. $style can be empty (default - puts a DIV with class module around the module), none (without surrounding DIV) and round (puts module into 3 DIVs - old method of rounded corners, left mostly for compatibility reasons). This method is a replacement of mosLoadModules function from the Elxis 2009.x series.

// Example (load modules assigned in positions user1 and user2):
$eDoc = eFactory::getDocument();
$eDoc->modules('user1');
$eDoc->modules('user2');

module

public function module($modname, $style='')

Display the output of a specific module. $style can be empty (default - puts a DIV with class module around the module), none (without surrounding DIV) and round (puts module into 3 DIVs - old method of rounded corners, left mostly for compatibility reasons). This method is a replacement of elxLoadModule function from the Elxis 2009.x series.

// Example (load module language):
$eDoc = eFactory::getDocument();
$eDoc->module('mod_language');

component

public function component()

Sets the spot where component output will be displayed.


pathway

public function pathway($pathway_here=false)

Displays the Elxis pathway. If $pathway_here is set to true the sentence "You are here" will be displayed before the pathway.


It has been read 11014 times
Elxis cache
Previous article
Elxis cache
elxisUri
Next article
elxisUri

You are free to copy, distribute and transmit the articles in this site for non-commercial purposes.
Creative Commons 3.0