How to Deal With Codeigniter Templates?

前端 未结 12 1821
梦谈多话
梦谈多话 2020-12-08 01:02

I\'m fairly new to MVC, and I\'ve found CodeIgniter recently. I\'m still learning everyday, but one problem is its template engine. What is the best way to create templates

相关标签:
12条回答
  • 2020-12-08 01:36

    Well you can actually use a codeigniter library for templates. The most famous ones are:

    1. Codeigniter Simplicity (Actively Developed)
    2. Phil Sturgeon's Template Library (Not actively developed)
    3. An Introduction to Views & Templating in CodeIgniter (Here you actually create a template library from scratch)
    0 讨论(0)
  • 2020-12-08 01:36

    Allow me propose an easier way to do this. Consider my answer to a similar question.

    Pros:

    1. Your template file can be a full HTML file. You don't have to break up the header and the footer.
    2. Any view file can be turned into a template with minimal effort.
    3. Data for the specific view can be generated in the template.

    Cons: 1. You may have to add a template (or layout—if you want to do it the Rails way) directory under views in order to structure your code properly. This follows from Pros[2]. 2. Data for the specific view from the controller must first of all be passed to the template.

    0 讨论(0)
  • 2020-12-08 01:37

    I've tried several ways to do codeigniter templates and the way that I stay is the fastest and simplest, is as follows.

    In controller:

        //Charge the view inside array
        $data['body'] = $this->load->view('pages/contact', '', true);
    
    
        //charge the view "contact" in the other view template
        $this->load->view('template', $data);
    

    In view template.php:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es"> 
    <head> 
        <title>Template codeigniter</title> 
    </head> 
    <body> 
        <div> 
            <?=$body?>
        </div> 
        <div class="clear"></div> 
        <div>Footer</div> 
        </div> 
    </body> 
    </html> 
    

    $body is the view contact.

    0 讨论(0)
  • 2020-12-08 01:39

    Make a library that includes all your views and send it data that you need to send to your content view. This is all!

    <?php
    class Display_lib
    {
    
        public function user_page($data,$name)
        {
            $CI =& get_instance ();
    
            $CI->load->view('preheader_view',$data);
            $CI->load->view('header_view');
            $CI->load->view('top_navigation_view');
            $CI->load->view($name.'_view',$data);
            $CI->load->view('leftblock_view',$data);
            $CI->load->view('rightblock_view',$data);
            $CI->load->view('footer_view');        
        }
    }
    
    0 讨论(0)
  • 2020-12-08 01:39

    This library, easy to use and customize, does exactly what you'd expect:

    • avoid HTML duplication (header, footer..)
    • no need to learn a new language (!)

    Most Simple Template Library for CodeIgniter

    0 讨论(0)
  • 2020-12-08 01:39

    I'm biased towards this template library made by Carmelo Capinpin because it is so easy to use: link text. Just copy the file in your library and you're ready to go. Instructions on how to use it is in the link I provided.

    0 讨论(0)
提交回复
热议问题