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

Request: Content check blocks #607

Open
jaw-sh opened this issue Jan 14, 2022 · 3 comments
Open

Request: Content check blocks #607

jaw-sh opened this issue Jan 14, 2022 · 3 comments

Comments

@jaw-sh
Copy link

jaw-sh commented Jan 14, 2022

Some templating engines support a streamlined "if content" block.

For instance, in XenForo's templating engine, it looks like this.
image

Take for instance the following scenario which is very common in a template.

<ul class="moderator-actions">
    ​{% if client.can_update_post(post) %}<li><a href="/posts/{{ post.id }}/edit">Edit</a></li>{% endif %}
    ​{% if client.can_delete_post(post) %}<li><a href="/posts/{{ post.id }}/delete">Delete</a></li>{% endif %}
</ul>

We can have many buttons in this list which should only display to the client if they have the ability to use them. The more buttons in this list, the more checks we are using. However, the list itself should only render at all if at least a single item in the list is available.

To achieve this effect, we'd need to add a check like this:

{% if client.can_update_post(post) || if client.can_delete_post(post)  %}
<ul class="moderator-actions">
    ​{% if client.can_update_post(post) %}<li><a href="/posts/{{ post.id }}/edit">Edit</a></li>{% endif %}
    ​{% if client.can_delete_post(post) %}<li><a href="/posts/{{ post.id }}/delete">Delete</a></li>{% endif %}
</ul>
{% endif %}

This is undesirable for two reasons. First, if this list had 10 options, we would need 10 checks, and that is very clumsy to write out. Second, these checks add computational complexity, so needing to run them all twice just for a cosmetic effect is a very poor fix.

A content check could look like this.

{% contentcheck  %}
<ul class="moderator-actions">
    {% content %}
    ​{% if client.can_update_post(post) %}<li><a href="/posts/{{ post.id }}/edit">Edit</a></li>{% endif %}
    ​{% if client.can_delete_post(post) %}<li><a href="/posts/{{ post.id }}/delete">Delete</a></li>{% endif %}
    {% endcontent %}
</ul>
{% endcontentcheck %}

If only whitespace exists within a {% content %} block, then the entire {% contentcheck %} will not render.

This should be nestable. Example: A list may have two categories of options with a header. The category+header does not render unless it has at least one option. The whole list will not render unless at least one category is visible.

@djc
Copy link
Collaborator

djc commented Jan 14, 2022

What Rust code would you expect your template example to compile to?

@Kijewski
Copy link
Collaborator

Please have a look if building a tuple as in this test would work for you. To me it feels much more rust-y and jinja-y than a {% contentcheck %} block.

@jaw-sh
Copy link
Author

jaw-sh commented Jan 28, 2022

I agree, that's an acceptable solution.

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

3 participants