Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make {% if %} tag able to perform comparisons #20

Open
funkybob opened this issue May 12, 2019 · 4 comments
Open

Make {% if %} tag able to perform comparisons #20

funkybob opened this issue May 12, 2019 · 4 comments

Comments

@funkybob
Copy link
Owner

Currently {% if %} can only do "if " or "if not "

It would obviously be far more powerful to be able to express other comparisons, against full expressions.

@funkybob
Copy link
Owner Author

I've a branch, now, that uses python's built in expression parsing to handle all expressions.

This makes ALL tags far more flexible. However, it presents a large stumbling block: the handling of {% include %}

Currently since we control the parsing of tokens, we can parse a single expression from the token stream, then parse a series of name=expre sequences.

I can't yet see a way to provide this same functionality without recreating the full expression parsing abilities of Python anyway.

@Alysha-94
Copy link

Hey is there a workaround for this? I'm using stencil with Gilbert and trying to display certain team cards if they match the author/s of a post

For example I have a BlogPost and TeamMember class in plugins.py:

class BlogPost(Templated, Content):
    authors: list
    template: str = "blog/post_detail.html"

class TeamMember(Templated, Content):
    name: str
    template: str="team/team_detail.html"

Then in the post_detail template I pass each author of the post with all the team members to team_list template:

{% for author in this.authors %}
{% include "team/team_list.html" author=author, members=site.content["team.yaml"].pages %}
{% endfor %}

Now in team_list I want to display the team information only if the member.name matches the author:

{% for member in members %}
{% if member.name == author %}
<div>display team member details</div>
{% endfor %}

@funkybob
Copy link
Owner Author

funkybob commented Jul 26, 2019

A dirty hack might be to use {% if author.__contains__(member.name) %}.

>>> from stencil import Template
>>> t = Template("""{% if authors.__contains__(user) %}Woo{% endif %}""")
>>> t.render({"authors": ['one', 'two', 'four'], "user": "foo"})
''
>>> t.render({"authors": ['one', 'two', 'four'], "user": "one"})
'Woo'

@Alysha-94
Copy link

A dirty hack might be to use {% if author.__contains__(member.name) %}.

Ah clever I think that will be fine to use, thanks for providing an example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants