Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Custom template files #61

Open
craic opened this issue Oct 26, 2010 · 7 comments
Open

Custom template files #61

craic opened this issue Oct 26, 2010 · 7 comments

Comments

@craic
Copy link

craic commented Oct 26, 2010

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.

@ryanb
Copy link
Owner

ryanb commented Dec 28, 2010

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?

@craic
Copy link
Author

craic commented Dec 28, 2010

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

@ryanb
Copy link
Owner

ryanb commented Dec 29, 2010

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.

@craic
Copy link
Author

craic commented Feb 17, 2011

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.

@ryanb
Copy link
Owner

ryanb commented Feb 18, 2011

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

@chnoack
Copy link

chnoack commented Mar 18, 2011

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

@barbaraw
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants