简体   繁体   中英

Replace tag in Apache velocity in java

Haven't used Apache velocity , but in current project, we use this library to populate data into templates.

would like to ask if it is possible to replace tag inside template, using Velocity ?

I have such template:

<head>
    <link href="conf/templates/template_style.css" rel="stylesheet" type="text/css"/>
</head>
<p>
    <span class="mention" data-id="295" data-value="${AutumnExams}">
        <span contenteditable="false">${AutumnExams}</span>
    </span>
</p>

and using such code:

private String applyDynamicContent(String templateHtmlData, List<DynamicContentPart> dynamicContentParts) throws ParseException {
   if (!dynamicContentParts.isEmpty()) {

       RuntimeServices rs = RuntimeSingleton.getRuntimeServices();
       SimpleNode sn = rs.parse(new StringReader(templateHtmlData), "Template body");
       Template template = new Template();
       template.setRuntimeServices(rs);
       template.setData(sn);
       template.initDocument();
       VelocityContext context = createVelocityContext(null, null);

       dynamicContentParts.forEach(dContent -> context.put(dContent.getPlaceholder(), dContent.getContent()));

       StringWriter writer = new StringWriter();
       template.merge(context, writer);
       return writer.toString();
   }
   return templateHtmlData;
}

I am replacing only: ${AutumnExams} placeholders. But is there any possibility to replace the whole SPAN for example based on data-value="${AutumnExams}"

I have 1-hour experience with velocity lib , so would appreciate help with code.

To replace the entire tag you would need to use the Velocity if condition ( more info at this page ) based on a parameter that you insert in the model. That parameter can then be based on a condition in the backend.

For example, if you want to have two versions of the span tag based on a boolean "foo" variable use the following code:

#if( $foo )
  <span class="mention" data-id="295" data-value="${AutumnExams}">
        <span contenteditable="false">${AutumnExams}</span>
    </span>
#else
<!-- second version of span -->
#end

What @gmcontessa said is correct, you will need to use the if-check on your span field. Easiest would be to check if the value isn't null and then just replace it in your span:

#if ($!AutumnExams)
    <span class="mention" data-id="295" data-value="$!AutumnExams">
        <span contenteditable="false">$!AutumnExams</span>
    </span>
#end

The main reason for using $! is to check if your variable has a value or not and if it doesn't it will just print an empty string. When used with an if-check it returns false if the variable is blank.

Also if you want to run a specific if-check on what the value of that velocity variable is you can do this:

#if ("$!AutumnExams" == "Maths")
    <span class="mention" data-id="295" data-value="$!AutumnExams">
        <span contenteditable="false">Maths</span>
    </span>
#else
    ##Code here
#end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM