admin管理员组文章数量:1024593
Using Askama, I'm trying to create a form template that can be included in both the creation and editing template for an entity (e.g. a blog post). In the former the form would start out empty and in the latter it would be filled with values fetched from the database.
Here's the template I got so far (post
is a struct passed into the template, which is None
in the case of the creation flow):
{% let action -%}
{% let method -%}
{% let title -%}
{% let content -%}
{% let author -%}
{% if let Some(post) = post -%}
{% let action = format!("/posts/{}", post.id) %}
{% let method = "data-hx-put" %}
{% let title = post.title.as_ref() -%}
{% let content = post.content.as_ref() -%}
{% let author = post.author.as_ref() -%}
{% else -%}
{% let action = "/posts".to_string() %}
{% let method = "data-hx-post" %}
{% let title = "" -%}
{% let content = "" -%}
{% let author = "" -%}
{% endif -%}
<form {{method}}="{{ action }}">
<fieldset>
<div class="form-group">
<label for="title">Title</label>
<input class="form-control" name="title" value="{{ title }}" />
</div>
<div class="form-group">
<label for="content">Content</label>
<textarea class="form-control" name="content">
{{- content -}}
</textarea>
</div>
<div class="form-group">
<label for="author">Author</label>
<input class="form-control" name="author" value="{{ author }}" />
</div>
<input class=" btn btn-primary" type="submit" />
</fieldset>
</form>
Writing it this way is very verbose and thus tedious. Is there a better way in Askama to write this?
Using Askama, I'm trying to create a form template that can be included in both the creation and editing template for an entity (e.g. a blog post). In the former the form would start out empty and in the latter it would be filled with values fetched from the database.
Here's the template I got so far (post
is a struct passed into the template, which is None
in the case of the creation flow):
{% let action -%}
{% let method -%}
{% let title -%}
{% let content -%}
{% let author -%}
{% if let Some(post) = post -%}
{% let action = format!("/posts/{}", post.id) %}
{% let method = "data-hx-put" %}
{% let title = post.title.as_ref() -%}
{% let content = post.content.as_ref() -%}
{% let author = post.author.as_ref() -%}
{% else -%}
{% let action = "/posts".to_string() %}
{% let method = "data-hx-post" %}
{% let title = "" -%}
{% let content = "" -%}
{% let author = "" -%}
{% endif -%}
<form {{method}}="{{ action }}">
<fieldset>
<div class="form-group">
<label for="title">Title</label>
<input class="form-control" name="title" value="{{ title }}" />
</div>
<div class="form-group">
<label for="content">Content</label>
<textarea class="form-control" name="content">
{{- content -}}
</textarea>
</div>
<div class="form-group">
<label for="author">Author</label>
<input class="form-control" name="author" value="{{ author }}" />
</div>
<input class=" btn btn-primary" type="submit" />
</fieldset>
</form>
Writing it this way is very verbose and thus tedious. Is there a better way in Askama to write this?
本文标签: rustReusing form for both creating and editing an entity in AskamaStack Overflow
版权声明:本文标题:rust - Reusing form for both creating and editing an entity in Askama - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745608413a2158861.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论