I am using CakePHP 1.3
I have a static page called temp.ctp
located at app/views/pages/temp.ctp
.
It is accessible from my site like开发者_开发知识库: localhost/pages/temp
I am trying to set the page title from the view.
I have tried putting each of the following inside the temp.ctp file:
<?php $this->set("title_for_layout","Temp Temp Temp"); ?>
and
<? $this->pageTitle = 'Temp Temp Temp'; ?>
The entire file looks like this:
<?php $this->set("title_for_layout","Temp Temp Temp"); ?>
Hello World temp
And is using the default layout
(for header and footer) that ships with cakephp 1.3.
Neither of which have worked. What is the correct way to do this?
According to a thread on the CakePHP Google Group, it should be possible to use $this->set("title_for_layout","Temp");
directly in your view - in CakePHP 1.3 that is, 1.2 uses this->pageTitle = "Temp";
According to a ticket, the pages controller still uses the $title instead of $title_for_layout though, so try that:
$this->set("title","Temp");
http://cakephp.lighthouseapp.com/projects/42648/tickets/511-pages-controller-needs-updating-for-title_for_layout
The first thing to understand is, by default design, you should not be setting the page title from the view file. It's actually too late at that point. The Controller renders your final HTML in two parts: It puts the View inside of a Layout. The layout file is where the page title is set by default using $title_for_layout
.
Take a look at cake/libs/view/layouts/default.ctp. In the <head>
you'll see this, which explains what you've already described
<title>
<?php __('CakePHP: the rapid development php framework:'); ?>
<?php echo $title_for_layout; ?>
</title>
Because the Controller is where you are setting your viewVars, $this->set("title_for_layout","Temp Temp Temp");
only works in the Controller. It does nothing in a view or layout file (as you document in your question).
You may have a good reason for skipping the layout and use a single static view .ctp file. To do this, set the autoLayout variable to false in your Controller. You can set this for the entire Controller just like you set the name, uses, etc variables: var $autoLayout = false;
or on a method by method basis with $this->autoLayout = false;
.
Now Cake only renders your view .ctp file without the wrapper of a layout. All the viewVars are still available so you can use $title_for_layout if you want, otherwise hardcode a title. Either way you need to include your own <head>
/<title>
code at the top of the view .ctp file because it's not include with the layout anymore.
精彩评论