views:

88

answers:

2

I would like to store html snippets inside a yaml file, what is the best way to do that?

something like:

myhtml: |
  <div>
    <a href="#">whatever</a>
  </div>
+1  A: 

Example

Here is a sample record from a YAML-based snippet management system I created years ago:

- caption:    fieldset msie5
  mnemonic:   fieldset
  domain:     html
  desc:       fieldset and legend tag
  alias:      <@blank@>
  href_doc:   <@blank@>
  keywords:   <@blank@>
  body: |
      <fieldset>
      <legend>legend</legend>

      </fieldset>

You can repeat that or something like it for all the snippets you want to manage. This particular system stores the snippets as an array of name-value pairs (Perl people would call this an AoH). If you do not need all this extra information, you can easily get by just using two name-value pairs (e.g., caption + body).

The nice thing about this system is YAML indentation prevents "delimiter collision" problems. You do not have to escape the characters inside your snippet body.

Text Editor or IDE alternative

Note: Increasingly, text editors and IDEs support flexible snippet management options natively, so you may want to consider using the format of a text editor rather than re-inventing your own. If you do re-invent your own, you can write a script to translate your YAML format into the native format of a text editor if you later decide you want to do that.

See also:

http://en.wikipedia.org/wiki/Snippet%5F%28programming%29

http://en.wikipedia.org/wiki/Delimiter#Delimiter%5Fcollision

http://perldoc.perl.org/perldsc.html#ARRAYS-OF-HASHES (Perl AoH)

dreftymac
+1  A: 

use templates. we probably shan't mix to put html in .po or in datalayer since escape and double escape (javascript escape) rather keep distinct presentation (view) and persistance (storage) layers. html is presentation and view hence belongs to presentation layer

LarsOn