CodeIgniter or PHP Equivalent of Rails Partials and Templates

后端 未结 11 1841
猫巷女王i
猫巷女王i 2020-12-30 14:13

In CodeIgniter, or core PHP; is there an equivalent of Rails\'s view partials and templates?

A partial would let me render another view fragment inside my view. I co

11条回答
  •  旧巷少年郎
    2020-12-30 15:02

    this is essentially what I use:

    function render_partial($file, $data = false, $locals = array()) {
        $contents = '';
    
        foreach($locals AS $key => $value) {
            ${$key} = $value;
        }
    
        ${$name . '_counter'} = 0;
        foreach($data AS $object) {
            ${$name} = $object;
    
            ob_start();
            include $file;
            $contents .= ob_get_contents();
            ob_end_clean();
    
            ${$name . '_counter'}++;
        }
    
        return $contents;
    }
    

    this allows you to call something like:

    render_partial('/path/to/person.phtml', array('dennis', 'dee', 'mac', 'charlie'), array('say_hello' => true));
    

    and in /path/to/person.phtml have:

     ()
    

    this is some magic going on though that may help you get a better picture of what's going on. full file: view.class.php

提交回复
热议问题