Skip to content

Compile mixins for client #1950

Open
Open
@mikeyhew

Description

@mikeyhew

It would be nice if you could write mixins for use on the server, and compile them for use on the client:

//- tweets.jade

mixin tweet(tweet)
  li
    h3= tweet.handle
      small= tweet.time
    p= tweet.text

h1 Tweets
ul#tweets
each tweet in tweets
  +tweet(tweet)

And then on the client, when a new tweet comes in, you do:

new_tweets.forEach(function(tweet) {
  $('ul#tweets').prepend(tweet_mixin(tweet));
})

You can do this by moving the tweet template to _tweet.jade, and writing include _tweet.jade instead of +tweet(tweet), but there are some issues with this approach:

  1. Each mixin that you want to be able to access on the client has to be put in a separate file. It would be nice if you could have multiple mixins in a mixins.jade file, and be able to send those mixins to the client.
  2. If the mixin has more than one argument, then it gets tricky - this example worked nicely because the tweet mixin takes a single object as its only argument; but what about a mixin that takes a single number, or if the tweet mixin looked like +tweet(tweet_data, time)?
  3. Usually you design the site with HTML (or jade) and CSS first, and then implement functionality second. When you're writing the template, you'll naturally want to use the mixin syntax. But then you'll have to go and refactor the code later on, moving the mixins you want on the client to a separate file and including them.

I propose something along the lines of compileMixinsClient(mixin_names, templateString) and compileMixinsFileClient(mixin_names, templateFileName), that return a string of javascript declaring a function for each mixin in mixin_names:

function mixinName1 (locals, arg1, arg2) {...}
function mixinName2 (locals, arg1) {...}

// or what would be even better - a curried function that you can use like this:
function mixinName1 (locals) {
  return function (arg1, arg2) {...}
}
var mixinName1WithLocals = mixinName1(locals);
$('#my-el').html(mixinName1WithLocals(arg1, arg2));

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions