How to use utf-8 encoded data in erb template

后端 未结 4 736
盖世英雄少女心
盖世英雄少女心 2021-01-20 23:38

I have an data-file stored with utf-8 encode, and I want to embed the data to an erb template. The data-file is explicitly encoded with utf-8 at the top. But while

4条回答
  •  攒了一身酷
    2021-01-21 00:06

    (I decided to write different answer)

    Two issues, I think.

    • datafile encoding on input
    • how you output

    The erb library knows about the encoding specification in magic comments, but the data file part, you need to take care by yourself. So, when you read the file, you have to specify encoding, or specify default encoding beforehand.

    On output, you need to specify the encoding for output. You can specify per I/O channel basis.

    To specify default encoding (easiest), you can:

    Encoding.default_external = "UTF-8"
    

    to use UTF-8 for all I/O.

提交回复
热议问题