Custom template files #61
Comments
Sorry for not getting back to you sooner on this, but can you clarify what you are looking for? Do you want more customization in the generator behavior? Can you provide an example of this? |
What was I looking for? ... let me just fetch that from cache... So with Rails3 regular scaffolding I can add my own erb scaffold templates for views in myapp/lib/templates/erb/scaffold. The regular scaffold generator will use those in preference to the Rails defaults. I would like to have the same functionality for controllers, models, tests where I can drop in custom templates into an individual Rails project. What I'm doing at the moment is modifying files in the nifty_generators gem templates directory (nifty-generators-0.4.2/lib/generators/nifty/scaffold/templates) so that I can create, for example, a custom model file. Now that works great - but it is system-wide and I really don't want to mess with the innards of gems. So if nifty_generators looked in myapp/lib/templates first and used anything in there before using its defaults, that would be great. For some parts of a MVC 'module' (not sure what the right term is for that entire bolus of code), such as the model .rb file itself, this just involves rendering a custom erb template. But for the tests, if I remember correctly, this was a little more involved - I had to modify some of the generator code. So there may not be a simple way to use templates for ALL the pieces of that need to be generated. Hope that makes sense. ... and thanks for all your good works for the community - very much appreciated --Rob Jones |
If I'm understanding you correctly, Rails 3 already has this functionality. You can place your own templates in the lib/generators directory in your Rails app. For example if you want to customize the index template for the view you would place it in You can do the same thing for any of the generator templates such as Let me know if that works for you. |
Finally got back to looking at this - and sorry, that doesn't work for me (using nifty-generators 0.4.5 / Rails 3 / Ruby 1.9 / rvm) The regular scaffold generator will pick up a custom view template $ rails generate nifty:scaffold mymodel name:string But nifty:scaffold just gets the default nifty:scaffold index page Looking through your code I don't see a test for the existence of files under the application lib/templates directory so I presume you're inheriting this from Rails::Generators::Base I see reference to @_source_root in there which looks like the relevant variable but I'm not having much luck picking it apart beyond that. |
Hmm, I'm not sure why it isn't working. I'll need to do more research to try to determine the problem. |
Hi everybody, today I also tried to modify the templates coming with nifty-generators. As suggested above I copied them to lib/generators/nifty/scaffold/templates/views/erb/ and modified them, but it looks like they were not used while generating. Best regards, Christian |
Hi, I got this to work by copying the templates to lib/templates/nifty/scaffold/views/erb in my Rails app. |
This gem has the great feature of using templates for the controller, model, etc - not just the views.
I would love to see it able to use custom templates for a given project in the same way that the rails 3 generators can with respect to view templates.
Right now I can hack my installed nifty_generators gem but being able to tweak things for each project would be very nice. I'll try and have a go at this.
The text was updated successfully, but these errors were encountered: