【问题标题】:Re-using function inside jade重用玉内部的功能
【发布时间】:2013-11-12 11:21:27
【问题描述】:

我有一个内联脚本和代码块在 .jade 文件中重复了 2 次,我想:

  • 重新使用它。 (我的意思是干它,只有一个块/功能)
  • 按照here的建议转义html,现在我正在使用!= linkExist('foo')

我的想法是使用mixin,但不知道如何使用。我的代码按原样工作,但想知道如何更好地编写它。考虑过codereview(因为我的代码确实有效,我只是想改进它)但是玉石甚至还没有标签,所以我认为这样可能会更好。

h1 Teachers
for result in object.teachers
    - var linkExist = function(i){
    -   if (result[i] != 'undefined'){
    -       var html = ', follow on ' + i + ': <a href="' + result[i] + '" target="_blank">' + result[i].split("http://")[1] + '</a>';
    -       return html;
    -   };
    - }

    section
        h3 #{result.Name} 
        p.inline #{result.Nick}

        img(src=result.img)

        p.small Location: #{result.Location}

        p.small 
            | Web: 
            for webResult in result.Web
                a(href=webResult,target='_blank') #{webResult.split('http://')[1]}

            != linkExist('Twitter')
            != linkExist('GitHub')

//now it repeats the code but for students
h1 Students
for result in object.students
    - var linkExist = function(i){
//etc.......

【问题讨论】:

    标签: node.js pug


    【解决方案1】:

    你应该可以使用 mixin;如果你也通过result,它应该是非常通用的:

    mixin linkExist(result, type)
      if result[type] !== undefined
        | , follow on #{type}: <a href="#{result[type]}">...</a>
    
    //- use like this
    for result in object.teachers
      ...
      mixin linkExist(result, 'Twitter')
      mixin linkExist(result, 'GitHub')
    
    for result in object.students
      ...
      mixin linkExist(result, 'Twitter')
      mixin linkExist(result, 'GitHub')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-10
      • 2012-08-24
      • 1970-01-01
      • 2011-11-02
      • 2021-08-06
      • 1970-01-01
      • 2016-01-06
      • 2013-02-28
      相关资源
      最近更新 更多