Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added optional action and action views templates #4

Open
wants to merge 1 commit into from

1 participant

@Perturbatio

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.

Also added a template_exists function to the Common class

@Perturbatio Perturbatio Added optional action and action views templates
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
5a625c5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 22, 2012
  1. @Perturbatio

    Added optional action and action views templates

    Perturbatio authored
    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
This page is out of date. Refresh to see the latest.
Showing with 41 additions and 3 deletions.
  1. +24 −0 classes/common.php
  2. +17 −3 classes/generators/controller.php
View
24 classes/common.php
@@ -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
View
20 classes/generators/controller.php
@@ -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;
@@ -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
Something went wrong with that request. Please try again.