@compress
doesn't completely remove whitespace-regions because that would change the meaning of the output in almost all applications. @compress
itself is technically just a custom directive (that's why it uses @
instead of #
). Only it's always there for backward compatibility... So, yes, you can implement filters like that. You could implement TemplateDirectiveModel
(where you can use your own Writer
implementation to do the filtering) then drop it into the data-model, like, with name myCompress
. Or you can pull it into the template like #assign myCompress = 'com.example.MyCompressDirective'?new
, wherever you would use #macro
. In both cases, you could use it like <@myCompress>...</@myCompress>
.
Actually, you can solve this with a macro too. You can capture the generated output inside the macro with <#local captured><#nested></#local>
, then use regular expressions with ?replace
and such. It's just kind of ugly... filtering fits Java better. (Note that if the output_format
of the defining template is HTML
, XML
, etc., then you will need something like captured?markup_string?replace(...)?no_esc
. That is, you have to convert the captured markup to plain string, do the modifications on that, then convert it back to markup again.)
Note that you can also solve compression outside the template, if you pass a custom compressing Writer
to Template.process
.