Templates in Rails 3
Templates have been around since version 2.3.0 of Rails. They're great at getting an application started quickly with many of the gems, plugins, and code you need. Pratik has an excellent high level overview on the feature when it was first released. Now that Rails 3 is on the horizon, I just wanted to point out some of the new things that you can do with your templates.
Overview
As I'm sure you know, Rails 3 brings with it a number of rewrites to many areas of the codebase. The code that handles the templates is no different. What I found interesting is that the code that handles many of the actions within the templates comes from both Railties and Thor.
What's the same since 2.3
Many of the commands are the same from the previous iteration of the template feature. They include:
- gem
- plugin
- initializer
- rakefile
- generate
- run
- route
- inside
- ask
- yes?
- no?
- git
- environment
- run_ruby_script
- gsub_file
- append_file
What's new
Most of the new stuff comes from Thor and is not Rails specific:
- add_source - Adds a source line to the top of your Gemfile
- apply - Loads an external file and then executes it.
- thor - You can run any thor commands that you want within your template.
- copy_file - copies a file to a directory that you name
- get - will download files and place in a directory/file you name
- template - ERB template that you specify and will get executed
- chmod - Changes mode
- prepend_file - Prepends text to file
- inject_into_class - injects text into a given class
- inject_into_file - injects text into file
- remove_file - deletes files and entire directories
- create_file - creates an empty file
- empty_directory - creates an empty directory
- directory - copies directories. It's also recursive.
That's it! If you need more detailed information on how these functions work, check out the rdoc. The rdoc for the Thor commands be found here and the Rails template commands can be found here I'm pretty excited about these changes and they will definitely help me clean up my templates.
Posted 4/19/2010 08:31:00 AM - rails
WHO AM I?
I'm Rimas Silkaitis, a mild mannered Ruby dev living in San Francisco. I like building stuff for the web and am particularly interested in machine learning. I also own a set of turntables.
Posts

