Quick links: Content - sections - sub sections
EN FR

To fetch a jResponseHtml object, you should pass the string 'html' as argument to getResponse().


   $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();

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 for the generation of the content of the <body> element.

Let's have a look at all these things.

Generating (X)HTML headers

jResponseHtml provides features to change document's title, favicon, links to style sheets or js scripts, metas ...

Feature 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>
$rep->addCSSLink('maFeuille.css');

// generate a <style>
$rep->addStyle('span', 'font-weight:bold;');

// Add a description meta tag :
$resp->addMetaDescription( 'description' );

// Add a keywords meta tag :
$resp->addMetaKeywords( 'jelix php framework' );

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():


$rep->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.

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 template
    • false 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 on the page. So you will have a first template for the common things, and all pages will define another "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.

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 ;