freemarker - 如何在 FreeMarker 中嵌套相同的自定义指令?
问题描述
这是我的模板:
<@base64>head://<@base64>{"add":"addr","aid":"0","host":"sefse.sdf.xyz"}</@base64></@base64>
这是我的指令:
class Base64Directive : TemplateDirectiveModel {
@Throws(TemplateException::class, IOException::class)
override fun execute(env: Environment,
params: Map<*, *>, loopVars: Array<TemplateModel?>,
body: TemplateDirectiveBody?) { // Check if no parameters were given:
if (!params.isEmpty()) {
throw TemplateModelException(
"This directive doesn't allow parameters.")
}
if (loopVars.size != 0) {
throw TemplateModelException(
"This directive doesn't allow loop variables.")
}
// If there is non-empty nested content:
if (body != null) {
// Executes the nested body. Same as <#nested> in FTL, except
// that we use our own writer instead of the current output writer.
body.render(Base64FilterWriter(env.out))
} else {
throw RuntimeException("missing body")
}
}
/**
* A [Writer] that transforms the character stream to upper case
* and forwards it to another [Writer].
*/
private class Base64FilterWriter internal constructor(out: Writer) : Writer() {
private val out: Writer = out
@Throws(IOException::class)
override fun write(cbuf: CharArray, off: Int, len: Int) {
var transformedCbuf = String(Base64.getEncoder().encode(String(cbuf).toByteArray(Charsets.UTF_8).clone()))
out.write(transformedCbuf)
}
@Throws(IOException::class)
override fun flush() {
out.flush()
}
@Throws(IOException::class)
override fun close() {
out.close()
}
}
}
有两个相互嵌套的指令。当我运行我的代码时,它无法从内部编译指令对(<@base64>{"add":"addr","aid":"0","host":"sefse.sdf.xyz"}/@ base64)到外部(<@base64>head://base64-after/@base64)。
我怎样才能使这个编译工作从内到外?
解决方案
推荐阅读
- python - 在熊猫中连接两个具有不同列的数据框
- bazel - 在 Bazel 中为 git_repository 指定多个 Git 遥控器
- rust - 如何使用兄弟/相邻 rust 文件中的结构?
- swift - 不能在属性初始化器中使用实例成员“currColor”;属性初始化程序在“自我”可用之前运行
- php - MySQL/PHP - 基于具有 ORDER 和 LIMIT 的另一列选择 DISTINCT 列
- python - 如何显示从字典到元组的文本中短语的频率?
- pyspark - 将 SQL 查询转换为 PySpark DataFrame 查询(百分比排名计算)
- css - 如何为 Vuetify 制作动画
关于状态变化? - python - 自定义损失 - 对 tf.Tensor 的迭代适用于虚拟数据,但不适用于 model.fit_generator
- apache-kafka - Alpakka Kafka 流永远不会被终止