Node.js jade template - custom syntax and replacements?

I'm trying to get jade to automatically generate urls for me:

therefore, if I have a link to the user, like this:

.userLink
  a(href="/#{user}") #{user}

I want to replace it with something like (hypothetical syntax):

.userLink
  userLink(#{user})

Anyway to do this?

thank

Edit: OKAY I got it:

mixin userLink(user)
  a(href="/" + user)= user

mixin userLink("Bob")

It seems to work.

+5
source share
1 answer

UPDATE:

The version using jade mixins is used here. Unfortunately, I can not find the variable "user" in the list of attributes in the brackets of the tag "a".

mixin userLink(user)
  a(href="/" + user)= user

mixin userLink("Bob")
mixin userLink("Alice")
mixin userLink("Cooper")

Creates this HTML

<a href="/undefined"></a> Bob
<a href="/undefined"></a> Alice
<a href="/undefined"></a> Cooper

Just put the helper function in your locals object and call it from the template

locals = {userLink: function(userName) { return "<a href=....."}}

jade.render('myview', {locals: locals})

:

= userLink(user)

, dynamicHelper.

+3

All Articles