当前位置: 代码迷 >> python >> 瓶?具有变基和/或包括的模板
  详细解决方案

瓶?具有变基和/或包括的模板

热度:29   发布时间:2023-06-13 16:51:01.0

需要一些建议以使用rebase和/或include。

为了使用可变菜单系统构建灵活的概念,我需要在不同的“ mainX.tpl”页面中插入“ menuY.tpl”模板。

听起来很简单,但不仅页面需要

   thisTemplate = template('mainX', keys)

但菜单也需要更改菜单键

   thisMenu = template('menuY', menukeys)

如何定义不同的指令?

蟒蛇

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

mainX.tpl

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

menuY.tpl仅带有html代码,例如菜单代码

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

这将无法正常工作,在mainX.tpl行中使用%insert python表示:

   NameError: name 'insert' is not defined

还有如何将变量 (titleY,titleZ)传递给该“ menuY ”? 上面的编码没有引用“ rv”。

这里描述了解决方案, ...非常简单,只需添加即可! 模板参考。

我在Python上做了一些进一步的步骤:

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

..与menuTag声明如下:

因此mainX.tpl成为

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

提到的insertTPL python函数具有:

  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
  相关解决方案