Node application variables passed to the stylus file

I am building a small node application and using Express with Jade and Stylus to display some basic HTMl pages.

I was curious if there is a way to pass some INTO.styl file variables that are generated from Node? I am well aware that I can define variables inside a .styl file, but I need to be more dynamic. In particular, I was looking for an easy way to save some colors in db, node to capture these values, and then paste them into a .styl file so that when passing the page these variables are passed. It seems like it should be capable, but I lack details. Any help is appreciated. Thank!

+5
source share
2 answers

Stylus API define() Stylus JS.

+6

@ebohlman, , .

, Connect Middleware, :

app.configure (key 'compile), :

app.use(require('stylus')
  .middleware({
    src: app.root + '/app/public',
    compile: compile
  })
);

:

var stylus = require('stylus');
var mylib = function(style){
  style.define('themeColor1', function(){
    //call to the db that returns a color
    color = 'blue';
    color = color ? color : 'orange';
    return new stylus.nodes.Literal(color);
  });
};

var compile = function(str, path) {    
  return stylus(str)
    .use(mylib);
};

.styl :

background-color themeColor1();

themeColor1 . API, , , , , . - - , , .

+6

All Articles