-
-
Notifications
You must be signed in to change notification settings - Fork 332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Translate Leaf to Japanese #1005
Changes from 1 commit
b7311dd
ae66e50
6d616f5
e9d7a60
2844462
293824c
820ab00
674c164
b33141d
2bcc80b
862d3ba
eb1634f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
# カスタムタグ | ||
|
||
[`LeafTag`](https://api.vapor.codes/leafkit/documentation/leafkit/leaftag) プロトコルを使用して、カスタム Leaf タグを作成することができます。 | ||
|
||
これを実際に試してみるために、現在のタイムスタンプを表示するカスタムタグ `#now` を作成してみましょう。このタグは、日付形式を指定するためのオプションのパラメータもサポートします。 | ||
|
||
!!! tip | ||
もしカスタムタグが HTML をレンダリングする場合は、HTML がエスケープされないように、`UnsafeUnescapedLeafTag` に準拠させる必要があります。ユーザー入力のチェックやサニタイズを忘れないようにしましょう。 | ||
|
||
## `LeafTag` | ||
|
||
まず、`NowTag` というクラスを作成し、`LeafTag` に準拠させます。 | ||
|
||
```swift | ||
struct NowTag: LeafTag { | ||
func render(_ ctx: LeafContext) throws -> LeafData { | ||
... | ||
} | ||
} | ||
``` | ||
|
||
次に、`render(_:)` メソッドを実装します。このメソッドに渡される `LeafContext` は、必要なすべての情報を持っています。 | ||
|
||
```swift | ||
enum NowTagError: Error { | ||
case invalidFormatParameter | ||
case tooManyParameters | ||
} | ||
|
||
struct NowTag: LeafTag { | ||
func render(_ ctx: LeafContext) throws -> LeafData { | ||
let formatter = DateFormatter() | ||
switch ctx.parameters.count { | ||
case 0: formatter.dateFormat = "yyyy-MM-dd HH:mm:ss" | ||
case 1: | ||
guard let string = ctx.parameters[0].string else { | ||
throw NowTagError.invalidFormatParameter | ||
} | ||
|
||
formatter.dateFormat = string | ||
default: | ||
throw NowTagError.tooManyParameters | ||
} | ||
|
||
let dateAsString = formatter.string(from: Date()) | ||
return LeafData.string(dateAsString) | ||
} | ||
} | ||
``` | ||
|
||
## タグの設定 | ||
|
||
`NowTag` を実装したので、Leaf にそれを伝えるだけです。このようにして、別のパッケージから来るタグでも追加することができます。通常、これを `configure.swift` で行います: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 好みですが There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 「だった」は話し言葉な気がするので、「たとえ別のパッケージで定義されたタグでも追加することができます。」の方が文章としては自然だと思うのですが、いかがでしょうか? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ありがとうございます! とてもいいと思います! |
||
|
||
```swift | ||
app.leaf.tags["now"] = NowTag() | ||
``` | ||
|
||
これで完了です!Leaf でカスタムタグを使用できるようになりました。 | ||
|
||
```leaf | ||
The time is #now() | ||
``` | ||
|
||
## コンテキストプロパティ | ||
|
||
The `LeafContext` contains two important properties. `parameters` and `data` that has everything we should need. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 元の英文も混ざっちゃっています |
||
`LeafContext` には、重要なプロパティが 2 つあります。それが `parameters` と `data` です。この 2 つで必要な情報はすべて揃っています。 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
- `parameters`: タグのパラメータを含む配列です | ||
- `data`: コンテキストとして `render(_:_:)` に渡されたビューのデータを含む辞書です | ||
|
||
### サンプルの Hello タグ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
これを理解するために、両方のプロパティを使ったシンプルな hello タグを実装してみましょう。 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
#### パラメータの使用 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. parametersがプロパティ名なのでそのままparametersと英語表記の方がわかりやすいかもです |
||
|
||
最初のパラメータに名前が含まれていることを想定しています。 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 少しニュアンスが違うような気がしました。 直訳で |
||
|
||
```swift | ||
enum HelloTagError: Error { | ||
case missingNameParameter | ||
} | ||
|
||
struct HelloTag: UnsafeUnescapedLeafTag { | ||
func render(_ ctx: LeafContext) throws -> LeafData { | ||
guard let name = ctx.parameters[0].string else { | ||
throw HelloTagError.missingNameParameter | ||
} | ||
|
||
return LeafData.string("<p>Hello \(name)</p>") | ||
} | ||
} | ||
``` | ||
|
||
```leaf | ||
#hello("John") | ||
``` | ||
|
||
#### データの使用 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. こちらもdataの方がいいかと思われます! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DONE! : 820ab00 |
||
|
||
データプロパティの中の "name" キーを使って名前の値にアクセスします。 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dataがプロパティ名なのでそのまま |
||
|
||
```swift | ||
enum HelloTagError: Error { | ||
case nameNotFound | ||
} | ||
|
||
struct HelloTag: UnsafeUnescapedLeafTag { | ||
func render(_ ctx: LeafContext) throws -> LeafData { | ||
guard let name = ctx.data["name"]?.string else { | ||
throw HelloTagError.nameNotFound | ||
} | ||
|
||
return LeafData.string("<p>Hello \(name)</p>") | ||
} | ||
} | ||
``` | ||
|
||
```leaf | ||
#hello() | ||
``` | ||
|
||
_Controller_: | ||
|
||
```swift | ||
return try await req.view.render("home", ["name": "John"]) | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍