Skip to content

Commit

Permalink
Added optional action and action views templates
Browse files Browse the repository at this point in the history
Added the possibility to have custom action templates and view templates
for controllers.

Generate the controller as normal (i.e. bob c test add edit delete)
when generating the controller and it's views, bob will look for a
/bob/controller/action.#action_name#.tpl file for specific actions and
view.#action_name#.tpl for the views.

This will allow you for instance to always fire a custom event in your
controllers when the delete action is called
  • Loading branch information
Perturbatio committed May 22, 2012
1 parent 5342b9c commit 5a625c5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
24 changes: 24 additions & 0 deletions classes/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,30 @@ public static function load_template($template_name)
static::error('A generation template could not be found for this object.');
}
}
/**
* Load the source from a template file and return it
* as a string.
*
* @param $template_name string The file name of the template.
* @return string The template content.
*/
public static function template_exists($template_name)
{
// first look in the project templates this way
// the user can have project-specific templating
if(File::exists($source = Config::get('bob::options.project_templates').$template_name))
{
return true;
}
elseif(File::exists($source = Config::get('bob::options.template_path').$template_name))
{
return true;
}
else
{
return false;
}
}

/**
* Use a key-value array to replace markers within
Expand Down
20 changes: 17 additions & 3 deletions classes/generators/controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ private function _controller_generation()

// holder for actions source, and base templates for actions and views
$actions_source = '';
$action_template = Common::load_template('controller/action.tpl');
$view_template = Common::load_template('controller/view.tpl');
$main_action_template = Common::load_template('controller/action.tpl');
$main_view_template = Common::load_template('controller/view.tpl');

$restful = (strstr(implode(' ', $this->arguments), ':')) ? true : false;

Expand All @@ -90,11 +90,25 @@ private function _controller_generation()
// add the current action to the markers
$markers['#ACTION#'] = Str::lower($action);
$markers['#VERB#'] = $verb;


$custom_action_template_name = 'controller/action.' . Str::lower($action) . '.tpl';
if ( Common::template_exists($custom_action_template_name) ){
$action_template = Common::load_template($custom_action_template_name);
} else {
$action_template = $main_action_template;
}

// append the replaces source
$actions_source .= Common::replace_markers($markers, $action_template);

$file_prefix = ($restful) ? $verb.'_' :'';

$custom_view_template_name = 'controller/view.' . Str::lower($action) . '.tpl';
if ( Common::template_exists($custom_view_template_name) ){
$view_template = Common::load_template($custom_view_template_name);
} else {
$view_template = $main_view_template;
}


// add the file to be created
Expand Down

0 comments on commit 5a625c5

Please sign in to comment.