Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to access tag helpers from a different scope (e.g. inside a helper object instance)? #105

Open
lancejpollard opened this issue Jan 11, 2012 · 1 comment

Comments

@lancejpollard
Copy link

If I do this:

helpers =
  formFor: ->
    form = new Form(arguments…)
    form.render()

class Form
  constructor: ->

  render: ->
    form action: "/", ->
      input type: 'text'

…the form and input methods are undefined.

I see that it has something to do with how the function scopes are declared here:

Since this and global don't include the coffeekup tag helper methods, how do I use them here?

I can see potentially running them through the coffeekup.compile method like this:

class Form
  render: ->
    require('coffeekup').compile ->
      form action: "/", ->
        input type: 'text'

But that seems like overkill. Is there a way to fix/add this?

Thanks!

@lancejpollard
Copy link
Author

I was able to do a workaround by passing in the hidden __ck variable:

helpers =
  formFor: ->
    form = new Form(__ck, arguments…)
    form.render()

class Form
  constructor: (template, options) ->
    @template = template

  tag: (name, args…) ->
    @template.tag name, args

  render: ->
    @tag "form", action: "/", =>
      @tag "input", type: 'text'

There are still 2 problems with this approach:

  1. I can't do template.tag "input", type: "text", I have to do something like template.tag "input", [type: "text"], hence the tag: (name, args…) -> method.
  2. I don't have access to the tag helper variables (h1, h2, etc.). That's cool though, HAML was just like that, they had 1 method, used like @template.haml_tag "input", :type => "text".

Is there a better approach to this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant