tags:

views:

83

answers:

1

I'm working on a Merb application using Haml as the templating language. As haml encourages moving logic out of the view and into helpers, I soon started thinking about removing the copy / text from the templates themselves. In the past I have either just left the text inline within the templates, or moved it into separate yaml files separated by controller. I've found that leaving text inline is more convenient, but uglier and involves using Ack a lot to find out where a particular piece of text is stored. Storing it in a yaml file is easier to search, but harder to come up with a sane naming schema for finding a particular piece of text in a given area.

I'm also curious about other approaches to this issue.

+1  A: 

Storing texts separately is good if you want to globalize/internationalize your application. For rails there are a lot of fine plugins for doing this (Rails 2.3 even has an api for this), but these are usually working for merb too. There are various approaches to this, the two major approach is how you store the key:

  • The YAML-like interface you are using stores symbols I think, and it assigns the text to that symbol.
  • The gettext-style approach is that the text IS the symbol, and you can redefine it in a separate file if you want (but if you don't then you'll get back the same string as the key was).

Both approaches have their advantages and disadvantages. Apart from the key there are numerous storage facilities you might use, like YAML, or you can actually build a complete ActiveRecord/DataMapper/etc. based storage mechanism.

You might check how rails 2.3 does with globalization, because their naming schema might be a good starting point for you. Or use namespaces to split the YAML files, so they will be easier to search. You might try the gettext-approach too, but it's actually not considered neither DRY nor safe (but it has a lot of tools that will create mapping files that contains all of the text your application has).

SztupY