Debian Codegraph @

docs/content/en/methods/resource/Content.md

raw
1 ---
2 title: Content
3 description: Returns the content of the given resource.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 returnType: any
9 signatures: [RESOURCE.Content]
10 ---
11
12 {{% include "/_common/methods/resource/global-page-remote-resources.md" %}}
13
14 The `Content` method on a `Resource` object returns `template.HTML` when the [resource type][] is `page`, otherwise it returns a `string`.
15
16 ```text {file="assets/quotations/kipling.txt"}
17 He travels the fastest who travels alone.
18 ```
19
20 To get the content:
21
22 ```go-html-template
23 {{ with resources.Get "quotations/kipling.txt" }}
24 {{ .Content }} → He travels the fastest who travels alone.
25 {{ end }}
26 ```
27
28 To get the size in bytes:
29
30 ```go-html-template
31 {{ with resources.Get "quotations/kipling.txt" }}
32 {{ .Content | len }} → 42
33 {{ end }}
34 ```
35
36 To create an inline image:
37
38 ```go-html-template
39 {{ with resources.Get "images/a.jpg" }}
40 <img src="data:{{ .MediaType.Type }};base64,{{ .Content | base64Encode }}">
41 {{ end }}
42 ```
43
44 To create inline CSS:
45
46 ```go-html-template
47 {{ with resources.Get "css/style.css" }}
48 <style>{{ .Content | safeCSS }}</style>
49 {{ end }}
50 ```
51
52 To create inline JavaScript:
53
54 ```go-html-template
55 {{ with resources.Get "js/script.js" }}
56 <script>{{ .Content | safeJS }}</script>
57 {{ end }}
58 ```
59
60 [resource type]: /methods/resource/resourcetype/