6

I have built a 3.x component using Component-Creator.com and I want to be able to set the page title to the item heading (taken from the component itself). I have tried the following code but it's not working:

$browserbar= "$this->item->heading";
$document = JFactory::getDocument();
$document->setTitle($browserbar);

This just output's:

JObject->heading

as the Page Title in the browser

4 Answers 4

9

Ok, I've solved it. I removed the " " marks around $this->item->heading as follows:

$browserbar= $this->item->heading;
$document = JFactory::getDocument();
$document->setTitle($browserbar);

and now it's working!

7

An alternative to removing the quotes is to wrap the entire variable in curly brackets:

$browserbar= "{$this->item->heading}";

This causes PHP to process the entire piece as one variable instead of just processing $this->item (which is an object of the type JObject, hence the output) followed literally by ->heading.

In your case, the quotes are not needed, but this could be useful for also appending a literal string to the contents of the heading property:

$browserbar= "The heading is {$this->item->heading}.";
0
1

This is actually for Page Header output, but the same applies to the Browser Title. I've had this situation occur most frequently with 3rd party components. Joomla stores these values when you specify them in the menu item, but it's up to the component whether or not they're used. Below is the code I've added to my components, and sometimes to the template overrides. If you're not familiar with template overrides, you should read How to override the output from the Joomla! core first.

Put this code before any other HTML code in the template override file. It checks to see if you've specified to show the page headings or not, and if so, it will use that, otherwise it will use whatever the menu title is.

<?php if ($this->params->get('show_page_heading',1)) : ?>
    <h1><?php echo  $this->params->get('page_heading') ? $this->params->get('page_heading') : JFactory::getApplication()->getMenu()->getActive()->title; ?></h1>
<?php endif; ?>

To see how Joomla does it by default, look at components\com_content\views\article\tmpl\default.php

<div class="item-page<?php echo $this->pageclass_sfx?>">
    <?php if ($this->params->get('show_page_heading', 1)) : ?>
    <div class="page-header">
        <h1> <?php echo $this->escape($this->params->get('page_heading')); ?> </h1>
    </div>
    <?php endif;
-1

GDP's answer is correct; however, there has been a change in the parameter name. You'll want to use this instead:

<h1><?php echo $this->escape($this->params->get('page_title')); ?></h1>
4
  • The correct answer based on the OP code example was to remove the quotation marks from around the items title variable.
    – Terry Carter
    Commented Mar 16, 2016 at 21:39
  • Actualy I was incorrect and it should use page_heading. However, this simple code will not display the title IF the "Page Heading" field is empty on the menu item's "Page Display" tab. For some reason, it doesn't automatically display the menu item name is this is left blank. And Terry, I was trying to reply to GDP's response.
    – Paul S
    Commented Mar 16, 2016 at 21:55
  • @Paul when you have more rep/privileges, you will be able to comment under other users' answers. You must not post answers as a means to reply to other posts -- this makes your post Not An Answer. Please edit or remove the post. Commented May 8, 2020 at 23:53
  • @GDP it appears that this post was meant to reach you, but of course you would not be pinged. Ping Commented May 8, 2020 at 23:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.