Добавление метаданных в представление codeigniter с контроллера



В моем контроллере я добавил этот код.Для разных файлов просмотра существуют разные функции.Как я могу добавить мета-теги для разных функций для разных представлений?
Это мой контроллер:



<?php
class home extends CI_Controller {



function index()
{
$data['meta_title'] = 'Tracenow | iOS Version';
$data['meta_description'] = 'Responsive HTML5 Theme in iOS Style';
$data['meta_keywords'] = 'responsive html5 theme, ios, android, material design, landing, application, mobile, blog, portfolio, bootstrap 3, css, jquery, flat, modern';
$data['meta_author'] = '8Guild';
$data['meta_viewport'] = 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no';
$this->load->view('home_page/home_page');


}

function about()
{
$this->load->view('home_page/about');

}

function blog()
{
$this->load->view('home_page/blog');

}

function blog_single()
{
$this->load->view('home_page/blog-single');

}


}
?>
467   4  

4 ответов:

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; ?>" />

Надеюсь, это поможет вам. или если вам нужна какая-либо помощь, пожалуйста, прокомментируйте ниже.

Вы можете использовать эту библиотеку Мета-Теги

На вашем контроллере

$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 }?>
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

Comments

    Ничего не найдено.