Such a template is quite easy to parse.
The key is recognizing that such templates basically consist of a sequence of just two kinds of strings: boilerplate (HTML) text, and script text.
The boiler plate text basically starts with "%>" and ends at "<%" (with special cases at begin-template and end-template). Script text is just everything else. Yes, you can pick off both with just a while loop for each one that watches for "<%", "%>" or "end-of-template". The sequence is implicit in alternating back and forth. That makes for pretty simple parser:
while not eof
boilerplate="";
while next_characters~="<%" or eof
boilerplate concat next_characters
end
scripttext="";
while next_characters~="%>" or eof
scripttext concat next_characters
end
end
(I leave the details of the individual character management for the coder).
What you didn't say is what you wanted to do with the parsed result. If the goal is to "generate output" from the parsed result, you'll have to turn it into a program.
That's actually pretty easy.
Basically you write the result to a file and compile it.
For each piece of collected boilerplate text, emit a print statement that prints the boilerplate text; you may have to escape the characters to make them legal in a string literal in your chosen target language, or break the boilerplate into multiple blocks to print it. For each block of script text, simply emit that unchanged. You'll likely have to emit a prolog text chunk to make a function header, and as postlog text chunk to make a function end.
That's it.
[Because of the trivial conversion between such "templates" and a simple program with print statements, I don't find such template programming to be very enticing. It saves me a few print keywords, and that's it.]