jinja2

Go HTML templates: defining array of array of strings to range over

前提是你 提交于 2020-01-06 06:24:13
问题 In Jinja2 (Python Flask) templates, I'm able to create a static navigation menu by defining a list of tuples with code similar to the following: {% for item in [('', 'Home'), ('menu1', 'Menu1'), ('menu2', 'Menu2')] %} <li><a href="{% if item[0] == '' %}/{% else %}{{ '/%s/' % item[0] }}{% endif %}">{{ item[1] }}</a></li> {%- endfor %} I'd like to create something similar in Go HTML templates. I assume the equivalent to a list of tuples would be an array/slice of arrays of strings, i.e.,

Any way to get variables keys from jinja2 template string?

↘锁芯ラ 提交于 2020-01-06 06:24:09
问题 Here is the code snippet I have initialized jinja2 template from jinja2 import Template templ = Template("{{foo}} to {{bar}}") And I am willing to extract the template string variable keys from the template obj as below. templ.keys() == ["foo", "bar"] Is there any API to make it work? I have searched for a long while but got nothing work. 回答1: using jinja2.meta you could: from jinja2 import Environment, meta templ_str = "{{foo}} to {{bar}}" env = Environment() ast = env.parse(templ_str) print

Google App Engine Upload to TextArea (without the blobstore)

折月煮酒 提交于 2020-01-06 02:15:05
问题 I'm trying to get simple file reading in GAE python. I want to allow users to upload a csv file directly from their disk and have it display in a text field. I am having an outrageous amount of trouble doing so. Any help would be appreciated. I would prefer to avoid using the blobstore if possible. I don't want to save the data forever, I just want to use it to allow people to fill the textarea easily. Here's what my form looks like <div class = "section hidden" id ="file_choice"> <form

Google App Engine Upload to TextArea (without the blobstore)

杀马特。学长 韩版系。学妹 提交于 2020-01-06 02:14:07
问题 I'm trying to get simple file reading in GAE python. I want to allow users to upload a csv file directly from their disk and have it display in a text field. I am having an outrageous amount of trouble doing so. Any help would be appreciated. I would prefer to avoid using the blobstore if possible. I don't want to save the data forever, I just want to use it to allow people to fill the textarea easily. Here's what my form looks like <div class = "section hidden" id ="file_choice"> <form

How to pass variables between methods in the same class in Python

こ雲淡風輕ζ 提交于 2020-01-05 09:08:11
问题 I don't think I'm using class variables correctly. Inside the ClientFormPage class, I initialize the active_form to 'f1_form'. After I post the first form, I'd like to advance the active_form to 'f2_form', however it keeps resetting to 'f1_form'. What is the best way to do this? class ClientFormPage(PageHandler): active_form = 'f1_form' def render_form(self, f1='hidden', f2='hidden', **kw): self.render('clientforms.html', form1=f1, form2=f2, **kw) def get(self): self.render_form(f1='') def

How to pass variables between methods in the same class in Python

喜夏-厌秋 提交于 2020-01-05 09:06:08
问题 I don't think I'm using class variables correctly. Inside the ClientFormPage class, I initialize the active_form to 'f1_form'. After I post the first form, I'd like to advance the active_form to 'f2_form', however it keeps resetting to 'f1_form'. What is the best way to do this? class ClientFormPage(PageHandler): active_form = 'f1_form' def render_form(self, f1='hidden', f2='hidden', **kw): self.render('clientforms.html', form1=f1, form2=f2, **kw) def get(self): self.render_form(f1='') def

Using Jinja2 template under Knockout attr binding

混江龙づ霸主 提交于 2020-01-05 08:22:09
问题 I am making a project using Flask and Knockoutjs. I am using Knockout to show comments and below the commentor's name would also be displayed which is when clicked takes the person to that user profile Here is the code <a data-bind="attr: { href: '{{ url_for('user_profile')}}' + '/' + name() + '/' + uid() + '/' }"><p data-bind="text: name"></p></a> but in the above code there comes a Jinja2 Template error as BuildError: ('user_profile', {}, None) So I changed the above piece of code referred

pass javascript variables into my macro in jinja2

こ雲淡風輕ζ 提交于 2020-01-05 08:14:32
问题 I am trying to display a simple error message upon a $.get() failure $.get('{{ url_for(c) }}', function(data) { $('#{{ chart_id }}').highcharts(data); }).fail(function(resp) { {# I am not sure why this is not working as intended #} {% set error_html = error(resp, c)|replace('\n', '\\\n') %} $('#{{ chart_id }}').html('{{ error_html }}'); }); However, it does not seem that I have access to my resp object, which is a javascript variable. How does one pass that into the macro as if it were a

pass javascript variables into my macro in jinja2

陌路散爱 提交于 2020-01-05 08:14:30
问题 I am trying to display a simple error message upon a $.get() failure $.get('{{ url_for(c) }}', function(data) { $('#{{ chart_id }}').highcharts(data); }).fail(function(resp) { {# I am not sure why this is not working as intended #} {% set error_html = error(resp, c)|replace('\n', '\\\n') %} $('#{{ chart_id }}').html('{{ error_html }}'); }); However, it does not seem that I have access to my resp object, which is a javascript variable. How does one pass that into the macro as if it were a

Why does this Jinja macro render text instead of HTML?

我是研究僧i 提交于 2020-01-05 08:06:36
问题 I have the following template called post-macro , but when I call the macro in the page.html template, it inserts a string, with quotes, containing the HTML, rather than directly inserting the HTML. post-macro : {% macro postmacro(post) %} // html here {% endmacro %} page.html : {% from "post-macro" import postmacro with context %} {{ postmacro(post) }} It renders the page like this. Notice the double quotes around the output. " html here " What do I need to change to get the HTML directly