מקור המעבדה: תמלול השיעור


מטרות

במעבדה זו תלמדו ותבצעו:

  1. שילוב Named Templates עם ולידציה (fail, required).
  2. יצירת פונקציות ולידציה מותאמות (Custom Validators).
  3. קריאה ל־validators באמצעות include.
  4. הבנת context בעת קריאה ל־validator.
  5. יצירת validators reusable.
  6. הפרדה בין ולידציה לבין רינדור.

חלק 1: יצירת Validator ראשון – Port Range

הבנה מושגית

נרצה:


מימוש ב־_helpers.tpl

{{- define "templating-deep-dive.validators.portRange" -}}
{{- /* expects a port as context */ -}}

{{- $sanitizedPort := int . -}}

{{- if or (lt $sanitizedPort 1) (gt $sanitizedPort 65535) }}
{{- fail "ports must be between 1 and 65535" }}
{{- end }}

{{- . -}}

{{- end -}}