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

Trying to find a way to override the title of flash message with custom message #14

Closed
Shivam0609 opened this issue Aug 23, 2022 · 6 comments

Comments

@Shivam0609
Copy link

Current Behaviour :
Getting category displayed on flash message like Info, Success, Warning, Message etc..

image

Required Behaviour :
Instead of Category want to see a Custom message as below.

image

If their is some configuration paramter to do that please confirm ?

@Shivam0609
Copy link
Author

Seems like it is not supported as of now

@Shivam0609
Copy link
Author

Shivam0609 commented Aug 23, 2022

Just need this minor change

message = Template('''
{% with messages = get_flashed_messages(with_categories=true) %}
  {% if messages %}
    <script type="text/javascript">
      (function($) {
        $(document).ready(function() {
          {{ toastr_options }}
          {% for category, message in messages %}
            {% if category is undefined or category == 'message' %}
              toastr.info(\'{{ message | replace("'","\\\\'") }}\', \'{{ category|capitalize }}\')
            {% elif category and category not in ['message', 'error', 'warning', 'info', 'success']  %}
              toastr.info(\'{{ message | replace("'","\\\\'") }}\', \'{{ category|capitalize }}\')
            {% else %}
              toastr.{{ category }}(\'{{ message | replace("'","\\\\'") }}\', \'{{ category|capitalize }}\')
            {% endif %}
          {% endfor %}
        });
      })(jQuery);
    </script>
  {% endif %}
{% endwith %}
''')

And to invoke

flash("All OK","test header")

@wiltonsr
Copy link
Owner

Hi, @Shivam0609

Thanks for the interest in Flask-Toastr.

You are right, for now, Flask-Toastr doesn't support custom titles.

Just need this minor change

message = Template('''
{% with messages = get_flashed_messages(with_categories=true) %}
  {% if messages %}
    <script type="text/javascript">
      (function($) {
        $(document).ready(function() {
          {{ toastr_options }}
          {% for category, message in messages %}
            {% if category is undefined or category == 'message' %}
              toastr.info(\'{{ message | replace("'","\\\\'") }}\', \'{{ category|capitalize }}\')
            {% elif category and category not in ['message', 'error', 'warning', 'info', 'success']  %}
              toastr.info(\'{{ message | replace("'","\\\\'") }}\', \'{{ category|capitalize }}\')
            {% else %}
              toastr.{{ category }}(\'{{ message | replace("'","\\\\'") }}\', \'{{ category|capitalize }}\')
            {% endif %}
          {% endfor %}
        });
      })(jQuery);
    </script>
  {% endif %}
{% endwith %}
''')

And to invoke

flash("All OK","test header")

This way all messages will be categorized as info.

I was thinking about:

flash({'title': "Custom Title", 'message': "All OK"}, 'success')

And check if is a mapping:

{% with messages = get_flashed_messages(with_categories=true) %}                
  {% if messages %}                                                             
    <script type="text/javascript">                                             
      (function($) {                                                            
        $(document).ready(function() {                                          
          {{ toastr_options }}                                                  
          {% for category, message in messages %}                               
            {% if category is undefined or category == 'message' %}             
              {% set category = 'info' %}                                       
            {% endif %}                                                         
            {% if message is mapping %}                                                                                                                           
              toastr.{{ category }}(\'{{ message['message'] | replace("'","\\\\'") }}\', \'{{ message['title'] | replace("'","\\\\'") }}\')
            {% else %}                                                          
              toastr.{{ category }}(\'{{ message | replace("'","\\\\'") }}\', \'{{ category|capitalize }}\')
            {% endif %}                                                         
          {% endfor %}                                                          
        });                                                                     
      })(jQuery);                                                               
    </script>                                                                   
  {% endif %}                                                                   
{% endwith %} 

@Shivam0609
Copy link
Author

Above solutions looks Fine to me aswell and is more general

@Shivam0609
Copy link
Author

Hi @wiltonsr ,

Hope to get this implemented soon, That will be Great.

Thanks

@wiltonsr
Copy link
Owner

wiltonsr commented Aug 23, 2022

@Shivam0609

Version 0.5.8 already available.

I used your solution and mine, so you could use:

@app.route('/')
def index():
    flash("Message", 'Custom Title') # Will be info category by default
    flash({'title': "Custom Title", 'message': "Info Category"}) # Will also be info category by default
    flash({'title': "Custom Title", 'message': "Error Message"}, 'error') # Or use dict syntax to pass a custom category
    return render_template('index.html')

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