jinja2 变量 ajax,javascript中的jinja2变量(jinja2 variables in javascript)

javascript中的jinja2变量(jinja2 variables in javascript)

例如,如果我在javascript中编写了jinja2变量

var array = [{{count}}...

它工作,即使我将代码移动到一个单独的js文件,它会工作吗? 关于这种做法还有什么我需要知道的吗?

If I have written jinja2 variables in javascript, for example

var array = [{{count}}...

and it works, will it work even if I move the code to a separate js file? Is there anything else I need to know about this practice?

原文:https://stackoverflow.com/questions/43765758

更新时间:2020-02-22 23:01

最满意答案

您当然可以创建一个Jinja2模板,其中包含带有Jinja2变量的Javascript,将其呈现为JavaScript文件,并将其提供给您的用户。 Jinja2并不关心你渲染的文件类型。

一个重要的考虑因素是您正在将静态文件更改为动态文件。 典型的Javascript文件是静态的,但您现在将其设置为动态,这会给您的服务器带来额外的负担。

典型的解决方案是使用静态JavaScript,但将JavaScript数据呈现到JavaScript文件可以访问的HTML页面中。

You can certainly create a Jinja2 template that contains Javascript with Jinja2 variables, render that into a JavaScript file, and serve it to your users. Jinja2 doesn't care what kind of file you are rendering.

An important consideration is that you are changing a static file to a dynamic file. A typical Javascript file is static but you are now making it dynamic which puts additional load on your servers.

A typical solution is to use static JavaScript but render JavaScript data into your HTML page that the JavaScript file can access.

相关问答

我不明白你的问题。 当你将变量传递给上下文时(就像你试图做的那样),这些变量将在子和父级中可用。 要将标题传递给父项,您必须使用继承,有时需要与super结合使用: http : //jinja.pocoo.org/docs/templates/#super-blocks 另请参阅此问题: 覆盖if中的应用程序引擎模板块 I do not understand your problem. When you pass variables to the context (as you do with

...

我发现这个包可以帮助你:jinja2schema from jinja2schema import infer, model

def test(self):

for key, val in infer(TEMPLATE).items():

assert key in self.data, 'Missing {}'.format(key)

if type(val) == model.List:

for subkey in val.it

...

以下是完整的答案: var creationSuccess = function (data) {

console.log('Card created successfully.');

var http = new XMLHttpRequest();

var url = "URL to your server (where you want to send the data)";

http.open("POST", url, true);

http.setRequestH

...

一种可能性是在服务器端创建包含变量的dict或list 。 然后,您可以将该对象作为模板变量发送到Jinja。 就目前而言,您只需将import_name设置import_name等于string,该字符串将不具有.created_at属性。 One possibility is to create a dict or a list on the server-side which contains your variables. You can then send that object to

...

使用vars查找插件来引用变量(而不是遍历名称中的字符,就像现在这样)。 你的内部循环应该是: {% for index in lookup('vars', prefix) %}

另外,请检查如何控制Jinja2中的空白 ,因为使用当前代码获得的输出将被奇怪地缩进。 Use the vars lookup plugin to refer to variables (instead of iterating over the characters in their names, as you do

...

我找不到这样做的方法,最后只是每次都传递默认参数: macro.jinja2 {% macro do_stuff(something, something_else, value) %}

Something is {{ something | default('nothing') }}.

Something else is {{ something_else | default ('nothing') }}.

Values is {{ va

...

使用json模块将Python数据转换为JSON数据; JSON是JavaScript *的一个子集,可以作为JavaScript文字使用: import json

js_value = json.dumps(python_value)

并在模板中渲染js_value 。 如果您还需要JSON数据也是HTML安全的 ,那么您需要添加一些替换: js_value = (json.dumps(python_value)

.replace(u'

.rep

...

Jinja2是一种模板语言。 这意味着在提供给客户端之前,所有模板表达式都将被评估并替换为文本或HTML代码。 而Javascript是客户端使用的脚本语言。 因此实际上没有办法将任何值传递给Javascript中的Jinja2表达式,因为客户端上根本不存在任何Jinja2表达式,因为它们已经全部被文本或html代码替换。 但是,如果您只是想将任何数据从客户端传递到服务器,那么有很多方法可以做到这一点。 可能最合适的是Ajax调用。 Jinja2 is a templating language.

...

您当然可以创建一个Jinja2模板,其中包含带有Jinja2变量的Javascript,将其呈现为JavaScript文件,并将其提供给您的用户。 Jinja2并不关心你渲染的文件类型。 一个重要的考虑因素是您正在将静态文件更改为动态文件。 典型的Javascript文件是静态的,但您现在将其设置为动态,这会给您的服务器带来额外的负担。 典型的解决方案是使用静态JavaScript,但将JavaScript数据呈现到JavaScript文件可以访问的HTML页面中。 You can certain

...

只需将字符串更改为不包含变量的逐字名称,然后执行一些字符串追加。 例如: {{ [

variable1 + " and other text ",

variable2 + " and other text"

] | random }}

您已经在模板部分中,因此您无需转义变量声明。 Just change your strings to not include the verbatim name of the variable and do some string appending instea

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值