将Meta数据添加到控制器的codeigniter视图中

时间:2016-06-06 08:53:00

标签: codeigniter meta-tags

在我的控制器中我添加了这个代码。对于不同的视图文件,有不同的功能。如何为不同的视图添加不同功能的元标记? 这是我的控制者:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Welcome to CodeIgniter</title>
</head>
<body>
<div id="container">
    <h1>Hello world!</h1>
</div>
</body>
</html>

5 个答案:

答案 0 :(得分:2)

For this you can simply use this in your controller:

$data['meta_title'] = 'Your meta title';
$data['meta_description'] = 'Your meta description';
$data['meta_keywords'] = 'Your meta keywords';

And you view should be like:

<title><?php echo $meta_title; ?></title>
<meta name="description" content="<?php echo $meta_description; ?>" />
<meta name="keywords" content="<?php echo $meta_keywords; ?>" />

希望它会对你有所帮助。或者如果您需要任何帮助,请在下面评论。

答案 1 :(得分:0)

您可以使用此库 Meta Tags

答案 2 :(得分:0)

在您的控制器上

$data['metas'] = array(
             array('name'=>'description', 'content'=>'A short but sweet DEFAULT description of this fine site'),
             array('name' =>'keywords', 'content'=>'some awesome DEFAULT keywords for those rascally web crawlers')
    );

在您的观看

<?php 
      foreach($metas as $meta)
      {?>
         <meta name="<?=$meta['name']?>" content="<?=$meta['content']?>" />
<?php }?>

答案 3 :(得分:0)

class Home extends CI_Controller{
    public function __construct(){
        parent::__construct();
        $this->load->helper('html'); //Load global helper 'html' 
    }
[...]
//in controller: meta tag
$meta = array(
        array(
                'name' => 'robots',
                'content' => 'no-cache'
        ),
        array(
                'name' => 'description',
                'content' => 'My Great Site'
        ),
        array(
                'name' => 'keywords',
                'content' => 'love, passion, intrigue, deception'
        ),
        array(
                'name' => 'robots',
                'content' => 'no-cache'
        ),
        array(
                'name' => 'Content-type',
                'content' => 'text/html; charset=utf-8', 'type' => 'equiv'
        )
);

//HTML
<!DOCTYPE html>
<head>
    <title></title>
    <?= meta($meta) ?> //use helper HTML to show meta tag
</head>
// Generates:
// <meta name="robots" content="no-cache" />
// <meta name="description" content="My Great Site" />
// <meta name="keywords" content="love, passion, intrigue, deception" />
// <meta name="robots" content="no-cache" />
// <meta http-equiv="Content-type" content="text/html; charset=utf-8" />

Font:https://codeigniter.com/userguide3/helpers/html_helper.html?highlight=helper

答案 4 :(得分:0)

在您的控制器中:

$this->output->set_common_meta($page_title, $page_description, $page_keywords);  // for title, description, keywords
$this->output->set_meta('property_name', 'property_value');  // for other properties