I may get down voted because I don't have any references, but...
What you're referring to is how every referential IT system works. It takes data from a dynamic resource (either external or on-page) & appends it into the page
I want to call it parsing
but I don't think that's the right technical term. My thinking is in PHP, your page will first go through PHP (where it will output any PHP elements as plain text), and then that parsed document will be sent to the front-end HTML processor
So to answer your question, I would say that you're probably over-complicating it. HTML is the base-standard for output, so why re-invent the wheel? Your parser should only look for its relevant tags & then run the code inside. It doesn't have to recognize the HTML inside - just treat it all as plain text, unless you want to render more of your code
If what you proposed was right, we wouldn't have to write any HTML in Rails or PHP. We'd just use the in-built methods
For example:
<p><? echo "hello" ?></p>
In my simplistic definition, PHP would first render the "plain text" output for the <? ?>
, and then send the formatted page to the front-end as HTML