在代码点火器 (MVC) 中从内容文件设置页面标题


Setting page title from content file in CodeIgniter (MVC)

我的CodeIgniter网站的控制器是这样设置的:

$this->load->view('head', $data); // Contains <head> stuff
$this->load->view('header', $data); // Nav and sidebars
$this->load->view($this->uri->segment(1), $data); // Main page content
$this->load->view('footer', $data); // Footer

我的问题是在哪里存储<title>标签的内容。理想情况下,它将与页面内容(第 3 个加载的视图)一起使用,但是您如何将其放入已在第一个视图中加载的 <title> 标签中?

我读过让JavaScript设置标题是不好的,因为它与搜索引擎机器人不兼容。

我还读过一个建议,据说有一个包含所有页面的大视图文件

对于更改页面标题,这是更好的方法。在配置文件中添加此参数:

$config['pageTitle'] = 'Book Store';

在模板页面中:

<title><?=$this->config->config["pageTitle"]?></title>

在您喜欢的任何页面中,更改此配置的操作:

$this->config->config["pageTitle"] = $detail[0]['Title'];

每次您都可以轻松更改标题。

我不太明白为什么您希望在加载的第三个视图中而不是在已编译的HTML文档的头部(加载的第一个视图)中使用标签。为什么不这样的东西:

$data['title'] = 'my page title';
$this->load->view('head', $data);
我想

到了解决问题的方法,我从 http://joshhighland.com/blog/2008/11/09/how-i-do-layouts-and-views-in-codeigniter/那里得到了这个想法。如果您查看该页面,您可以在第一个代码提取的底部附近看到这样的一行:

$layout_data['content_body'] = $this->load->view('home/homePage', $body_data, true);

这会将视图作为字符串加载到该变量中。然后,我将对该字符串运行一个函数来提取我想要的标题(我将它存储在 HTML 注释中)并将其放入 $layout_data['pageTitle'] 中。然后,通过遵循链接上的其余布局方法,我将能够解决我的问题。

从逻辑上讲,您的<title>标签应该在<head>标签内,将其放在您的"头部"视图中是有意义的。因此,您要传递给头视图的$data数组应包含 tag 的元素。从您的脚本中我知道您正在为所有 4 个视图传递相同的$data数组?这不是很好的做法,因为您最终可能会传递一个视图中使用的元素,而不是其他四个视图中使用的元素。我建议为每个视图形成一个不同的数组。

编写一个帮助程序函数怎么样?

帮助程序helpers/header_helper.php

function set_title($title) {
    $ci &= get_instance();
    $ci->header_title = $title;
}
function get_title() {
    $ci &= get_instance();
    return $ci->header_title;
}

您可能希望将其设置为自动加载config/autoload.php

然后,您可以在

视图中调用函数get_title(),在控制器中可以调用set_title()

我已经以我的风格解决了这个问题。也许它不完美,但它解决了我最大的目的。

我创建了一个名为getMyPageTitle()的辅助函数。

在那里,我写下了页面标题的逻辑。

逻辑的基本部分是我在配置文件中假定默认标题。

            <?php
            function getMyPageTitle(){
                $title = '';
                $ci = &get_instance();
                $default_title = 'My Site';// you can load it from a config file,and that is better option
                // get the view name
                $view = $this->uri->segment(2);
                // check if controller set a page tile
                /* If you want to set a page title for a particular page then always 
                 * named that variable 'pageTitle' as $data['pageTitle'] = 'My page title | For this page'
                 */
                global $pageTitle;
                if($pageTitle){// the controller sets the page title
                    $title = $pageTitle;
                }elseif($view !== ''){//you can include your view name in the page title,,and the view name is not blanks
                    $title = $default_title.' | '. $view;
                }/**
                  * You can write several else if , before or after this or
                  * can change the total logic as you want
                  */
                 else{
                    $title = $default_title;
                }
                return $title;
            }
            ?>

并在视图中使用此

<title><?= getMyPageTitle()?></title>

我认为这解决了目的。

试试这个

-> 在模板中设置标题标签

    <title>
        <?php if (!empty($page_title)) echo $page_title;?>
    </title>

-> 设置 $data['page_title'] = ' 标题名称 '

public function index()
    {
        $data['page_title'] = 'List'; 
        $this->load->view('head', $data); // Contains <head> stuff
        $this->load->view('header', $data); // Nav and sidebars
        $this->load->view($this->uri->segment(1), $data); // Main page content
        $this->load->view('footer', $data); // Footer
    }

在代码点火器控制器中

public function index()
{
    $data['title'] = 'Website &mdash; Welcome To Website';
    $this->load->view('header', $data);
    $this->load->view('index');
    $this->load->view('footer');
}
public function about()
{
    $data['title'] = 'Coachify &mdash; About Us';
    $this->load->view('header', $data);
    $this->load->view('about');
    $this->load->view('footer');
}

view/header.php

<title><?= $title; ?></title>