visionmedia/jade — Issue #375: Support for Jade Tag Helpers like Haml#haml_tag?
Author: viatropos
Created: 2011-10-16 23:51:44+00:00
State: open

Hey,

In Haml (ruby), I can create tags in helper methods like this:

``` ruby
module CustomHelpers
def meta_tag(name, content)
capture_haml do
haml_tag :meta, :name => name, :content => content
end
end
end
```

``` haml
!!! 5
%html
%head
%title Haml
= meta_tag "description", "haml example"
```

Is there a way to do this in Jade?

So far, from looking here https://github.com/visionmedia/jade/blob/master/lib/jade.js#L95, I have been able to do this:

``` haml
!!! 5
html
head
title Jade
- var func = function() { console.log(buf); buf.push(""); }
!= func()
```

That's definitely not a public api or anything, but it could be made more robust so you could create something similar to the `haml_tag` helpers.

Maybe the compiled javascript template is executed inside of a Template class instead of the anonymous function. Then we can call methods that output javascript code into the buffer, something like:

``` coffeescript
class Template
jade_tag: (name, attrs) ->
@compiler.visitTag(new Tag(name, attrs))
```

``` haml
!!! 5
html
head
title Jade
!= jade_tag("meta", name: "description", content: "jade example")
//- or if it output directly into the buffer
- jade_tag("meta", name: "description", content: "jade example")
```

What are your thoughts?