玉内部重用功能 [英] Re-using function inside jade

查看:48
本文介绍了玉内部重用功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a inline script and code block repeated 2 times inside a .jade file and would like to:

  • 重复使用它.(我的意思是干燥它并且只有一个块/功能)
  • 像建议的那样转义 html 此处,现在我正在使用 != linkExist('foo')

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

My idea was to use mixin, but don't know how to. My code works as is, but would like to know how to write it better. Thought about codereview (because my code actually works and I just want to improve it) but the jade has not even a tag there yet, so I think SO might be better.

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.......

推荐答案

你应该能够使用 mixin;如果你也传递 result,它应该是非常通用的:

You should be able to use a mixin; if you pass result as well, it should be pretty generic:

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')

这篇关于玉内部重用功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆