简体   繁体   English

在Java中替换Apache Velocity中的标记

[英]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. 尚未使用Apache velocity ,但是在当前项目中,我们使用此库将数据填充到模板中。

would like to ask if it is possible to replace tag inside template, using Velocity ? 想问问是否可以使用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. 我只替换: ${AutumnExams}占位符。 But is there any possibility to replace the whole SPAN for example based on data-value="${AutumnExams}" 但是有可能替换整个SPAN,例如基于data-value="${AutumnExams}"

I have 1-hour experience with velocity lib , so would appreciate help with code. 我有1小时的velocity lib经验,因此希望获得代码帮助。

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. 要替换整个标签,您将需要根据要插入模型的参数使用Velocity if条件( 在此页面上的更多信息 )。 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: 例如,如果要基于布尔“ foo”变量拥有两个版本的span标记,请使用以下代码:

#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. @gmcontessa说的是正确的,您将需要在跨度字段上使用if-check。 Easiest would be to check if the value isn't null and then just replace it in your span: 最简单的方法是检查该值是否不为null,然后在您的跨度中将其替换:

#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. 与if-check一起使用时,如果变量为空,则返回false。

Also if you want to run a specific if-check on what the value of that velocity variable is you can do this: 另外,如果要对速度变量的值进行特定的if检查,则可以执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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