All scripts in this post checked with laast nightly 1.2 build.

In continue of previous post i want to show how to extract similar elements of views to partials on example.

I create several bake scripts that allow to solve this.

After we place files to desired places we can use it with bake script.

So if we have controller Articles we run next commands.

cake bake view articles partial _partial 

cake bake view articles add

cake bake view articles edit

/vendors/shell/templates/views/form.ctp

<div class=”<?php echo $pluralVar;?> form”>
<?php echo “<?php echo \$form->create(‘{$modelClass}’);?>\n”;?>
    <fieldset>
         <legend><?php echo “<?php __(‘”.Inflector::humanize($action).” {$singularHumanName}’);?>”;?></legend>
<?php
        
echo “\t<?php\n”;
        foreach (
$fields as $field) {
            if (
$action != ‘add’ && $field == $primaryKey) {
                
$humanName=$field;
                
$humanName=Inflector::humanize($humanName);
                echo 
“\t\techo \$form->input(‘{$modelClass}.{$field}’,array(‘label’=>__(‘{$humanName}’,true)));\n”;
            }
        }
        echo 
“\t\techo \$this->renderPartial(‘_partial’);\n”;
        echo 
“\t?>\n”;
?>
    </fieldset>
<?php
    
echo “<?php echo \$form->end(‘Submit’);?>\n”;
?>
</div>
<div class=”actions”>
    <ul>
<?php if ($action != ‘add’):?>
        <li><?php echo “<?php echo \$html->link(__(‘Delete’, true), array(‘action’=>’delete’, \$form->value(‘{$modelClass}.{$primaryKey}’)), null, sprintf(__(‘Are you sure you want to delete # %s?’, true), \$form->value(‘{$modelClass}.{$primaryKey}’))); ?>”;?></li>
<?php endif;?>
        <li><?php echo “<?php echo \$html->link(__(‘List {$pluralHumanName}’, true), array(‘action’=>’index’));?>”;?></li>
<?php
        $done 
= array();
        foreach (
$associations as $type => $data) {
            foreach(
$data as $alias => $details) {
                if (
$details[‘controller’] != $this->name && !in_array($details[‘controller’], $done)) {
                    echo 
“\t\t<li><?php echo \$html->link(__(‘List “.Inflector::humanize($details[‘controller’]).“‘, true), array(‘controller’=> ‘{$details[‘controller’]}’, ‘action’=>’index’)); ?> </li>\n”;
                    echo 
“\t\t<li><?php echo \$html->link(__(‘New “.Inflector::humanize(Inflector::underscore($alias)).“‘, true), array(‘controller’=> ‘{$details[‘controller’]}’, ‘action’=>’add’)); ?> </li>\n”;
                    
$done[] = $details[‘controller’];
                }
            }
        }
?>
    </ul>
</div>

/vendors/shell/templates/views/partial.ctp

 <?php
        
echo “\t<?php\n”;
        foreach (
$fields as $field) {
            if (
$field == $primaryKey) {
                continue;
            } elseif (!
in_array($field, array(‘created’‘modified’‘updated’))) {
                
$humanName=$field;
                if (
substr($humanName, –3) == ‘_id’) {
                    
$humanName substr($humanName0strlen($humanName) – 3);
                }                 
                
$humanName=Inflector::humanize($humanName);
                echo 
“\t\techo \$form->input(‘{$modelClass}.{$field}’,array(‘label’=>__(‘{$humanName}’,true)));\n”;
            }
        }
        if(!empty(
$associations[‘hasAndBelongsToMany’])) {
            foreach (
$associations[‘hasAndBelongsToMany’] as $assocName => $assocData) {
                echo 
“\t\techo \$form->input(‘{$assocName}’);\n”;
            }
        }
        echo 
“\t?>\n”;
?>

Advertisements