CouchDB视图或设计文档中显示?
我目前正在为 CouchDB 的视图和显示功能之间的差异而苦苦挣扎。我正在使用 CodeIgniter 通过curl 查询CouchDB。我的 _design 文档中没有附加模板。每个模板都通过 CI 的视图方法以纯 HTML 形式加载。在 Mustache.js 的帮助下,我希望能够使用 Mustache 的伪变量。
正如我从文档中了解到的,视图结果始终以键:值对的形式呈现。
假设我从 http://127.0.0.1:5984/blog 得到以下结果/_design/entries/_view/all
{"total_rows":2,"offset":0,"rows":[
{"key":"56a13b96ea3492ef8c7e554f67000952","value":{title:"First post"}},
{"key":"56a13b96ea3492ef8c7e554f670015a9","value":{title:"Second post"}}
]}
按照 Mustache 的方法,我将能够做这样的事情:
{{#rows}}
<div class="entry">
<h1>{{#value}}{{title}}{{/value}}</h1>
</div>
{{/rows}}
这是可以接受的,但我宁愿做这样的事情:
{{#entries}}
<div class="entry">
<h1>{{title}}</h1>
</div>
{{/entries}}
我如何能够做这个吗?视图函数中是否需要更改某些内容,或者这根本不能通过视图函数来完成?我这里需要显示功能吗?如果是,创建一个最简单的方法是什么?问题、问题、问题……
I am currently struggling a bit with CouchDB's difference between view and show functions. I am using CodeIgniter to query the CouchDB via curl. I have no attached templates in _design documents. Each template is loaded via CI's view method as plain HTML. With the help of Mustache.js I want to be able to make use of Mustache's pseudo-variables.
As I understood from the documentation, view results are always presented in key:value pairs.
Let's say I have the following result from http://127.0.0.1:5984/blog/_design/entries/_view/all
{"total_rows":2,"offset":0,"rows":[
{"key":"56a13b96ea3492ef8c7e554f67000952","value":{title:"First post"}},
{"key":"56a13b96ea3492ef8c7e554f670015a9","value":{title:"Second post"}}
]}
Following Mustache's methodology I would be able to do something like this:
{{#rows}}
<div class="entry">
<h1>{{#value}}{{title}}{{/value}}</h1>
</div>
{{/rows}}
This is acceptable but I would rather do something like this:
{{#entries}}
<div class="entry">
<h1>{{title}}</h1>
</div>
{{/entries}}
How am I able to do this? Is there something that needs to be altered in the view function or can this simply not be done through a view function? Do I need show functions here? If yes, what is the easiest way to create one? Questions, questions, questions...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确的话,您希望能够从 CodeIgniter 调用 CouchDB 并接收 HTML 作为响应而不是 JSON。首先,我不确定您为什么要这样做,因为您已经拥有 PHP 以及与之相关的所有内容供您使用。 CouchDB 中的模板对于直接从其提供服务的应用程序来说非常有意义。
无论如何,如果这就是您想要的,那么您需要的是
list
函数。您可以使用_design//_list//?vew_params
调用列表函数。这样list
函数将接收您的视图结果并以任何方式转换它。此外,如果您想用胡子格式化它,手动执行此操作并不是很简单,通常需要在 CouchApp 的帮助下完成。原因是,从列表(也不是显示/映射/减少)函数中,您无法调用其他地方定义的其他函数。因此,您的列表函数必须包含整个 Mustache.js 库和模板,以便它可以调用它。
If I understand correctly, you want to be able to call CouchDB from CodeIgniter and receive HTML as response instead of JSON. First I'm not sure why you want to do that, because you already have PHP and all that goes with it at your disposal. Templating within CouchDB mostly has sense with applications served directly from it.
Anyway if that's what you want, what you need is a
list
function. You can call a list function with_design/<doc>/_list/<listname>/<viewname>?vew_params
. That waylist
function will receive a result of your view and transform it in whatever way.Further if you want to format it with mustache, doing so by hand isn't quite straightforward and is usually done with help of CouchApp. Reason is that from list (nor show/map/reduce) function you can't call other functions defined elsewhere. So your list function must include entire mustache.js library and templates so that it could call it.