In the couchDB guide, Managing Design Documents, they state that
The _attachments directory contains files that are saved to the Sofa design document as binary attachments. CouchDB serves attachments directly (instead of including them in a JSON wrapper), so this is where we store JavaScript, CSS, and HTML files that the browser will access directly.
then further down they create a separate folder to store their templates, outside the _attachments folder. This is something that's been puzzling me for a while now. They only way I've been able to include templates in my show functions is if they reside outside the _attachments folder.
Can I place my templates in the _attachments folder, or should they be placed outside the _attachments folder?
If I can/should place them inside the _attachments folder, how can I c开发者_如何学Call them in my show functions?
If you want to render the templates server-side (_show
and _list
functions) they must not be in _attachments because show and list functions do not have access to attachments. In these functions, the variable this
is a Javascript object, copied from the design document. this.templates.foo
would come from the templates/foo.html
file in your project.
If you want to render the templates client (browser) side, they may be in attachments, or in the design document. It depends what works better for you. With templates stored in attachments, you can fetch them directly, and use them on the client; you can simply link to it in your app or web page. You can also leave the templates in the design document and let the client fetch that whole document. Then, the client can use whichever templates it wants from the ddoc.
As a general rule, you should put in the _attachments folder any resource you want to serve directly to the client.
The couchapp script does put all else in the design document.
Sofa does server-side rendering of mustache templates. It gets the templates through the design document:
Mustache.to_html(ddoc.templates.index, stash, ddoc.templates.partials, List.send);
You can also choose to serve the templates as attachments, retrieve them through CouchDB's API and render them client-side.
Or you can keep them in the design document, retrieve the design document containing the templates in the client and render the templates client side.
Having Javascript on both client and server, gives you a lot of freedom.
精彩评论