Textarea not displaying placeholder in Python and Flask

Question:

I have a Python variable being returned in a textarea, however, when the variable returns nothing none is displayed instead of reverting back to the placeholder text.

Examples:

<textarea placeholder="some text">{{ variable }}</textarea>

Attempt:

<textarea placeholder="some text">
 {% if result %}
   {{ variable }}
 {% endif %}
</textarea>
Asked By: Empty

||

Answers:

If you want the placeholder text to be displayed when the result variable is empty or not defined, you can use the else statement in your Jinja2 code. Here is an example of how you can modify your code to achieve this:

<textarea placeholder="some text">
 {% if result %}
   {{ result }}
 {% else %}
   {{ placeholder }}
 {% endif %}
</textarea>

In this example, if the result variable is defined and not empty, it will be displayed in the textarea. If the result variable is not defined or is empty, the placeholder text will be displayed instead.

Answered By: linus
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.