Skip to content

Instantly share code, notes, and snippets.

@JamesEggers1
Created March 26, 2013 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamesEggers1/5245252 to your computer and use it in GitHub Desktop.
Save JamesEggers1/5245252 to your computer and use it in GitHub Desktop.
An example of what I'd like to be able to do in Jade.
!!! 5
head
body
//- Currently this is the norm and works find
include path/to/partial
//- This is what I want to be able to do - provide the base directory from the route/rendering data.
include #{dir_from_route}/to/partial
@adunkman
Copy link

Have you considered leaving this sort of logic out of your view and just pre-rendering that section using your controller? That might be a good option, if you don't want to use express-partials like @dustyburwell suggested.

Though, if you're thinking "oh man, why can't I just do != partial(dir_from_route + "/to/partial") then use express-partials fo sho.

@JamesEggers1
Copy link
Author

Trying to figure out how I would push this logic to the server so it's not in the view.

Also express-partials doesn't re-add partial(...) to Jade views, only to EJS. There's a bug because of how the latest version of Jade uses asych rendering now.

@dustyburwell
Copy link

One possibility would be to render the partial yoursel by calling into jade and rendering the resulting string in your view. Alternatively, you may be able to create your own partial method. It would be essentially the same thing, only called from your view.

@dustyburwell
Copy link

If the included content is simple enough, skip the rendering altogether and just add a helper method to return a string of it.

@adunkman
Copy link

How would you do such at the route/middleware layer?

A-like so:

app.use(function (req, res, next) {
  res.locals.renderSomething = function (filename, locals) {
    // Presumably, you'd use the jade module to render the file to a string here

    // Return the rendered string.
    return results;
  };

  next();
});

Then, in your views:

div.renderedStuff= renderSomething("your filename.jade", locals);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment