0

Need some advice to use rebase and/or include.

For building a flexible concept with a variable menu system I need to insert a 'menuY.tpl' template into different 'mainX.tpl' pages.

Sounds easy but not only the pages need

   thisTemplate = template('mainX', keys)

but also the menus need changing menukeys

   thisMenu = template('menuY', menukeys)

How to define the different instructions?

python

@app.route('/doit')
def week():
   ...some code here to load keys etc ...
   thisTemplate = template('mainX', keys)
   return thisTemplate

mainX.tpl with

    <body>
      % insert ('menuY', rv)
      <section class="container">
         <p>{{param1}}</p>
         some html code for the main page
      </section>
   </body>

menuY.tpl with just html code for the menu code like this

   <div id="hambgMenu">
       <a href="/">Home - {{titleY}}</a>
       <a href="/week">{{titleZ}}</a>
   </div>

This will not work, at the mainX.tpl line with % insert python says:

   NameError: name 'insert' is not defined

Also how are the variables (titleY,titleZ) passed to that 'menuY'? There is no reference for 'rv' with the coding above.

neandr
  • 209
  • 1
  • 3
  • 13

1 Answers1

0

The solution was described here How to pass html directly to template ... very easy, just to add ! to the template reference.

I did some further steps just to have on Python:

@app.route('/doit')
def doit():
    page = insertTPL('mainX', keys, 'menuY', menukeys, 'menuTag')
    return page

.. with menuTag as declared as follows:

So mainX.tpl becomes

    <body>
      {{!menuTag}}
      <section class="container">
         <p>{{param1}}</p>
         some html code for the main page
      </section>
   </body>

The mentioned insertTPL python function has:

  def insertTPL(tpl, keys, menu, menukeys, menuTag):
      subtpl = template(menu, menukeys)
      rv = combineKeys(keys, {menuTag:subtpl}) # combine the menu code with other keys! 
      return template(tpl, rv)

  def combineKeys(rv1, rv2):
      try:
          keys = {key: value for (key, value) in (rv1.items() + rv2.items())}
      except:
          keys = rv1
      return keys
Community
  • 1
  • 1
neandr
  • 209
  • 1
  • 3
  • 13