string template to generate Jinja2 template
up vote
0
down vote
favorite
I want to generate jinja2 template that represent XML document.
I have long list of possible tags, and final template would look like this:
from jinja2 import Template
template = Template("""
<root>
{%- tag1 %}<tag1>{{ tag1 }}</tag1>{% endif %}
{%- tag2 %}<tag2>{{ tag2 }}</tag2>{% endif %}
</root>
""")
template.render(**{'tag1':'value1'})
Above code would generate next document:
<root>
<tag1>value1</tag1>
</root>
Now, i have a list of tags tags_for_template = [f'tag{i}' for in in range(N)]
and want to generate above template.
at the end I want generator code to be readable.
Here is my best solution so far:
"n".join(map(lambda x: "{%- if "+x+" %}<"+x+">{{ "+x+" }}</"+x+">{% endif %}",
tags_for_template))
Could you suggest anything more readable?
python jinja2 code-generation
add a comment |
up vote
0
down vote
favorite
I want to generate jinja2 template that represent XML document.
I have long list of possible tags, and final template would look like this:
from jinja2 import Template
template = Template("""
<root>
{%- tag1 %}<tag1>{{ tag1 }}</tag1>{% endif %}
{%- tag2 %}<tag2>{{ tag2 }}</tag2>{% endif %}
</root>
""")
template.render(**{'tag1':'value1'})
Above code would generate next document:
<root>
<tag1>value1</tag1>
</root>
Now, i have a list of tags tags_for_template = [f'tag{i}' for in in range(N)]
and want to generate above template.
at the end I want generator code to be readable.
Here is my best solution so far:
"n".join(map(lambda x: "{%- if "+x+" %}<"+x+">{{ "+x+" }}</"+x+">{% endif %}",
tags_for_template))
Could you suggest anything more readable?
python jinja2 code-generation
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to generate jinja2 template that represent XML document.
I have long list of possible tags, and final template would look like this:
from jinja2 import Template
template = Template("""
<root>
{%- tag1 %}<tag1>{{ tag1 }}</tag1>{% endif %}
{%- tag2 %}<tag2>{{ tag2 }}</tag2>{% endif %}
</root>
""")
template.render(**{'tag1':'value1'})
Above code would generate next document:
<root>
<tag1>value1</tag1>
</root>
Now, i have a list of tags tags_for_template = [f'tag{i}' for in in range(N)]
and want to generate above template.
at the end I want generator code to be readable.
Here is my best solution so far:
"n".join(map(lambda x: "{%- if "+x+" %}<"+x+">{{ "+x+" }}</"+x+">{% endif %}",
tags_for_template))
Could you suggest anything more readable?
python jinja2 code-generation
I want to generate jinja2 template that represent XML document.
I have long list of possible tags, and final template would look like this:
from jinja2 import Template
template = Template("""
<root>
{%- tag1 %}<tag1>{{ tag1 }}</tag1>{% endif %}
{%- tag2 %}<tag2>{{ tag2 }}</tag2>{% endif %}
</root>
""")
template.render(**{'tag1':'value1'})
Above code would generate next document:
<root>
<tag1>value1</tag1>
</root>
Now, i have a list of tags tags_for_template = [f'tag{i}' for in in range(N)]
and want to generate above template.
at the end I want generator code to be readable.
Here is my best solution so far:
"n".join(map(lambda x: "{%- if "+x+" %}<"+x+">{{ "+x+" }}</"+x+">{% endif %}",
tags_for_template))
Could you suggest anything more readable?
python jinja2 code-generation
python jinja2 code-generation
asked Nov 22 at 1:44
vav
3,34911130
3,34911130
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422797%2fstring-template-to-generate-jinja2-template%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown