Section: Generating HTML content
^ Responses: generating content | Generating plain text » |
− Table of content
To create a jResponseHtml object, pass 'html' as argument to getResponse()
method in your controller's action.
$resp = $this->getResponse('html')
Then in $resp, you have an instance of jReponseHtml
.
XTHML or HTML ¶
jResponseHtml is set to return XHTML output by default. You can enable, disable or fetch this setting :
$rep->setXhtmlOutput( true ); // XHTML output
$rep->setXhtmlOutput( false ); // HTML output
// Fetch the setting value :
$outputXhtml = $this->isXhtml();
Going further, you can specify your own DOCTYPE by overloading outputDocType()
. This method should echo a valid DOCTYPE.
Generating contents ¶
jResponseHtml implements methods and properties to work on (X)HTML contents.
The source of an HTML page is split into two parts: the <head>
part, and the <body>
part.
jResponseHTML generates itself the content of the <head>
element, from data you give to the jResponseHTML via its dedicated methods.
However, you are responsible of the generation of the content of the <body>
element.
Let's see all these things.
Generating (X)HTML headers ¶
jResponseHtml provides features to change document's title, favicon, links to style sheets or JS scripts, meta tags...
Features tour:
$resp->title = 'Document Title';
// generate a script tag
$resp->addJSLink( 'lib.js' );
// generate a script tag with inline code :
$resp->addJSCode( 'alert( "Hello world" ) )' );
// generate a <link>
$resp->addCSSLink('style.css');
// generate a <style>
$resp->addStyle('span', 'font-weight:bold;');
// Add a description meta tag:
$resp->addMetaDescription( 'description' );
// Add a keywords meta tag:
$resp->addMetaKeywords( 'jelix php framework' );
// Add the author:
$resp->addMetaAuthor('Laurent');
// change the meta generator name
$resp->addMetaGenerator('My Super App 1.0');
Note that addCSSLink()
and addJSLink()
can use an associative array argument specifying custom (X)HTML tag properties, for example:
$resp->addCSSLink(
'stylesheet.css',
array(
'title' => 'blue design',
'rel' => 'alternate stylesheet',
)
);
If you want to add custom content in the <head>
element, use addHeadContent()
:
$resp->addHeadContent('<link rel="alternate" type="application/rss+xml" title="Recent Changes" href="/feed.php" />')
It happens to require removal of parts of the generated headers (if you refuse to take the hand on third-party-module).
You should use method clearHtmlHeader()
for this :
// remove links to stylesheets done with addCSSLink(), and style tags from addStyle()
$resp->clearHtmlHeader( array( 'CSSLink', 'Styles' ) );
Acceptable array values are strings :
- CSSLink,
- Styles,
- JSLink,
- JSCode,
- Other.
Minifying css and javascript files ¶
The object jResponseHtml is able to use Minify to concatenate and minify CSS and JS files. It improves performance during the load of the page.
Here are configuration options to minify/concatenate css or javascript files in jResponseHtml
.
[jResponseHtml]
;concatenate and minify CSS and/or JS files :
minifyCSS = off # [on|off] : concatenate/minify CSS files
minifyJS = off # [on|off] : concatenate/minify JS files
; check all filemtime() of original js files to check if minify's cache should be generated again.
; Should be set to "off" on production servers (i.e. manual empty cache needed when a file is changed) :
minifyCheckCacheFiletime = on # [on|off] : check whether at least one of the source files are newer than the cached files.
; list of filenames (no path) which shouldn't be minified - coma separated :
minifyExcludeCSS = "file1.css,file2.css"
minifyExcludeJS = "jquery.wymeditor.js"
; add a unique ID to CSS and/or JS files URLs ( this gives for exemple /file.js?1267704635 ). This ID is actually the filemtime of each served file.
; useful to serve far future expires header thus enhancing page load time and server bandwidth.
jsUniqueUrlId = off
cssUniqueUrlId = off
First, with minifyCSS
and minifyJS
you activate the "minification". You can indicate files to NOT minify in minifyExcludeCSS
and minifyExcludeJS
.
Keep the file name "jquery.wymeditor.js" in minifyExcludeJS
. Wymeditor doesn't like to be concatenated with other files.
Be sure to have a www/cache/minify
folder and to have enough rights on it for the webserver.
Generate (X)HTML body ¶
jResponseHtml generates the (X)HTML <body>
tag, but you can control its contents and attributes.
To control its attributes :
$resp->bodyTagAttributes = array( 'onload'=>'bodyonload( )',
'class'=>'maincontent');
Control its contents with either :
- a template
- method
addContent( )
Using a template ¶
jResponseHtml provides a couple of properties :
$bodyTpl
, which value should be the template selector;body
, which contains a jTpl instance allowing control over template "settings".
For example :
$resp->bodyTpl = 'myapp~main';
$resp->body->assign( 'person','Guybrush Treepwood');
Contents generated by the template engine will be integrated in the (X)HTML body tags automatically.
To know more about templates, read chapter about templates.
It happens to require appending some arbitrary contents in addition to the (X)HTML body code generated with
method addContent()
:
- first argument is the string value to append to the (X)HTML body contents,
- second argument is a bool about the order :
true
if the first argument's string value should be placed before the templatefalse
if it should be placed after the template.
Note that it's set to false by default.
Example :
$resp->addContent( 'This text will be placed after the template' );
$resp->addContent( 'This text will be placed before the template', true);
It's possible to use this method to append contents from "zones", for example :
$resp->addContent( jZone::get( 'aModule~aZone'));
Using a main template and some "sub-templates" ¶
We often have a common template for all pages, and only few things change inside this common template, depending of the page. So you will have a first template for the common things, and all pages will define an other "sub-template" to generate specific things for the page. The result of this sub-template will be inserted into the main template.
You can do this work directly into all your controllers, with zones.
But there is a more convenient way to do it : you can define an object which inherits from jResponseHtml. In this object, you will define all common things. And then you declare this object as the default HTML view for all your actions.
Read the section: how to create a custom common response.
Not using templates ¶
Let bodyTpl
empty if you don't want to use a template for the (X)HTML body of the response, and use addContents()
as described in the previous paragraph of the documentation.
$rep->addContent('contenu pour mon body');
Other response parameters ¶
It's possible to control HTTP headers since jResponseHtml inherits from jResponse, for the status code as well as for miscellaneous properties.
$resp->setHttpStatus( '404', 'Not Found' );
$resp->addHttpHeader( 'Date-modified', '...' );
If jResponseHtml's property $xhtmlContentType
is true : HTTP response's "Content-Type" will be application/xhtml+xml
.
Note that the client-browser must support xHTML, or Content-Type text/html
is sent.
$resp->xhtmlContentType = true ;