mbr / flask-nav

Easily create navigation for Flask applications.
http://pythonhosted.org/flask-nav/
MIT License
62 stars 37 forks source link

Can you provide example of RawTag? #2

Open aok1425 opened 9 years ago

aok1425 commented 9 years ago

I've tried implementing a RawTag object, but can't figure it out by looking through the source code.

I've tried

but they don't work.

Can you provide an example in the README/demo?

thanks!

mbr commented 9 years ago

That seems to be bug at the moment, it's missing an implementation in the Renderer.

For now, you'll have to subclass the Renderer and add a visit_RawTag method. However, RawTag is, if I remember correctly, not used and just a leftover from when other classes inherited from it. This is not a good state of affairs, I'll have to change the API somewhat, removing raw tag and adding another method to add raw stuff.

In the meantime, maybe sublcassing a Renderer and a NavigationItem (see http://pythonhosted.org/flask-nav/advanced-topics.html#implementing-custom-renderers) is what you need?

albfan commented 4 years ago

FTR:

Trying to add a button on navbar (login from popup, taken from bootstrap examples):

from flask_bootstrap.nav import BootstrapRenderer
from dominate import tags
from dominate.util import raw

class BootstrapRawTagRenderer(BootstrapRenderer):
    def visit_RawTag(self, node):
        return tags.li(raw(node.content))

app = Flask(__name__)
register_renderer(app,'bootstrap_rawtag', BootstrapRawTagRenderer)
...

@nav.navigation() 
def navbar(): 
    return Navbar('My Project', View('About', 'about'), RawTag('<button type="button" id="loginButton" class="btn btn-primary" data-toggle="modal" data-target="#loginModal">Login</button>'))

In your templates:

{% block navbar %}
{{ nav.navbar.render(renderer='bootstrap_rawtag') }}
{% endblock %}

{% block content %}
...
<div class="modal fade" id="loginModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
        <h5 class="modal-title" id="exampleModalLabel">Login</h5>
      </div>
      <div class="modal-body">
        <form method="post" action='/login/' name="login_form">
          <p><input type="text" class="span3" name="eid" id="email" placeholder="Email"></p>
          <p><input type="password" class="span3" name="passwd" placeholder="Password"></p>
          <p><button type="submit" class="btn btn-primary">Sign in</button>
            <a href="#">Forget the password?</a>
          </p>
        </form>
      </div>
      <div class="modal-footer">
        Do you want to register?
        <a href="#" class="btn btn-primary">Register</a>
      </div>
    </div>
  </div>
</div>

{% endblock %}

@mbr I understand that allow this dominate.util.raw() is scary, but wrap everything from flask_nav seems to big to not consider this RawTag.

Let me know if you consider to accept a PR related to that