使用外部模块继承Jade模板的好策略,在Express上

A good strategy for Jade template inheritance with external modules, on Express

本文关键字:策略 Express 外部 模块 继承 Jade      更新时间:2023-09-26

我正在开发一个框架,开发人员可以在该框架中在外部模块中实现子模板。稍后我想将该模板包含在父模板中。

以下是一个基本的模板模式:

─任务(父模板)
├─成功任务(子模板)
├─失败的任务(子模板)
└─游戏任务(子模板)

每个人的父模板都是一样的。子模板由贡献者实现,并始终显示在不同的时刻(任务状态)。

我在一个模板文件中尝试了两种方法。

实现"块"模式:

block mission_play
  h2 Hello world!
  p Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
block mission_succeed
  h2 Congratulations!
  p You won buddy!
block mission_failed
  h2 Oh my gosh, dude, you did something realy bad
  p You should try again

这个不起作用,因为我没有任何extend layout指令(我不能使用它,因为每个子模板都是外部模块)有没有办法手动向玉石指示哪个模板延伸

在mixin:中实现每个子模板

mixin mission_introduction(mission)
  h2 Hello world!
  p Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
mixin mission_succeed(mission)
  h2 Congratulations!
  p You win buddy!
mixin mission_failed(mission)
  h2 Oh my gosh dude, you did something realy bad
  p You should try again

我本来打算将该模板包含在其父模板中,但Jade不允许在include的路径中使用表达式。这就是为什么我试图直接在母玉模板中解释玉代码,但我没有找到如何解释。我甚至尝试将它包含在partial函数中,但我可以在不禁用Express中默认视图继承策略的情况下使用该模块。

那么我错在哪里了?对于这位建筑之王来说,最好的策略是什么

干杯!

我终于找到了一个好策略:

case mission.state
    when "game"
      block mission_introduction   
        h2 Hello world!
        p Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
    when "succeed"
      block mission_succeed  
        h2 Congratulations!
        p You win buddy!              
    when "failed"
      block mission_failed                                    
        h2 Oh my gosh dude, you did something realy bad
        p You should try again

干杯!