Search code examples
codeignitercodeigniter-3meta-tagscodeigniter-helpers

Dynamic Meta tag through helper or library for each function or for each page in codeigniter 3


Hi i want generate dynamic Meta tag through helper or library in codeigniter 3

I am creating static website without model.

I have - Header.php - which have header part and meta tags.

middle_page.php

footer.php

if i am calling meta() html helper in my controller function like index then generated meta tag is showing in top of the page means above to deceleration of header.php

how to write helper or library for that so if i call meta tag in function and in header.php echo meta(); then particular meta tag should be generated.

I have tried many other answers from Stackoverflow but those are not worked for me.

your help will be appreciated.


Solution

  • Follow this pattern Maybe it will help you

    IN controller

    <?php
    function show_home() {
        $data['page'] = 'home';
        $this->load->view('header',$data);
        $this->load->view('home');
        $this->load->view('footer');
    }
    function show_contact_us() {
        $data['page'] = 'contact_us';
        $this->load->view('header',$data);
        $this->load->view('contact_us');
        $this->load->view('footer');
    }
     ?>
    

    Create helper meta.php

    <?php
        function meta($page) {
            $meta = '<meta charset="utf-8">';
            switch ($page) {
                case 'home':
                    $meta .= '<meta name="description" content="Home page description">';
                    $meta .= '<meta name="keywords" content="Home page keywords">';
                    break;
                case 'contact_us':
                    $meta .= '<meta name="description" content="Contact us page description">';
                    $meta .= '<meta name="keywords" content="Contact us page keywords">';
                    break;
                default:
                    $meta .= '<meta name="description" content="default page description">';
                    $meta .= '<meta name="keywords" content="default page keywords">';
                    break;
            }
            return $meta;
        }
    ?>
    

    load meta helper in autoload.php

    $autoload['helper'] = array("meta");
    

    call meta function in your header for individual view

    echo meta($page);