【问题标题】:A good strategy for Jade template inheritance with external modules, on Express在 Express 上使用外部模块继承 Jade 模板的好策略
【发布时间】:2012-08-29 09:48:04
【问题描述】:

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

这是一个基本的模板架构:

─ 任务(父模板)
├─ 任务成功(子模板)
├─ 任务失败(子模板)
└─ 玩任务(子模板)

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

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

实现“块”模式:

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 指令(我不能使用,因为每个子模板都是一个外部模块)。 有什么方法可以手动向jade指明扩展了哪个模板?

在 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 不允许在包含路径中使用表达式。这就是为什么我尝试直接在父玉模板中解释玉代码但我没有找到如何。我什至尝试将它包含在 partial function 中,但我可以使用该模块而不禁用 Express 中的默认视图继承策略。

那么我哪里错了?这位建筑之王的最佳策略是什么?

干杯!

【问题讨论】:

    标签: javascript node.js inheritance express pug


    【解决方案1】:

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

    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
    

    干杯!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-20
      • 1970-01-01
      • 2011-09-20
      • 1970-01-01
      • 2018-07-02
      • 2019-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多