Accessing class lists

32 views
Skip to first unread message

Tschudin Corporate Finance

unread,
Jul 26, 2016, 3:41:53 AM7/26/16
to Pebble Templating Engine
I am using a variable called errors which is a List<Error> to pass form validation errors back to the template after validation. The Error class has two properties: String field and String message.

So under each html form input element, I want to display the appropriate error message, if any. For instance, under the form input element with name="username", I want to display any error.message in errors where error.field == "username".

I currently use a Pebble macro which I include in each form to accomplish this, where for each html form input element I call the getErrors(errors, field) macro and where the field parameter is the name of the html input elemet (e.g. username):

 {% macro getErrors(errors, field) %}
     {% if errors is not empty and errors is iterable %}
         {% for error in errors %}
             {% if error.field == field %}
                 <p class="text-danger error">{{- error.message -}}</p>
             {% endif %}
         {% endfor %}
     {% endif %}
 {% endmacro %}

And while this works fine, it does not seem very efficient nor elegant.

Is there instead a way to do something like the following  under each html input element?

{% if errors.field["username"] is not empty %}
      {{ errors.field["username"].message | first }}
{% endif %}

I have tried the above and some variations of it, but without luck.

Is anything like this possible in Pebble and if not, is it maybe worth adding this feature in future?

Reply all
Reply to author
Forward
0 new messages