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

API for compiling one string template? #20

Open
pwnall opened this issue Jul 1, 2013 · 1 comment
Open

API for compiling one string template? #20

pwnall opened this issue Jul 1, 2013 · 1 comment

Comments

@pwnall
Copy link

pwnall commented Jul 1, 2013

Is there an API that I can use if I want to compile a single Toffee template string into a function that I can call with the context?

This would be useful for integrating Toffee into a system that already has its own caching and partials.

I've been playing with toffee.view and its run, and I'm wondering if that's a good path to follow.

@malgorithms
Copy link
Owner

Hey @pwnall -- make sure you have toffee 0.1.3 and any of these examples should work:

toffee = require 'toffee'

# ----------------------------------
# An example, using a view
# ----------------------------------

v = new toffee.view '''

  Dear #{name},
     Here be some odd numbers: 
       #{(x for x in [1...20] by 2).join ', '}

'''

[err, res] = v.run {name: "Chris"}

console.log res

Note that the view can take an optional second parameter with your autoescape, etc., settings, as mentioned in the documentation. It can also take a "fileName" var which helps with debugging.

Alternatively, for convenience, you can create the view and return its run function in one swoop, with the convenience function compileStr:

# ------------------------------------------------
# An example using the convenience function compileStr
# ------------------------------------------------

fn = toffee.compileStr 'Hi there, #{name}'
[err, res] = fn {name: 'Bob'}

If you want to use your own partial function, you can.

# ------------------------------------------------
# An example, using your own partial function + a view
# ------------------------------------------------

v = new toffee.view 'Hi there, #{name}. Check out #{partial "foo.toffee", {age:212} }'

[err, res] = v.run {
  name: "Batman"
  partial: (fname, vars) -> "\n\nTODO: implement partials so I can print #{fname} / #{vars}"
}

And again, you can use the compileStr convenience function if you prefer:

# ------------------------------------------------
# An example, using your own partial function + compileStr
# ------------------------------------------------

fn          = toffee.compileStr 'Hi there, #{name}. Check out #{partial "foo.toffee", {age:22} }'

[err, res]  = fn {
  name: 'George Bluth'
  partial: (fname, vars) -> "\n\nTODO: implement partials so I can print #{fname} / #{vars}"
}

Let me know if this answers your question...

-Chris

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

2 participants