在手柄中使用#each循环时访问先前的索引值

时间:2018-03-14 02:43:42

标签: handlebars.js handlebarshelper

在我的车把应用程序中,我正在尝试遍历一系列帖子。我正在尝试访问每个帖子的ID。我正确地获得当前帖子的id。但是,或者每个图像,我需要提供上一个和下一个ID。我使用数学助手将索引递增/递减1,它工作正常。

我的想法是我将使用handlebars子表达式来计算所需的索引,然后我将使用查找助手来访问该索引处的值。但我的代码没有按预期工作。有人可以帮我解决这个问题吗?

{{#each posts}}
    <div class="lb-overlay" id="image-{{id}}">
        <a href="#image-{{lookup id (math @index '-' 1)}}" class="lb-prev">Prev</a>
        <a href="#image-{{lookup id (math @index '+' 1)}}" class="lb-next">Next</a>
    </div> 
{{/each}}

更新:我自己想出来了。实施了我自己的车把助手。

{{#each posts}}     
    <div class="lb-overlay" id="image-{{id}}">       
        <a href="#image-{{getIdByIndex ../posts (math @index '-' 1)}}" class="lb-prev">Prev</a>       
        a href="#image-{{getIdByIndex ../posts (math @index '+' 1)}}" class="lb-next">Next   
    </div> 
{{/each}}

1 个答案:

答案 0 :(得分:0)

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/pip/_vendor/__init__.py", line 33, in vendored
    __import__(vendored_name, globals(), locals(), level=0)
ImportError: No module named 'pip._vendor.pkg_resources'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/bin/pip3", line 9, in <module>
    from pip import main
  File "/usr/lib/python3/dist-packages/pip/__init__.py", line 13, in <module>
    from pip.exceptions import InstallationError, CommandError, PipError
  File "/usr/lib/python3/dist-packages/pip/exceptions.py", line 6, in <module>
    from pip._vendor.six import iteritems
  File "/usr/lib/python3/dist-packages/pip/_vendor/__init__.py", line 75, in <module>
    vendored("pkg_resources")
  File "/usr/lib/python3/dist-packages/pip/_vendor/__init__.py", line 36, in vendored
    __import__(modulename, globals(), locals(), level=0)
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 634, in _load_backward_compatible
  File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 2927, in <module>
  File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 2913, in _call_aside
  File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 2952, in _initialize_master_working_set
  File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 956, in subscribe
  File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 2952, in <lambda>
  File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 2515, in activate
  File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 2097, in declare_namespace
  File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 2047, in _handle_ns
  File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 2066, in _rebuild_mod_path
AttributeError: '_NamespacePath' object has no attribute 'sort'.