How to customize pagetitle in typoscript

Sometimes it's usefull to customize the page title. With this snippet you can change the page title or add some special string.

config.noPageTitle = 2
page.headerData.5 = TEXT
page.headerData.5.field = subtitle // title
page.headerData.5.wrap = <title>Domain.com: &nbsp; |</title> 

Here is another sample that reads the page title wich is defined under "Template".

temp.pageTitle=COA
temp.pageTitle{
wrap=This is the title:|
5=RECORDS
5{
# id of the template record
source=1
tables = sys_template
conf.sys_template >
conf.sys_template = TEXT
conf.sys_template.field=sitetitle
}
}

This snippet was submited by Dominic Pfluger.

You can also learn quite a lot from studying the short code snippet that actually generates the page title. The function that generates the page title is printTitle located in class.t3lib_tstemplate.php. It is invoked from renderContentWithHeader located in class.tslib_pagegen.php.

// Title
$titleTagContent = $GLOBALS['TSFE']->tmpl->printTitle($GLOBALS['TSFE']->altPageTitle ? $GLOBALS['TSFE']->altPageTitle : $GLOBALS['TSFE']->page['title'], $GLOBALS['TSFE']->config['config']['noPageTitle'], $GLOBALS['TSFE']->config['config']['pageTitleFirst']);

if ($GLOBALS['TSFE']->config['config']['titleTagFunction']) {
   $titleTagContent = $GLOBALS['TSFE']->cObj->callUserFunction($GLOBALS['TSFE']->config['config']['titleTagFunction'], array (), $titleTagContent);
}

if (strlen($titleTagContent) && intval($GLOBALS['TSFE']->config['config']['noPageTitle']) !== 2) {
   $pageRenderer->setTitle($titleTagContent);
}

  • The $GLOBALS['TSFE']->altPageTitle is simply a placeholder in case some extension desire to modify the page title. If it is set it will be used instead of the actual page title.
  • The $GLOBALS['TSFE']->config['config']['noPageTitle'] is a value of config.noPageTitle which is decribed in TSREF as follows:

    If you only want to have the sitename (from the template record) in your <title> tag, set this to 1. If the value is 2 then the <title> tag is not printed at all.

    Please take note that this tag is required for XHTML compliant output, so you should only disable this tag if you generate it manually already.

  • The $GLOBALS['TSFE']->config['config']['pageTitleFirst'] is a value of config.pageTitleFirst which is also described in TSREF as follows:

    If set (and the page title is printed) then the page-title will be printed BEFORE the template title.
  • The config.titleTagFunction enables you to modify the default title tag with an external PHP function.
  • Finally if config.noPageTitle does not equal 2 the page title is set.

 

The function printTitle is an interesting study in itself:

function printTitle($title,$no_title=0,$titleFirst=0)    {
    $st = trim($this->setup['sitetitle']) ? $this->setup['sitetitle']:'';
    $title = $no_title ? '' : $title;
    if ($titleFirst)    {
        $temp=$st;
        $st=$title;
        $title=$temp;
    }
    if ($title && $st)    {
        return $st.': '.$title;
    } else {
        return $st.$title;
    }
}

Since all the parameters that this function receives are described above you should be able to easily discern what exactly happens here. These two code snippets are sufficient for you to have a full grasp of title tag generation in TYPO3. There are some extension that will override the default title tag such as tt_news, but that is a whole new issue in itself. Perhaps I will amend this article sometimes and explain how and why tt_news does it.

Your thoughts

No comments

Add comment

* - required field

*




*