【问题标题】:Is it possible to use jinja values in javascript/jquery functions是否可以在 javascript/jquery 函数中使用 jinja 值
【发布时间】:2013-02-01 19:58:06
【问题描述】:

查看 google web eng 示例;

{% for greeting in greetings %}
  {% if greeting.author %}
    <b>{{ greeting.author }}</b> wrote:
  {% else %}
    An anonymous person wrote:
  {% endif %}
  <blockquote>{{ greeting.content|escape }}</blockquote>
{% endfor %}

所以我想要的是:当这个迭代时,我需要以某种方式检查greeting.author 再次检查javascript 中存在的一些变量,然后做一些其他的事情。 有可能吗?

【问题讨论】:

    标签: javascript google-app-engine jinja2


    【解决方案1】:
    {% for greeting in greetings %}
      {% if greeting.author %}
        <b>{{ greeting.author }}</b> wrote:
      {% else %}
        An anonymous person wrote:
      {% endif %}
      <blockquote>{{ greeting.content|escape }}</blockquote>
      <script type="text/javascript">
         var greeting_author = "{{ greeting.author }}";
         if(greeting_author === someVariable){
            // do other stuff here
         }
      </script>
    {% endfor %}
    

    基本上,您可以将变量打印为 js。记住类型(例如,将"" 放在它们周围作为字符串)。

    【讨论】:

    • 我明白了。所以它必须在html中。换句话说,我们不能在 javascript 函数中编写循环并遍历问候语。
    猜你喜欢
    • 2014-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多