Freemarker update variable




















I had a bunch of names like Name1, Name2, To make a table of these names I used this code:. There's no directive that assigns to a variable that has a dynamic name. But here's a hack to achieve that:. I found the answer from here , and it works to me. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams?

Collectives on Stack Overflow. Learn more. Dynamic Freemarker variable name Ask Question. Asked 7 years, 7 months ago. Active 2 years, 10 months ago. Viewed 7k times. I just use void as a variable name to indicate that the method does not return anything. Tip: Did you notice the FreeMarker comment line in this template? You specify the name of the variable and the full class name of the Java class. Those two methods, creating a result via output string generation and directly modifying Java objects are the gist of using FreeMarker for any configuration purpose.

Depending on the use case you will basically always use characteristics of one of them or a combination of the two. Now that we have the basics covered we can try some use cases involving transforming between formats. For this use case it makes sense to use the string output generation of the template to build our JSON.

And with NodeModel which comes with FreeMarker we already have a very simple way to convert a string containing the XML into an object that can be easily used inside a FreeMarker template. Then we can just load the data node from the XML in our FreeMarker template and use it like a normal template to render an output. So, we cannot parse the JSON in the Java class like we did with the XML, but we need to handle this in the template itself, the template will just receive the input as a string.

For accessing the JSON inside the template the simplest solution is to parse it into a map, as FreeMarker has an easy approach on accessing maps. We need to create a utility class that converts a JSON-string into a map which we will then call from inside the template.

This is using a Jackson object mapper to parse the string into a map, but you could just as well use a different framework. The main method does not any longer have to do the parsing of the input, but instead must somehow make the JsonUtil class available to the template. We need to access those static methods and we cannot simply put the class directly into the data map. But luckily FreeMarker provides us with the functionality to the class into the data map using a static models wrapper.

For the real use case you could add several different utility classes to support parsing different input formats. Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search. I can't make this happen using the code below and instead I'm only getting the last instance from the loop.

Output: Actual: Expected:. I'm using the list directive incorrectly and I needed to place the root. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 4 years, 11 months ago. Active 4 years, 11 months ago. Viewed 1k times. Macro and function parameters are also local variables. User defined directives, like macros, can also create loop variables.

They are only visible directly between these tags, not from macros or functions called from there. As such, they are quite similar to local variables, but they can't be assigned to directly.

Global variables : These should be seldom used. Global variables are shared by all templates, even if they belong to different name spaces because of import -ing.

Thus, their visibility is similar to data-model variables. They are set via the global directive.



0コメント

  • 1000 / 1000