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

two questions actually #2

Closed
mdkocher opened this issue Mar 10, 2013 · 3 comments
Closed

two questions actually #2

mdkocher opened this issue Mar 10, 2013 · 3 comments

Comments

@mdkocher
Copy link

  1. Your comment: "A quick example (which will work again soon):", are you referring to ashes.load throwing this error "TypeError: load() takes exactly 2 arguments (3 given)"? At least that is the behavior I am seeing. "ashes.load('app.dust', 'tmpl')" triggers the error.

  2. Have you integrated ashes into any simple web frameworks such as bottle.py, web.py, etc? I started down the path of writing an adaptor for bottle.py extending the BaseTemplate API. No success so far but so I wanted to check with you on the question above.

FYI, bottle.py template API info: https://bottle.readthedocs.org/en/latest/api.html#templates

Thanks man!
~marc

@mahmoud
Copy link
Owner

mahmoud commented Mar 10, 2013

Two questions, one (longish) answer. First off, yes, that example is broken, and I suppose I don't need to wait for the inevitable big-docs-push to just fix the README. :) Now, for the actual answer: Ashes eliminates the global state that Dust depended on by introducing an AshesEnv, which keeps track of loaded templates and active helpers/escapes, etc.

Technically there is a default env named ashes within the ashes module, that's what's gotten with from ashes import ashes. Environments don't only keep track of loaded templates, but also the fallback behavior for when a template hasn't already been loaded, which usually involves finding a file on the filesystem. The good news is that Ashes comes with a pair of loaders that behave pretty much like how I saw people using dust. (TemplatePathLoader and FlatteningPathLoader)

Still, I couldn't really come up with a very-excellent default loading behavior that would work with all frameworks, so currently the default environment doesn't have any loaders attached, and I may remove this default environment, instead prescribing the following usage:

from ashes import AshesEnv

ashes = AshesEnv(['/path/to/templates', '/other/template/path'])
# also supports preserved whitespace in output: 
# AshesEnv(['/path/to/templates'], keep_whitespace=True)
ashes.render('core_pages/home.html'. {})

or

from ashes import ashes, FlatteningPathLoader

ashes.loaders.append(FlatteningPathLoader('./templates', keep_ext=False))
ashes.render('home', {})

Those both work, but I think the cleanest (if slightly verbose at 4-5 lines) and most powerful, is to create a new environment, create new loaders, optionally calling load_all() to eager-load templates and be notified of errors earlier, and add the loaders to the environment.

All that sounds kind of involved, but it's really just a few lines, and the focus should be on control and state management, since all of this is going to get wrapped up in a tiny integration layer for bottle/web.py/django/clastic/whichever. Options like paths and preserving/culling whitespace would be exposed through the framework's config, and template error presentation could be controlled by the framework, too. A web developer using a framework shouldn't have to see much of ashes directly. And we can all look forward to fewer long answers. :)

@mdkocher
Copy link
Author

Got it. In my opinion the environment route is a fine way to go. "A web developer using a framework shouldn't have to see much of ashes directly.", so that's me, "a web developer" and yeah, I prefer to spend my time writing dust templates and dig the magic rendering my templates.

Thanks again!

BTW:

>>> from ashes import AshesEnv
>>> ashes = AshesEnv(['.'])
>>> ashes.render('app.dust', {"name": "marc"})
u'hi marc!'

@mahmoud
Copy link
Owner

mahmoud commented Mar 11, 2013

Awesome! Keep me posted if you run into anything else. I'll be sure to let you know as I get around to writing integration wrappers for various frameworks.

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