首页 \ 问答 \ 主Jinja模板中的逻辑(Logic in Primary Jinja Template)

主Jinja模板中的逻辑(Logic in Primary Jinja Template)

在我的app.py文件中,我将导航存储在dict中。 键是主导航键,值是子菜单。

navigation = {"Search": ["Google","Yahoo","Bing"]}

在我的layout.html模板(我的主模板文件)中,我想调用dict来生成导航。 对我而言,这样做是有意义的,因为它将贯穿每一页。 但是,对于我所拥有的每个视图,我都必须定义“导航”。 这似乎是多余的和不必要的。

这引出了我的问题,构建网站的适当方式是什么。 我不想硬编码每个链接。 如果我决定更改应用程序提供的内容,我希望更改为跨站点级联到我正在使用导航词典的所有其他区域。

结构示例:

#app.py
navigation = {"Search": ["Google","Yahoo","Bing"]}
def my_view():
    return render_template('my_view.html')

#layout.html
{% for link in navigation %}
    {{ link }}
{% endfor %}

要解决我的问题,我只需要将全局导航添加到视图中。

return render_template('my_view.html', navigation=navigation)

我不想为每个视图添加导航。 感觉多余,尤其是当您可以拥有数十个视图时。


In my app.py file, I've stored my navigation in a dict. The keys being the main navigation and the values being the submenus.

navigation = {"Search": ["Google","Yahoo","Bing"]}

In my layout.html template (my primary template file) I want to call the dict to generate my nav. To me it makes sense to do this since it will be persistent across every page. However, for every view I have I will have to define "navigation". Which seems redundant and unecessary.

That leads me to my question, what is the appropriate way to structure the site. I don't want to hardcode every link. If I decide to change what the app offers, I would like the change to cascade across the site to all the other areas I'm using the navigation dict.

Example of the structure:

#app.py
navigation = {"Search": ["Google","Yahoo","Bing"]}
def my_view():
    return render_template('my_view.html')

#layout.html
{% for link in navigation %}
    {{ link }}
{% endfor %}

To resolve my issue I just have to add the navigation global to the view.

return render_template('my_view.html', navigation=navigation)

I don't want to add navigation to every view. It feels redundant especially when you could have dozens of views.


原文:
更新时间:2020-07-02 13:07

最满意答案

您的函数invalid definition 。 在()参数后面有extra ()

function calculateTotal(savingsGoal.value, initialInvestment.value,
annualContribution.value, interstRate.value)(){
                                          //^^remove this

更新

我刚才注意到的几点在你的情况下仍然无效。 所以,请fiddle这个fiddle显示的代码。 我已经更新了htmljs 。 我已经改变了使值function的逻辑..现在它的参数更少 。 此外, getIntrestRate()是一个函数,您尝试将其作为变量/元素访问,并且您调用它的方式是错误的。 有一个拼写错误,即getInterestRate 。 只是通过小提琴,请注意它没有完全functional 。 不知道最后你在intr变量中得到了什么。


Your function has invalid definition. You have extra () after parameters inside ()

function calculateTotal(savingsGoal.value, initialInvestment.value,
annualContribution.value, interstRate.value)(){
                                          //^^remove this

Update

Few points I just noted which stays invalid in your case. So just go through the code shown in this fiddle. I've updated both html and js. I've changed the logic of getting values to function.. Its a parameter less function now. Also getIntrestRate() is a function and you were trying to access it as a variable/element and the way you were calling it was wrong. There is a spelling mistake i.e. getInterestRate. Just go through the fiddle and please note that its not fully functional. Not sure what you are getting in intr variable at the end.

相关问答

更多
  • src是script标签的属性,因此: src is attribute of script tag, so:
  • 将字符串传递给setTimeout ,将在全局范围内对其进行求值。 由于getDetails是在另一个函数(至少在你的JSFiddle链接上)中定义的,它被称为onload ,因此在评估字符串时它超出了范围。 您需要将函数传递给setTimeout 。 这将创建一个闭包并保留范围。 function delayed_function() { getDetails(services[nextAddress].place_id, theNext); } setTimeout(delayed_funct ...
  • 您在第一个