How do you put HTML or XML into a YAML?
Asked Answered
T

2

35

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>
Tousle answered 23/11, 2009 at 19:31 Comment(0)
F
40

Example

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

- caption:    fieldset msie5
  tinycap:    fieldset
  domain:     html
  desc:       fieldset and legend tag
  wwbody: |
      <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, just two name-value pairs will suffice (e.g., caption + body).

The nice thing about this system: YAML indentation prevents "delimiter collision" problems. You never have to use clumsy escape sequences 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:

Furrier answered 23/11, 2009 at 21:19 Comment(0)
N
0

Use templates. We probably shan't mix to put HTML in .po files or in the data layer since escape and double escape (JavaScript escape) rather keep distinct presentation (view) and persistence (storage) layers. HTML is presentation and view, hence belongs to the presentation layer.

Nadene answered 26/11, 2009 at 12:36 Comment(2)
Personally I'm not a fan of an answer that is just "you shouldn't do that". Maybe the OP is aware of the pros and cons and has decided this is what they need to do. Don't lecture them -- at least not without pairing it with the actual solution to their question (ie, "I wouldn't do it, but here's how to make it work...")Semang
In my case I need to store a small chunk of xml in my config file (because I need to send it to a service from time to time). It's not markup, it's part of my program's operation. Storing it in config is sensible. Your answer would add a new layer of complexity and I would gain nothing from it.Mowbray

© 2022 - 2024 — McMap. All rights reserved.