# Create HTML snippets in Ruby

**URL:** <https://www.ruby-forum.com/t/create-html-snippets-in-ruby/255667>\
**Category:** Development\
**Created:** [February 19, 2020, 1:19pm UTC](https://www.ruby-forum.com/t/create-html-snippets-in-ruby/255667 "2020-02-19T13:19:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lykos42](https://www.ruby-forum.com/user_avatar/www.ruby-forum.com/lykos42/32/1723_2.png) [@Lykos42](https://www.ruby-forum.com/u/Lykos42)\
**Post date:** [February 19, 2020, 1:19pm UTC](https://www.ruby-forum.com/t/create-html-snippets-in-ruby/255667/1 "2020-02-19T13:19:54Z")

</div>

I want to create some small HTML snippets in Ruby. For example, in one place, I have an image URL and I want to create this from it

```
<img src='URL'>

```

Plus a few other similar things. But I haven’t found the right tool for it yet.

Alternatives I looked at and that didn’t work for me:

- Using an Erb file. This seems to be recommended in many places, but it seems overkill to me. This seems perfect if you want bigger snippets where you want to change the structure independently of thr code. I just want a few little snippet that can be generated directly in the code, adding the erb files adds too much indirection and complexity and isn’t justified for this. Of course I could live with it, but I don’t like it.
- Currently I just use string concatenation. This sucks and looks ugly. Of course, it works and given that it’s just a small internal project that only I run, I don’t have to worry about attack vectors. Besides, I could even fix escaping and make it moderately safe. But it’s just ugly as fuck, that’s why I would love to transition to a better solution.
- I found Nokogiri::HTML::Builder. When I saw it, I immediately loved the API and thought this is exactly what I want. However, it seems it wants to generate whole documents that also contain this “” stuff. I don’t want that, I need only snippets that contain my stuff without any extras. I tried figuring out how to surpress the doctype stuff, but I didn’t manage. I could of course postprocess the output and remove it, but then it gets brittle and just as ugly as any other solution, so I eventually gave up. But if someone knows how to make Nokogiri::HTML work for snippets rather than documents, that would be a nice solution.

Note that I already asked this on Stackoverflow, but it was closed for being too opinion based.  
[https://stackoverflow.com/questions/60169418/create-html-snippet-in-ruby#comment106424182\_60169418](https://stackoverflow.com/questions/60169418/create-html-snippet-in-ruby#comment106424182_60169418)

---

<div class="post-metadata">

**Author:** ![coze](https://www.ruby-forum.com/user_avatar/www.ruby-forum.com/coze/32/752_2.png) [@coze](https://www.ruby-forum.com/u/coze)\
**Post date:** [February 26, 2020, 10:54am UTC](https://www.ruby-forum.com/t/create-html-snippets-in-ruby/255667/2 "2020-02-26T10:54:22Z")

</div>

> [@Lykos42](#):
>
> ![]()

Maybe you can use some custom classes and heredoc templates ? Here’s an example :

[https://repl.it/repls/AgedRuddyMethods](https://repl.it/repls/AgedRuddyMethods)

---

<div class="post-metadata">

**Author:** ![YeomanRando](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/22d042/32.png) [@YeomanRando](https://www.ruby-forum.com/u/YeomanRando)\
**Post date:** [February 29, 2020, 4:13pm UTC](https://www.ruby-forum.com/t/create-html-snippets-in-ruby/255667/3 "2020-02-29T16:13:27Z")

</div>

Not sure exactly what you are going for, but you are suggesting some big guns for a little mosquito.

```
url = "http://www.something.com/this/that/image001.jpg"
tag = "<img src=\"#{url}\">"

```

#{} is string interpolation.

---

<div class="post-metadata">

**Author:** ![Lykos42](https://www.ruby-forum.com/user_avatar/www.ruby-forum.com/lykos42/32/1723_2.png) [@Lykos42](https://www.ruby-forum.com/u/Lykos42)\
**Post date:** [March 30, 2020, 9:03am UTC](https://www.ruby-forum.com/t/create-html-snippets-in-ruby/255667/4 "2020-03-30T09:03:20Z")

</div>

Thanks for the suggestions, but I don’t like the possibility of a bug causing \<\> to be present in the UI and suddenly there’s a potential for XSS… Technically this can’t happen atm, but I really feel uncomfortably if I have such vulnerable code lying around. Maybe I’ll forget and reuse it for something else later and boom.
