Skip to content

Conditionals within loops #57

@kieranpotts

Description

@kieranpotts

Problem

I would like to be able to use conditionals within loops, where the conditional expression evaluates against another local template variable.

Details

Input:

posthtml([
  /* Some other plugins */
  expressions({
    locals: {
      pages: [
        { path: "/page1", title: "Page 2" },
        { path: "/page2", title: "Page 2" },
        { path: "/page3", title: "Page 3" }
      ],
      current_path: '/page1'
    }
  })
])
/* process(), then(), etc. */
<each loop="page in pages">
    <if condition="page.path === current_path">
        <a class="selected" href="{{ page.path }}">{{ page.title }}</a>
    </if>
    <else>
        <a href="{{ page.path }}">{{ page.title }}</a>
    </else>
</each>

Desired output:

<a class="selected" href="/page1">Page 1</a>
<a href="/page2">Page 2</a>
<a href="/page3">Page 3</a>

Actual output:

ReferenceError: current_path is not defined

Thus the expression page.path === current_path does not evaluate current_path to a template variable.

Is this expected behaviour? If not, could it be considered by the project maintainers as a feature enhancement? (I may be able to contribute the necessary source code changes as a PR.)

Thank you.

Environment

OS Node npm PostHTML
Windows 10 10.15.3 6.4.1 1.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions