Andy
Andy

Reputation: 5395

CakePHP 2.9.5 $this->assign('title') does not work as described in documentation

In the documentation for CakePHP 2.9.5 https://book.cakephp.org/2.0/en/views.html it says:

The $title_for_layout is deprecated as of 2.5, use $this->fetch('title') in your layout and $this->assign('title', 'page title') instead.

In app/View/Layouts/default.ctp I have this, which incidentally comes with CakePHP and has not been modified:

<title>
    <?php echo $cakeDescription ?>:
    <?php echo $this->fetch('title'); ?>
</title>

And in my view file:

<?php $this->assign('title', 'My page title'); ?>

So this should output "My page title" in the <title> tag but just comes up with the default CakePHP title of "CakePHP: the rapid development php framework"

Where am I going wrong?

Upvotes: 0

Views: 734

Answers (3)

Andy
Andy

Reputation: 5395

The answer to this is that the original code I had works. But there is a change required to the default layout file...

In Layouts/default.ctp you have this on a clean installation:

<title>
    <?php echo $cakeDescription ?>:
    <?php echo $this->fetch('title'); ?>
</title>

The string for $cakeDescription is echo'd before the title set in your View. The default string is quite long and so if you're looking at it in a browser tab (as I was) it appears that the title never changes! Unless you view source.

In any case you need to remove this variable because otherwise all your titles will have Cake's default text prepended. So the solution is simply to change the above to:

<title>
    <?php echo $this->fetch('title'); ?>
</title>

In your Views, the code works as documented:

$this->assign('title', 'Page title here'); 

This is a poor default on the Cake developers part as there's no reason you'd ever want their default text in the title tag in a real application.

Upvotes: 2

Noneleft
Noneleft

Reputation: 1

If that doesn't work for you - and 2.9.5 was a fun release try this:

in your View/Layouts/default.ctp

parse the url and pull the path, set this after your title or as your title as your preference

snippet below:

<?php

$url = $_SERVER['REQUEST_URI'];
$url_array = parse_url($url);
preg_match('@/(?<path>[^/]+)@', $url_array['path'], $m);
$url_folder = $m['path'];

$cakeDescription = 'Prospector : '.$url_folder;
?>
<!DOCTYPE html>

<html>
<head>
    <?php echo $this->Html->charset(); ?>
    <title>
        <?php echo $cakeDescription ?>:
    </title>

The $url_folder contains the path I assume you are after, so if you view is example.com/companies/index for example the $url_folder contains 'companies'

so using the above code, the Title would be set at Prospector : companies in this instance.

You can of course capitalize as needed etc, dropping Prospector for your given preference.

full page below - note this is from a clean 2.9.5 install:

<?php

$url = $_SERVER['REQUEST_URI'];
$url_array = parse_url($url);
preg_match('@/(?<path>[^/]+)@', $url_array['path'], $m);
$url_folder = $m['path'];

$cakeDescription = 'Prospector : '.$url_folder;
?>
<!DOCTYPE html>
<html>
<head>
    <?php echo $this->Html->charset(); ?>
    <title>
        <?php echo $cakeDescription ?>:
    </title>
    <?php
        echo $this->Html->meta('icon');

        echo $this->Html->css('cake.generic');

        echo $this->fetch('meta');
        echo $this->fetch('css');
        echo $this->fetch('script');
    ?>
</head>
<body>
    <div id="container">
        <div id="header">
            <h1><?php echo $this->Html->link($cakeDescription, 'http://cakephp.org'); ?></h1>
        </div>
        <div id="content">

            <?php echo $this->Flash->render(); ?>

            <?php echo $this->fetch('content'); ?>
        </div>
        <div id="footer">
            <?php echo $this->Html->link(
                    $this->Html->image('cake.power.gif', array('alt' => $cakeDescription, 'border' => '0')),
                    'http://www.cakephp.org/',
                    array('target' => '_blank', 'escape' => false, 'id' => 'cake-powered')
                );
            ?>
            <p>

            </p>
        </div>
    </div>
    <?php echo $this->element('sql_dump'); ?>
</body>
</html>

Of course this would only work for you if you want the title to be the same as the model.

If you don't want the model name, but can use the model name to test against, then you could use $url_folder to test against in a switch.

Upvotes: 0

Noneleft
Noneleft

Reputation: 1

in your controller

$title = 'Title for your page';
$this->set(compact('title'));

then in your view just

$this->assign('title',$title); 

Upvotes: -1

Related Questions