Question

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..

Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top