首页 > 解决方案 > Handlebars 模板中的切换功能

问题描述

因此,我们使用把手创建了许多模板。在众多车把中,我们有一个车把,我们希望在其中进行一些更改,这些更改仅应在特定日期后生效。为此,我们想创建一种拨动开关,例如:

{{if switch on}}
 display new content
{{else}}
 display old content

下面是通用模板解析器,我在其中尝试创建一个可以在模板的 if 部分中注入的开关。有什么建议么?

/**
     * Creates HTML output of the specified context for the given templateId and templateVersion combination
     * we implicitly assume certain json fields (template specific) to be present in the content
     */
    @Timed("handlebarsService.parseTemplateToHtml")
    fun parseTemplateToHtml(htmlTemplateLocation: String, model: Map<String, Any>, locale: Locale): String {
        val modelWithLanguage = model.toMutableMap()
        modelWithLanguage[languageTag] = locale.language
        //modelWithLanguage[switch] = "off"

        val context = Context.newBuilder(modelWithLanguage)
            .resolver(MapValueResolver.INSTANCE)
            .build()

        val template = try {
            handlebars.compile(htmlTemplateLocation)
        } catch (e: IOException) {
            throw PdfGenerationException(e, "Internal error while compiling template")
        }

        return try {
            template.apply(context)
        } catch (e: IOException) {
            throw PdfGenerationException(e, "Internal error while applying template")
        }
    }
}

private const val languageTag = "languageTag"
//private const val switch ="off"

标签: handlebars.jshandlebarshelperhandlebars.java

解决方案


推荐阅读