在 Mustache PHP 中访问迭代字段的值
假设我在 PHP 中有一个数组,如下所示:
$values = Array(
'0' => 'value1',
'1' => 'value2',
'2' => 'value3'
)
我想使用 Mustache 迭代该数组,但我想要关联的值。这就是我希望做的:
{{#values}}
{{the current value}}
{{/values}}
我希望返回的结果是:
value1
value2
value3
我一直通过将结构更改为:
$values = Array(
'0' => array('value=' =>'value1'),
'0' => array('value=' =>'value2'),
'0' => array('value=' =>'value3'),
)
并在 Mustache 迭代器内调用 {{value}} 来解决这个问题。
我应该以完全不同的方式来做这件事吗?我在 PHP 中使用 SplFixedArray,我想使用此方法迭代这些值...
谢谢!
Say I have an array in PHP that looks like so:
$values = Array(
'0' => 'value1',
'1' => 'value2',
'2' => 'value3'
)
I'd like to iterate through the array using Mustache but I'd like the associated value. This is what I'm hoping to do:
{{#values}}
{{the current value}}
{{/values}}
I hope there returned result would be:
value1
value2
value3
I've been getting around this by changing my structure to:
$values = Array(
'0' => array('value=' =>'value1'),
'0' => array('value=' =>'value2'),
'0' => array('value=' =>'value3'),
)
And call {{valule}} inside the Mustache iterator.
Should I be doing this a completely different way? I'm using a SplFixedArray in PHP and I'd like to iterate through the values using this method...
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
隐式迭代器是处理简单数据的方法。如果您的数据比较复杂,那么 PHP 的 ArrayIterator 可以很好地完成这项工作。
这是我正在工作的一个例子。希望它对其他人有用。
在模板中你可以有
The implicit Iterator is the way to go for simple data. If your data is more complex then PHPs ArrayIterator does the job well.
Here is an example that I have working. Hope it is useful for somebody else.
And in the template you could have
您可以为此使用 Mustache 的隐式迭代器功能:
https://github .com/bobthecow/mustache.php/tree/master/examples/implicit_iterator
您的原始数组可能需要数字键,但现在需要字符串。它可能会这样工作,但我还没有测试过。
You can use the implicit iterator feature of mustache for this:
https://github.com/bobthecow/mustache.php/tree/master/examples/implicit_iterator
Your original array probably needs numeric keys and now string though. It might work that way, but I haven't tested it.
我当时正在开发一个超级旧的 php 框架,它使用类似 smarty 的语法,但使用双花括号,让我挂了很长一段时间,所以下面的代码让循环为我运行:)也许它也会对你有帮助。
I was working on a super old php framework, which was using smarty like syntax but double curly braces, kept me hanging for quite sometime, so the following made the loop run for me :) maybe it will help you too.