Switching feature in Handlebars Templates

265 Views Asked by At

So, we have created many templates using handlebars. Out of the many, we have one handlebar where we would like to make some changes that should only go live after a certain date. To do so, we wanted to create sort of a toggle switch, something like:

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

Below is the generic template parser where I am trying to create a switch that I can inject in the if part of my template. Any suggestions?

/**
     * 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"
0

There are 0 best solutions below