1

controller

$data["controller"] = $table;
$data["rows"] = $this->base_model->get();//get table attributes and names
$write_add = $this->load->view("generate/add",$data,TRUE);//load file into var
$file_vadd = fopen($path["dirname"].'/generate/views/'.$data["controller"].'/add.php',"w+"); //write to file

generate/add.php (template)

<?php echo "<?php " ?>echo form_open('<?=$controller?>/add'); ?>
        <fieldset>
        <legend>New <?=$controller?></legend>
        <?php foreach($fields as $field): ?>
            <label for="<?php echo $field->name?>" ><?php echo $field->name?></label>
            <?php if(!empty($rel)) : ?>
                    <?php if($rel["rel_type"]==1 && $field->name == $rel["rel_fk"] ):?>
                        <select name="<?php echo $field->name?>" id="<?php echo $field->name?>">

                            <?php echo "<?php " ?> foreach($<?=$rel["rel_table"]?>_option as $<?=$rel["rel_table"]?>): ?>
                                <option value="<?php echo "<?php " ?>echo $<?=$rel["rel_table"]?>-><?=$rel["rel_pk"]?>; ?>" ><?php echo "<?php " ?>echo $<?=$rel["rel_table"]?>-><?=$rel["rel_view"]?> ;?></option>

                            <?php echo "<?php " ?> endforeach; ?>
                        </select>

                    <?php else: ?>
                        <input type="text"  name="<?php echo $field->name?>" id="<?php echo $field->name?>"  />
                <?php endif; ?>  
            <?php endif; ?>
        <?php endforeach;?>
        <input name="submit" type="submit"  value="Add"/> 
       </fieldset>   
    </form>

i have a controller which will call this template to create views, controllers and models. i dont know a better way to do this and i dont like putting the php tag inside a php tag. suggestion?

This is like "bake" function in cakephp. But im creating a codeigniter version of bake. the script is running. But my code is messy.? looking for better way to manage the template file..

tereško
  • 58,060
  • 25
  • 98
  • 150
li9ht
  • 465
  • 5
  • 11
  • A good question. You could take a look how Smarty does it and I'd like to know it as well :) – Ignas Dec 13 '11 at 09:25
  • Please check this post: [http://stackoverflow.com/questions/8486807/how-to-creating-php-template-file][1] [1]: http://stackoverflow.com/questions/8486807/how-to-creating-php-template-file – SMSM Dec 12 '12 at 17:30
  • [http://stackoverflow.com/questions/8486807/how-to-creating-php-template-file][1] [1]: http://stackoverflow.com/questions/8486807/how-to-creating-php-template-file – SMSM Dec 12 '12 at 17:31

1 Answers1

0

A simple way of doing this is enabling output buffering, including the php file (which lets it do its thing), end output buffering and saving the result in a file. Something along the lines of

ob_start();
include("foobar.php");
$temp .= ob_get_clean();
file_put_contents('foo.tpl', $temp);
Jan Dragsbaek
  • 8,078
  • 2
  • 26
  • 46
  • well .. i already manage to run the script before.. its just that im looking for a better way to write my code above.. instead of echo my php tag inside a php tag – li9ht Dec 13 '11 at 09:42
  • This was really hard to read out of your question, and i'd suggest you to rewrite and rethink your initial question. – Jan Dragsbaek Dec 13 '11 at 09:44
  • sorry..english is not my first language..it just that may be im bad at explaining things.. may be its clearer with an example... im creating a bake function like cakephp for codeigniter.. the code that i post is a template file .im looking for a better way to write the template file – li9ht Dec 13 '11 at 09:47