【问题标题】:bottle › template with rebase and/or include瓶 › 带有变基和/或包含的模板
【发布时间】:2017-02-13 11:29:03
【问题描述】:

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

为了使用可变菜单系统构建一个灵活的概念,我需要将 'menuY.tpl' 模板插入到不同的 'mainX.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”的引用。

【问题讨论】:

  • 你把include错拼成insert不就是你的错吗?

标签: python templates bottle


【解决方案1】:

这里描述了解决方案How to pass html directly to template ...非常简单,只需添加!到模板参考。

为了使用 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>

提到的insertTPLpython函数有:

  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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多