.Get cannot be used outside the short code, how to use .Path or .Position

I want to add an error message to the parameters of the method, but the method is not in the short code.

{{ $targetPath := .targetPath }}
{{ $resourcesGet := .resourcesGet }}


{{ with $targetPath }}
    {{ else }}
    {{ errorf "Missing Required parameter name [ targetPath ]: %s" .Position }}
{{ end }}

{{ with $resourcesGet }}
    {{ else }}
    {{ errorf "Missing Required parameter name [ resourcesGet ]: %s" .Position }}
{{ end }}

The following is an interception of the main content of the code error report.

ERROR 2023/06/05 09:36:24 缺少必须的参数名称 [ resourcesGet ]: %!s(<nil>)
error calling toCSS: no Resource provided in transformation

.Get is specifically used when creating your own shortcode template, to access the positional and named parameters passed to it. When used with a numeric INDEX, it queries positional parameters (starting with 0). With a string KEY, it queries named parameters.

Code reference document address

Site search related issues, unresolved

I don’t understand. Your problem statement is unclear.

The following code files are located in the [ site/layouts/shortcodes ] folder.

where you can use the shortcode-specific functions [ .Get ],
If you put this file in another folder, for example [ /layouts/partials/functions/ ]
.Get is not the way to use it? So what should replace this function with?

My question is, in other folders, how to write one, with the parameter name and the number of parameters passed in as expected.
If it matches, continue until the last return related value reaches the calling place,
If it does not meet expectations, use errorf to remind and interrupt execution.

For this reminder, one of [ .Posistion / .Path ] needs to be used. The Position in my initial question obviously has no resulting value.

Translation source: Google Translate

{{- $name := (.Get 0) -}}
{{- with $name -}}
    {{- with ($.Page.Param .) }}
        {{ . }}
    {{ else }}
        {{ errorf "Param %q not found: %s" $name $.Position }}
    {{ end -}}
{{- else }}
    {{ errorf "Missing param key: %s" $.Position }}
{{ end -}}

The .Get, .Name, and .Position methods may be used in shortcode templates, but not in partial templates. If you call a partial template like this:

{{ partial "foo" (dict "param_a" 1 "param_b" 2) }}

You can “get” a parameter from anywhere within the partial template by referencing the top-level context ($). For example:

{{ $a := $.param_a }}

You can determine the number of parameters from anywhere within the partial template by checking length of the top-level context ($). For example:

{{ $numParams := len $ }}

To include the “name” of the partial template in an error message, you will need to hard code the value.

Currently, within a partial template, you do not know which template called the partial. Consequently, there is no way to include a “position” within an error message.

Finally, here’s an example of a partial that validates the context, checking number of parameters, required parameters, allowed values, and type:

layouts/partials/functions/math.html
{{- /*
Returns the average, median, minimum, maximum, product, or sum of a slice of numbers.

@context {slice} list A slice of numbers.
@context {string} op The operation to perform on the list.

@returns {float64}

@example {{ partial "functions/math" (dict "op" "average" "list" (slice 1 2 3)) }}
@example {{ partial "functions/math" (dict "op" "median" "list" (slice 1 2 3)) }}
@example {{ partial "functions/math" (dict "op" "min" "list" (slice 1 2 3)) }}
@example {{ partial "functions/math" (dict "op" "max" "list" (slice 1 2 3)) }}
@example {{ partial "functions/math" (dict "op" "product" "list" (slice 1 2 3)) }}
@example {{ partial "functions/math" (dict "op" "sum" "list" (slice 1 2 3)) }}
*/}}

{{- /* Initialize. */}}
{{- $commonErrorMessage := "The %q partial requires 2 parameters: op (a string) and list (a slice of numbers)." }}
{{- $partialName := "math" }}
{{- $result := 0 }}
{{- $validOps := slice "average" "median" "min" "max" "product" "sum" }}

{{- /* Validate parameters. */}}
{{- if ne (len .) 2 }}
  {{- errorf $commonErrorMessage $partialName }}
{{- end }}
{{- with $.op }}
  {{- if not (in $validOps $.op) }}
    {{- errorf "The %q parameter passed to the %q partial is invalid. Valid operations are %s." "op" $partialName (delimit $validOps ", " " and ") }}
  {{- end }}
{{- else }}
  {{- errorf $commonErrorMessage $partialName }}
{{- end }}
{{- with $.list }}
  {{- if not (reflect.IsSlice $.list) }}
    {{- errorf $commonErrorMessage $partialName }}
  {{- else }}
    {{- range . }}
      {{- if not (in (slice "int" "float64") (printf "%T" .)) }}
        {{- errorf "The %q parameter passed to the %q partial contains a non-numeric value." "list" $partialName }}
      {{- end }}
    {{- end }}
  {{- end }}
{{- else }}
  {{- errorf $commonErrorMessage $partialName }}
{{- end }}

{{- /* Cast list to float64 and sort. */}}
{{- $list := apply $.list "float" "." | sort }}

{{- /* Determine sum and length of list. */}}
{{- $sum := 0 }}
{{- range $list }}
  {{- $sum = add $sum . }}
{{- end }}
{{- $len := len $list }}

{{- /* Determine result based on operation. */}}
{{- if eq $.op "average" }}
  {{- $result = div $sum $len }}
{{- else if eq $.op "median" }}
  {{- if math.ModBool $len 2 }}
    {{- $n1 := index $list (div $len 2) }}
    {{- $n2 := index $list (sub (div $len 2) 1) }}
    {{- $result = div (add $n1 $n2) 2 }}
  {{- else }}
    {{- $result = index $list (div $len 2) }}
  {{- end }}
{{- else if eq $.op "min" }}
  {{- $result = index $list 0 }}
{{- else if eq $.op "max" }}
  {{- $result = index $list (sub $len 1) }}
{{- else if eq $.op "product" }}
  {{- $result = 1 }}
  {{- range $list }}
    {{- $result = mul $result . }}
  {{- end }}
{{- else if eq $.op "sum" }}
  {{- $result = $sum }}
{{- end }}

{{- /* Return result. */}}
{{- return (float $result) }}

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.