Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8028793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:13:54+00:00 2026-06-05T00:13:54+00:00

I spent a while trying to identify this ‘special variable’ in the documentation. I

  • 0

I spent a while trying to identify this ‘special variable’ in the documentation. I had a case where I wanted to be able to pass the root hash to a macro which would operate on it. I found references to Environment.getCurrentEnvironment() but that works in Java not templates. How do you pass the root data model to a macro?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T00:13:56+00:00Added an answer on June 5, 2026 at 12:13 am

    Below is a unit test that successfully does what I was after. The key was the ‘.data_model’ variable.

    public class TestFreeMarkerTemplating {
    
    Configuration cfg = new Configuration();
    StringTemplateLoader stringLoader = new StringTemplateLoader();
    { cfg.setTemplateLoader(stringLoader);
    cfg.setObjectWrapper(new BeansWrapper()); }
    
    @Test
    public void testTestableMacros() throws TemplateException, IOException{
        stringLoader.putTemplate("root", "<#macro user testPojo>Welcome ${testPojo.user}. <@subPojo sub/></#macro><#macro subPojo sub>Sub ${sub.user}!</#macro>");
        stringLoader.putTemplate("testPojoTemplate", "<#import \"root\" as w><@w.user .data_model/>");
        stringLoader.putTemplate("testSubPojoTemplate", "<#import \"root\" as w><@w.subPojo .data_model/>");
        assertTemplateAndBeanYield("root", new TestPojo(), "");
        assertTemplateAndBeanYield("testPojoTemplate", new TestPojo(), "Welcome Andy. Sub Bill!");
        assertTemplateAndBeanYield("testSubPojoTemplate", new SubPojo(), "Sub Bill!");
    
    }
    
    public void assertTemplateAndBeanYield(String templateName, Object bean, String expectedOutput) throws IOException, TemplateException{
        Template temp = cfg.getTemplate(templateName);
        StringWriter out = new StringWriter();
        temp.process(bean, out);
    
        assertEquals(expectedOutput, out.toString());
    }
    
    public static class TestPojo {
        private final String user = "Andy";
        private final SubPojo sub = new SubPojo();
    
        public String getUser() { return user; }
        public SubPojo getSub() { return sub; }
    }
    
    public static class SubPojo {
        private final String user = "Bill";
    
        public String getUser() { return user; }
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've spent a while trying to get this to work however I've had little
I've spent the better part of my day trying to solve this message while
I've spent quite a while trying to figure this out. I am working on
I've spent a while trying to figure this one out. I'm sure someone with
I've spent a good while this afternoon trying to implement the deserialization of JSON
This struck me as really weird behaviour and I spent a while checking for
I've spent a while on this but can't get this to work, I apologize
I've spent so much time trying to figure this out. I'm building a dialog
I've spent half day trying to figure out this and finally I got working
Is there another way to do this? Just spent 2 hours trying to figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.