で次のことを行う方法はありhtml/template
ますか?
{{template "mytemplate" struct{Foo1, Foo2 string}{"Bar1", "Bar2"}}}
上記のように、実際にはテンプレートを意味します。FuncMap
構造体を返す登録済みの関数経由ではありません。
私はそれを試しましたが、Parse
パニックになりました。 Playground を参照してください。多分私は構文を間違ったのですか?
で次のことを行う方法はありhtml/template
ますか?
{{template "mytemplate" struct{Foo1, Foo2 string}{"Bar1", "Bar2"}}}
上記のように、実際にはテンプレートを意味します。FuncMap
構造体を返す登録済みの関数経由ではありません。
私はそれを試しましたが、Parse
パニックになりました。 Playground を参照してください。多分私は構文を間違ったのですか?
他の人が指摘したように、それは不可能です。テンプレートは、Go コンパイラの助けを借りずに、実行時に解析されます。そのため、任意の Go 構文を許可することは現実的ではありません (ただし、標準ライブラリには Go ソース テキストを解析するためのすべてのツールが含まれているため、不可能ではないことに注意してくださいgo/
。標準ライブラリで「接頭辞」が付いているパッケージを参照してください)。設計哲学により、複雑なロジックはテンプレートの外にある必要があります。
あなたの例に戻ります:
struct{Foo1, Foo2 string}{"Bar1", "Bar2"}
これは構造体複合リテラルであり、テンプレートではサポートされていません。別のテンプレートを呼び出す場合でも、他の場所でもサポートされていません。
カスタム「引数」を使用して別のテンプレートを呼び出すには、次の構文があります(text/template
: Actionsから引用)。
{{template "name" pipeline}}
The template with the specified name is executed with dot set
to the value of the pipeline.
TL;DR; パイプラインは、定数、何らかの値のフィールドまたはメソッドを示す式 (メソッドが呼び出され、その戻り値が使用される場所)、「テンプレート組み込み」関数またはカスタム登録された関数への呼び出しである場合があります。関数、またはマップ内の値。
パイプラインの場所:
パイプラインは、チェーンされた一連の「コマンド」である可能性があります。コマンドは、単純な値 (引数) または関数またはメソッド呼び出しであり、複数の引数を持つ場合があります。
Argument The result is the value of evaluating the argument. .Method [Argument...] The method can be alone or the last element of a chain but, unlike methods in the middle of a chain, it can take arguments. The result is the value of calling the method with the arguments: dot.Method(Argument1, etc.) functionName [Argument...] The result is the value of calling the function associated with the name: function(Argument1, etc.) Functions and function names are described below.
引数は次のとおりです。
引数は単純な値で、次のいずれかで示されます。
- A boolean, string, character, integer, floating-point, imaginary or complex constant in Go syntax. These behave like Go's untyped constants. Note that, as in Go, whether a large integer constant overflows when assigned or passed to a function can depend on whether the host machine's ints are 32 or 64 bits. - The keyword nil, representing an untyped Go nil. - The character '.' (period): . The result is the value of dot. - A variable name, which is a (possibly empty) alphanumeric string preceded by a dollar sign, such as $piOver2 or $ The result is the value of the variable. Variables are described below. - The name of a field of the data, which must be a struct, preceded by a period, such as .Field The result is the value of the field. Field invocations may be chained: .Field1.Field2 Fields can also be evaluated on variables, including chaining: $x.Field1.Field2 - The name of a key of the data, which must be a map, preceded by a period, such as .Key The result is the map element value indexed by the key. Key invocations may be chained and combined with fields to any depth: .Field1.Key1.Field2.Key2 Although the key must be an alphanumeric identifier, unlike with field names they do not need to start with an upper case letter. Keys can also be evaluated on variables, including chaining: $x.key1.key2 - The name of a niladic method of the data, preceded by a period, such as .Method The result is the value of invoking the method with dot as the receiver, dot.Method(). Such a method must have one return value (of any type) or two return values, the second of which is an error. If it has two and the returned error is non-nil, execution terminates and an error is returned to the caller as the value of Execute. Method invocations may be chained and combined with fields and keys to any depth: .Field1.Key1.Method1.Field2.Key2.Method2 Methods can also be evaluated on variables, including chaining: $x.Method1.Field - The name of a niladic function, such as fun The result is the value of invoking the function, fun(). The return types and values behave as in methods. Functions and function names are described below. - A parenthesized instance of one the above, for grouping. The result may be accessed by a field or map key invocation. print (.F1 arg1) (.F2 arg2) (.StructValuedMethod "arg").Field
適切な解決策は、テンプレート呼び出しに渡したい値を構築するカスタム関数を登録することです。これは、関連する/可能な重複でわかるように: Golang pass multiple values from template to template?
別の半分の解決策は、ビルトインprint
またはprintf
関数を使用して、渡したい値を連結することですが、それには他のテンプレートで分割する必要があります。