mirror of
https://github.com/langgenius/dify-docs.git
synced 2026-03-28 13:38:36 +07:00
81 lines
3.1 KiB
Plaintext
81 lines
3.1 KiB
Plaintext
---
|
||
title: テンプレート
|
||
---
|
||
|
||
### 定義
|
||
|
||
Jinja2のPythonテンプレート言語を使って、データ変換やテキスト処理などを柔軟に行うことができます。
|
||
|
||
### Jinjaとは?
|
||
|
||
> [Jinja](https://jinja.palletsprojects.com/en/3.1.x/) は、速く、表現力があり、拡張可能なテンプレートエンジンです。
|
||
|
||
### シーン
|
||
|
||
テンプレートノードを使うことで、強力なPythonテンプレート言語であるJinja2を用いて、ワークフロー内で軽量かつ柔軟なデータ変換が可能になります。これは、テキスト処理やJSON変換などのシナリオに適しています。例えば、前のステップからの変数を柔軟にフォーマットして結合し、単一のテキスト出力を作成することができます。これは、複数のデータソースの情報を特定のフォーマットにまとめ、後続のステップの要件を満たすのに非常に適しています。
|
||
|
||
**例1:**複数の入力(記事のタイトル、紹介、内容)を一つの完全なテキストに結合する
|
||
|
||
**例2:** ナレッジリトリーバルノードで取得した情報およびその関連メタデータを、構造化されたMarkdown形式にまとめる
|
||
|
||
```
|
||
{% raw %}
|
||
{% for item in chunks %}
|
||
### Chunk {{ loop.index }}.
|
||
### Similarity: {{ item.metadata.score | default('N/A') }}
|
||
|
||
#### {{ item.title }}
|
||
|
||
##### Content
|
||
{{ item.content | replace('\n', '\n\n') }}
|
||
|
||
---
|
||
{% endfor %}
|
||
{% endraw %}
|
||
```
|
||
|
||

|
||
|
||
Jinjaの[公式ドキュメント](https://jinja.palletsprojects.com/en/3.1.x/templates/)を参考にして、さまざまなタスクを実行するためのより複雑なテンプレートを作成することができます。
|
||
|
||
**例3:** HTMLフォームのレンダリングをサポート
|
||
|
||
```html
|
||
<form data-format="json"> // Default to text
|
||
<label for="username">Username:</label>
|
||
<input type="text" name="username" />
|
||
<label for="password">Password:</label>
|
||
<input type="password" name="password" />
|
||
<label for="content">Content:</label>
|
||
<textarea name="content"></textarea>
|
||
<label for="date">Date:</label>
|
||
<input type="date" name="date" />
|
||
<label for="time">Time:</label>
|
||
<input type="time" name="time" />
|
||
<label for="datetime">Datetime:</label>
|
||
<input type="datetime" name="datetime" />
|
||
<label for="select">Select:</label>
|
||
<input type="select" name="select" data-options='["hello","world"]'/>
|
||
<input type="checkbox" name="check" data-tip="By checking this means you agreed"/>
|
||
<button data-size="small" data-variant="primary">Login</button>
|
||
</form>
|
||
```
|
||
|
||
<img
|
||
src="https://assets-docs.dify.ai/2025/04/9d24e9cfa3cdde00e4eee15bd4bbea76.png"
|
||
width="300"
|
||
className="mx-auto"
|
||
alt="metadata_field"
|
||
/>
|
||
|
||
{/*
|
||
Contributing Section
|
||
DO NOT edit this section!
|
||
It will be automatically generated by the script.
|
||
*/}
|
||
|
||
---
|
||
|
||
[このページを編集する](https://github.com/langgenius/dify-docs/edit/main/ja-jp/guides/workflow/node/template.mdx) | [問題を報告する](https://github.com/langgenius/dify-docs/issues/new?template=docs.yml)
|
||
|