Zend:如何在我的所有视图中添加网页标题?

发布于 2024-11-07 05:30:40 字数 404 浏览 4 评论 0原文

现在,我必须在所有视图中分别添加标题,如下所示:

<head>
       <title>TestProject - Home</title>
</head>

现在

<head>
       <title>TestProject - Dashboard</title>
</head>

,如果我想更改标题的 TestProject 部分,那么我必须在所有视图中更改它。我如何在 BootStrap.php 中提及这一点并将其添加到所有视图中?每当我必须改变这一点时,我都会在一个地方改变它。

For now I have to add title in all my views separately like this:

<head>
       <title>TestProject - Home</title>
</head>

and

<head>
       <title>TestProject - Dashboard</title>
</head>

Now if I want to change TestProject part of title then I have to change it in all my views. How can I mentioned this in BootStrap.php and add it in all views? And whenever I have to change this, I will change this in one place.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ˇ宁静的妩媚 2024-11-14 05:30:40

您应该查看 headTitle 视图助手。您可以将此代码片段放在引导文件下面(来自 http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.headtitle)。

// setting the controller and action name as title segments:
$request = Zend_Controller_Front::getInstance()->getRequest();
$this->headTitle($request->getActionName())
     ->headTitle($request->getControllerName());

// setting the site in the title; possibly in the layout script:
$this->headTitle('Test Project');

// setting a separator string for segments:
$this->headTitle()->setSeparator(' / ');

然后,您可以在控制器中单独设置每个页面标题,如下所示:

$this->view->headTitle('The page name')

渲染的标题将如下所示:

<title>Test Project / The page name</title>

哦,您需要在标签所在的布局脚本中使用此内容:

<?php echo $this->headTitle() ?>

You should look into the headTitle view helper. You can put this snippet below in your bootstrap file (from the documentation at http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.headtitle).

// setting the controller and action name as title segments:
$request = Zend_Controller_Front::getInstance()->getRequest();
$this->headTitle($request->getActionName())
     ->headTitle($request->getControllerName());

// setting the site in the title; possibly in the layout script:
$this->headTitle('Test Project');

// setting a separator string for segments:
$this->headTitle()->setSeparator(' / ');

Then you can set each page title individually in controller like this:

$this->view->headTitle('The page name')

The rendered title will look like this:

<title>Test Project / The page name</title>

Oh, and you need this in your layout script where the tag would go:

<?php echo $this->headTitle() ?>
最佳男配角 2024-11-14 05:30:40

研究使用 布局占位符。布局适用于您的所有视图,您可以在那里设置标题。然后,您可以在控制器中将“主页”或“仪表板”部分设置为布局将使用的占位符。

Look into using layouts and placeholders. Layouts are applied to all your views, you can set the title there. In your controller you can then set the "home" or "dashboard" part to a placeholder that will be used by your layout.

牵你的手,一向走下去 2024-11-14 05:30:40

Bootstrap.php

protected function _initViewHelpers() {
    $view = new Zend_View();
    $view->headTitle('Main Title')->setSeparator(' - ');
}

在任何 view/.phtml

<?php 
    $this->headTitle()->prepend('Page Title');
    echo $this->headTitle();
?>

In Bootstrap.php

protected function _initViewHelpers() {
    $view = new Zend_View();
    $view->headTitle('Main Title')->setSeparator(' - ');
}

In any view/.phtml

<?php 
    $this->headTitle()->prepend('Page Title');
    echo $this->headTitle();
?>
睫毛上残留的泪 2024-11-14 05:30:40

这是将页面标题设置到控制器中的最佳方法。如果您想设置新标题,则使用..

public function init()
 {
    $this->view->headTitle('My title'); 
 }

如果您想在标题前添加现有标题,则使用

$this->view->headTitle()->setSeparator(' - ')->prepend('Manager'); 

This is best way to set page title into the controller. If you want to set new title then use..

public function init()
 {
    $this->view->headTitle('My title'); 
 }

If you want to prepend the title with existing title then use

$this->view->headTitle()->setSeparator(' - ')->prepend('Manager'); 
烟雨扶苏 2024-11-14 05:30:40

我认为你可以使用js来做到这一点,因为你只需将标题=TestProject设置为站点,然后在每个页面中你可以使用javascript读取标题,然后连接额外的标题

,如果你使用的是母版页之类的东西

I think you may do this with js as you just site the title =TestProject only then in each page you could with javascript read the title then concatenate the extra title

this if you are using something like master pages

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文