How to do complex conditionals or regex in jade

How to do complex conditionals or regex in jade


I’m having an interesting challenge and can’t figure out how to go about solving it. I’m pulling a list of subscribers from my mailchimp and populating a members’ page on my website with the data. One of the field in my sign up for says, “Website or LinkedIn profile url.”

What I’d like to do is, check if they have provided a linkedin address, and if so drop their url in the linkedin member plugin, if not use the other data to create a simpler profile, name website, headline etc.

My problem is I can’t say if data.websiteField.match(/linkedin/) within jade, so I either need to pass the data to some client side javascript which I’m having trouble with or do something else.

Here’s a sample of data returned from mailchimp

[ // this first bit is just the fields
    "email address",
    "first name",
    "last name",
    "areas of interest",
    "please email me about:",
  [ // and here's the first user
    "", // haha, just hiding some data
    "", // hit me up sometime
    "Creativity, Innovation, Ideas and Science",
    "Job Placement, Internships, Recruitment & Retention, Technology & MOOCs, Measurement & Evaluation, Documentation & Dissemination",
    "2013-03-28 19:28:55",
    "2013-03-28 19:29:12",
    "America/New_York", "US", "NY", "2013-03-28 19:29:12"

Any help would rock!

Also, I’m using express. Does express make locals available in client side javascript?

Problem courtesy of: Costa


Client Side Regex

Jade allows you execute arbitrary javascript using the - modifier at the start of the line

- if (profile.websiteField.match(/linkedin/)
    // render linkedin profile here
- else
    // render simple profile here

Server Side Regex

I think it would be simpler to format the profile information server-side and set a renderLinkedIn boolean field to true or false

function displaySignUpPage(req, res) {
  var profile = formatMailChimpData()
  // profile now looks like
  // {
  //   "email address": "",
  //   "first name": "Noah",
  //   ...
  // }

  var linkedInRegex = /linkedin/;
  profile.renderLinkedIn = linkedInRegex.test( // set renderLinkedIn to true or false
  // say your jade view is called signUpPage.jade
  var pageData = {
    title: 'Register',
    profile: profile
  res.render('signUpPage', pageData)

function formatMailChimpData() {
  var mailChimpData = [
      "email address",
      "first name",
      "last name",
      // other fields truncated
      "", // haha, just hiding some data

  // mailChimp puts the keys as the first entry
  var mailChimpFields = mailChimpData[0]
  var mailChimpProfile = mailChimpData[1]
  // make profile into key-value pairs
  var keyValuePairs = (field, index) {
    var profileValue = mailChimpProfile[index]
    var keyValuePair = [field, profileValue]
    return keyValuePair
  var profile = keyValuePairs.reduce(function(prev, pair) {
    var key = pair[0]
    var value = pair[1]
    prev[key] = value
    return prev
  }, {})
  return profile
Solution courtesy of: Noah


Leave a Reply

Your email address will not be published. Required fields are marked *