nifty-generators icon indicating copy to clipboard operation
nifty-generators copied to clipboard

Custom template files

Open craic opened this issue 15 years ago • 7 comments
trafficstars

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.

craic avatar Oct 26 '10 17:10 craic

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?

ryanb avatar Dec 28 '10 23:12 ryanb

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

craic avatar Dec 28 '10 23:12 craic

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 lib/generators/nifty/scaffold/templates/views/erb/index.html.erb in your Rails app.

You can do the same thing for any of the generator templates such as lib/generators/nifty/scaffold/templates/model.rb

Let me know if that works for you.

ryanb avatar Dec 29 '10 01:12 ryanb

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 ... picks up custom index.html.erb from lib/templates/erb/scaffold/index.html.erb

But nifty:scaffold just gets the default nifty:scaffold index page $ rails generate nifty:scaffold mymodel name:string ... does not pick up a custom template in your path (as given above in your message of Dec 28)

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.

craic avatar Feb 17 '11 17:02 craic

Hmm, I'm not sure why it isn't working. I'll need to do more research to try to determine the problem.

ryanb avatar Feb 18 '11 18:02 ryanb

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. Any hints?

Best regards,

Christian

chnoack avatar Mar 18 '11 08:03 chnoack

Hi, I got this to work by copying the templates to lib/templates/nifty/scaffold/views/erb in my Rails app.

barbaraw avatar Apr 11 '11 20:04 barbaraw