0

I looked at the documentation and couldn't find anything that suggested this is possible. Here's my use case: I'm making a game and I've got 3 html files that are 90% the same. One is used for the actual game, one is for manual testing, and one is used for automated tests. Every time I change one, I've got to copy the change to the other two files for consistency. I'm trying to remove this DRY violation. Does haml give me a way to avoid copying and pasting this content to each of these files?

Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356

1 Answers1

1

I would recommend creating a partial file with all the common content shared between these 2 html files and then render the partial file using this notation

= render :partial => "common_content"

Check out more about partial files here http://haml.info/tutorial.html

Raghu
  • 2,543
  • 1
  • 19
  • 24
  • I *think* this is the answer I'm looking for. This must be a ruby concept and not a haml concept because it's not specifically mentioned in their documentation. I am not a ruby developer and I plan on using haml without rails. For completeness, would you mind explaining how that works? Perhaps with a snippet? – Daniel Kaplan Aug 30 '13 at 18:26
  • Thats actually a rails concept not a ruby. Rails allows you to create partials file which are nothing but your UI code fragments that can be reused. In Rails, All you have to do to create a partial is to create a file with the name starting with a undersore ( thats a convention you follow when u create a partial) something like _test.html.erb and the you can use that piece of code fragment or partial in you actaully template by saying = render :partial => "test" – Raghu Aug 30 '13 at 18:30
  • Just wondering if you are using any other framework other than rails for your development? – Raghu Aug 30 '13 at 18:31
  • Yeah I'm using [grunt](http://gruntjs.com/) (which I think is for node.js). It has with a [haml plugin](https://github.com/jhchen/grunt-contrib-haml). Other than that, I'm just generating static content. There is no server. – Daniel Kaplan Aug 30 '13 at 18:33
  • Check this thread http://stackoverflow.com/questions/18263750/static-html-compilation-with-partials-using-grunt-js and see if helps. Im not a grunt expert but i am good at googling :) – Raghu Aug 30 '13 at 18:37
  • Thank you, that looks like a better fit. – Daniel Kaplan Aug 30 '13 at 18:43