Unable to display comment tree with twig macro

51 Views Asked by At

I am trying to render comment tree using twig macro but keep getting following error:

"Neither the property "comments" nor one of the methods "comments()", "getcomments()"/"iscomments()"/"hascomments()" or "__call()" exist and have public access in class "Doctrine\ORM\PersistentCollection"."

If there is additional info needed, I am at disposal.

Br,

{% macro render_comment(comment) %}
    <li id="comment-{{ comment.id }}">
        <span>
            {{ comment.author.fullName }}
            {{ 'post.commented_on'|trans }}
            {{ comment.publishedAt|format_datetime('medium', 'short', '', 'UTC') }}
        </span>
        {{ comment.content|markdown_to_html|sanitize_html }}

        <a href="#comment_content" data-reply data-id="{{ comment.id }}">
            {{ 'action.submit_reply'|trans }}
        </a>

        {% if comment.children is not empty %}
            <ul>
                {% for child in comment.children %}
                    {{ _self.render_comment(child) }}
                {% endfor %}
            </ul>
        {% endif %}
    </li>
{% endmacro %}

{% macro render_comment_list(comments) %}
    {% for comment in comments %}
        {% if comment.parent is null %}
            {{ _self.render_comment(comment) }}
        {% endif %}
    {% else %}
        {{ 'post.no_comments'|trans }}
    {% endfor %}
{% endmacro %}
1

There are 1 best solutions below

3
Lajos Arpad On

You need to pass comments too:

{{ render_comment_list(post, comments) }}

and make sure that when you render this template, you expose comments properly so it can reach them.

EDIT

As per the comment section

{{ render_comment_list(post.comments) }}

was the solution.