0

I have a main layout template on flask/jinja to use with base.

I need injnect dynamic menu content in this layout.html by a call python function.

Is possible do it with jinha/flask ?

this is my layout.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="">
  <meta name="author" content="">
  <title>Simple Sidebar</title>
</head>
<body>
  <div class="d-flex" id="wrapper">
    <!-- Sidebar -->
    <div class="bg-light border-right" id="sidebar-wrapper">
        <div class="sidebar-heading">Start Bootstrap </div>
        <div class="list-group list-group-flush">
            {% block sidebar_content %}
            ------HERE-------
            {% endblock sidebar_content %}
        </div>
      </div>
   </div>
</body>
</html>

this my sidebar.html where the jinja inject by sidebar_content tag;


 <div class="list-group list-group-flush">
{% block sidebar_content %}

{% endblock sidebar_content %}
</div>

Python method:

def render_sidedbar():
    sidebar = """
      <a href="#" class="list-group-item list-group-item-action bg-light">Overview</a>
      <a href="#" class="list-group-item list-group-item-action bg-light">Events</a>
      <a href="#" class="list-group-item list-group-item-action bg-light">Profile</a>
       """

    return sidebar

can help me? Thanks

dmrpy
  • 59
  • 1
  • 8
  • I just [answered](https://stackoverflow.com/a/60920489/2052575) another question which is pretty similar to this. To inject the linkbar into every page, use a context processor as demonstrated in the linked gist. – v25 Mar 29 '20 at 22:49
  • i will try use the context processor.. thnks – dmrpy Mar 30 '20 at 11:53
  • app_context not works for me. this works better and i'm try use this example - https://code.tutsplus.com/tutorials/templating-with-jinja2-in-flask-advanced--cms-25794 – dmrpy Mar 30 '20 at 12:27

1 Answers1

0

You'll probably want to use a jinja2 macro, documentation here.

You can dynamically create things functionally that way. They can be a pain to write, but they very much can act as simple web components.

If you need it to be injected after the page is rendered, you'll likely need to implement and api with javascript.

scoofy
  • 103
  • 8