Add request shortcode
parent
2e8c210119
commit
4eac14a0bb
@ -0,0 +1,79 @@
|
|||||||
|
+++
|
||||||
|
authors = ["author-demo"]
|
||||||
|
categories = ["paige", "shortcodes"]
|
||||||
|
description = "Demonstration of the request shortcode."
|
||||||
|
tags = ["request", "figures"]
|
||||||
|
title = "Request"
|
||||||
|
+++
|
||||||
|
|
||||||
|
The `paige/request` shortcode displays the content for a URL.
|
||||||
|
|
||||||
|
<!--more-->
|
||||||
|
|
||||||
|
## Basic
|
||||||
|
|
||||||
|
Code:
|
||||||
|
|
||||||
|
```go-html-template
|
||||||
|
<pre>
|
||||||
|
{{</* paige/request "https://raw.githubusercontent.com/willfaught/paige/master/LICENSE" */>}}
|
||||||
|
</pre>
|
||||||
|
```
|
||||||
|
|
||||||
|
Result:
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
{{< paige/request "https://raw.githubusercontent.com/willfaught/paige/master/LICENSE" >}}
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
## Method parameter
|
||||||
|
|
||||||
|
Code:
|
||||||
|
|
||||||
|
```go-html-template
|
||||||
|
<pre>
|
||||||
|
{{</* paige/request method="get" url="https://raw.githubusercontent.com/willfaught/paige/master/LICENSE" */>}}
|
||||||
|
</pre>
|
||||||
|
```
|
||||||
|
|
||||||
|
Result:
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
{{< paige/request method="get" url="https://raw.githubusercontent.com/willfaught/paige/master/LICENSE" >}}
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
## Url parameter
|
||||||
|
|
||||||
|
Code:
|
||||||
|
|
||||||
|
```go-html-template
|
||||||
|
<pre>
|
||||||
|
{{</* paige/request url="https://raw.githubusercontent.com/willfaught/paige/master/LICENSE" */>}}
|
||||||
|
</pre>
|
||||||
|
```
|
||||||
|
|
||||||
|
Result:
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
{{< paige/request url="https://raw.githubusercontent.com/willfaught/paige/master/LICENSE" >}}
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
## Figure
|
||||||
|
|
||||||
|
Code:
|
||||||
|
|
||||||
|
```go-html-template
|
||||||
|
{{</* paige/figure caption="License" */>}}
|
||||||
|
<pre>
|
||||||
|
{{</* paige/request "https://raw.githubusercontent.com/willfaught/paige/master/LICENSE" */>}}
|
||||||
|
</pre>
|
||||||
|
{{</* /paige/figure */>}}
|
||||||
|
```
|
||||||
|
|
||||||
|
Result:
|
||||||
|
|
||||||
|
{{< paige/figure caption="License" >}}
|
||||||
|
<pre>
|
||||||
|
{{< paige/request "https://raw.githubusercontent.com/willfaught/paige/master/LICENSE" >}}
|
||||||
|
</pre>
|
||||||
|
{{< /paige/figure >}}
|
@ -0,0 +1,16 @@
|
|||||||
|
{{- $method := .Get "method" | default "get" -}}
|
||||||
|
{{- $url := .Get 0 | default (.Get "url") -}}
|
||||||
|
|
||||||
|
{{- if not $url -}}
|
||||||
|
{{- errorf "layouts/shortcodes/paige/request.html: no url" -}}
|
||||||
|
{{- end -}}
|
||||||
|
|
||||||
|
{{- with resources.GetRemote $url (dict "method" $method) -}}
|
||||||
|
{{- with .Err -}}
|
||||||
|
{{- errorf "layouts/shortcodes/paige/request.html: cannot request remote resource %q: %v" $url . -}}
|
||||||
|
{{- else -}}
|
||||||
|
{{- strings.TrimRight "\f\n\r\t\v " .Content -}}
|
||||||
|
{{- end -}}
|
||||||
|
{{- else -}}
|
||||||
|
{{- errorf "layouts/shortcodes/paige/request.html: cannot request remote resource %q" $url -}}
|
||||||
|
{{- end -}}
|
Loading…
Reference in New Issue