Geertjan's Blog

  • May 20, 2012

HTML Embedding for FreeMarker

Geertjan Wielenga
Product Manager

When you click on @_sites, as you can see below, the IDE lets you open the file registered under that namespace, in this case, "/macros/sites.ftl":

But I'm thinking that any FreeMarker support would be better if it were embedded in the HTML Editor.

That means, the following needs to happen:

  1. Add this statement to the constructor of the FreeMarker data object, so that its MIME type is set to "text/html":
  2. Create a language provider that will embed the FreeMarker MIME type within the HTML editor, if the current token ID is "TEXT", which is what unrecognized tokens in HTML, such as those provided by the FreeMarker support, are mapped to:

    @ServiceProvider(service = LanguageProvider.class)
    public class HTMLEmbeddingLanguageProvider extends LanguageProvider {

        private Language embeddedLanguage;

        public Language<?> findLanguage(String mimeType) {
            return HTMLTokenId.language();

        public LanguageEmbedding<?> findLanguageEmbedding(Token<?> token, 
            LanguagePath languagePath, InputAttributes inputAttributes) {
            if (languagePath.mimePath().equals("text/html")) {
                if (token.id().name().equals("TEXT")) {
                    return LanguageEmbedding.create(embeddedLanguage, 0, 0, false);
            return null;

        private void initLanguage() {
            embeddedLanguage = MimeLookup.getLookup("text/x-ftl").lookup(Language.class);
            if (embeddedLanguage == null) {
                throw new NullPointerException("Can't find language for embedding");

  3. Change all the hyperlinkproviders. First, in the annotation at the top of the hyperlinkproviders, change from your MIME type (e.g., text/x-ftl) to text/html. Then, get your embedded language like this, from the HTML token sequence:

    TokenHierarchy<?> th = TokenHierarchy.get(doc);
    TokenSequence htmlTs = th.tokenSequence(Language.find("text/html"));
    if (htmlTs == null || !htmlTs.moveNext() && !htmlTs.movePrevious()) {
        return null;
    TokenSequence<SJTokenId> ts = htmlTs.embedded();
    if (ts == null) {
        return null;

The above gets you here, which is not perfect, but on the way to where it probably should be, i.e., HTML support within a FreeMarker editor:

Click to enlarge the image above to get a fuller perspective.

Join the discussion

Comments ( 1 )
  • rgm Sunday, May 20, 2012

    This goes above and beyond my expectations of Netbeans freemarker support -- wonderful!

    Do you think it'll be possible for end-users to use Netbeans with highlighting to edit freemarker templates for non-HTML files, like JSP, CSS, Java, or Resource Properties?

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.