Output to browser in .rhtml templates

Today I learned how to append dynamically content to output send to browser from .rhtml files. But for this action <%= should be used? Yes, however this is not always exactly what is needed. All code between tags <%= and %> is executed and result of this code is send to browser. But sometimes not all output is needed to send, or just in some bigger chunk of code You need to append something to HTML code being currently generated. This can be fixed with switching between <% and <%= modes, but this makes code less readable. So what You can do about it?

You can use _erbout object to append HTML. _erbout contains all already generated HTML code, so You need add some data and voila! does not need to switch between modes.

_erbout << "My special HTML code"

It is not shorter than switching between modes, but when I read .rhtml templates, too much <% makes harder to read this files. At least for me ;))

Probably writing more elegant code allows to avoid such shortcuts, but as for now I’m using it from time to time.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.