JSP template inheritance

前端 未结 6 861
太阳男子
太阳男子 2020-12-13 14:13

Coming from a background in Django, I often use \"template inheritance\", where multiple templates inherit from a common base. Is there an easy way to do this in JSP? If not

6条回答
  •  心在旅途
    2020-12-13 14:42

    Rythm Template engine has implemented an elegant approach for template inheritance.

    So suppose your layout template (parent template) called main.html:

    @get("title", "default main page")

    @render("leftPanel")
    @render("rightPanel")
    @render()

    And here is your target template:

    @extends(main)
    @set(title: "My Cool Page")
    @section("leftPanel") {
    
    }
    
    @section("rightPanel") {
    
    ...
    } @*** note no "footer" section supplied so the default content will be used **@ @*** the rest is for the main content **@ ...

    Check the real demo at http://rythmengine.com/demo/testdefaultlayoutcontent

    A comprehensive document could be found at http://www.playframework.org/modules/rythm. Though it's targeted to Play!Framework, most of the content also apply to pure rythm engine without Play!Framework.

提交回复
热议问题