>  기사  >  백엔드 개발  >  조건이 있는 경우 템플릿 이동

조건이 있는 경우 템플릿 이동

王林
王林앞으로
2024-02-06 11:24:13380검색

Go 模板 if 条件

질문 내용

andeq/ne 기능을 어떻게 결합하나요?

이 영상은 제가 썼습니다

으아아아

목표는:

  • 내 알림에 두 개의 라벨이 포함된 경우 infoalert: truetopic:database grafana 링크만 표시됩니다
  • 내 알림에 태그만 포함된 경우 topic: database 但不包含 infoalert: true 그러면 데이터베이스 링크만 표시됩니다

경고가 실행될 때 Alertmanager.log에 다음 오류가 발생하기 때문에 조건부 {{- if and eq .commonlabels.infoalert "true" eq .commonlabels.topic "database" -}} 구문이 잘못된 것 같습니다.

{{ define "opsgenie.default.tmpl" }}
  <font size="+0"><b>{{.commonlabels.alertname }}</b></font>
  {{- range $i, $alert := .alerts }}
  <font size="+0">{{ .annotations.description }}</font>
  {{- end -}}
  {{- "\n" -}}
  {{- "\n" -}}
  {{- if and eq .commonlabels.infoalert "true" eq .commonlabels.topic "database" -}}
  grafana: https://{{ .commonlabels.url }}
  {{- "\n" -}}{{- end -}}
  {{- if and ne .commonlabels.infoalert "true" eq .commonlabels.topic "database" -}}
  database:
    • https://{{ .commonlabels.url }}/
    • https://{{ .commonlabels.url }}/
  {{- "\n" -}}{{- end -}}
  {{- end -}}
  {{- end -}}
{{- end -}}

정답


괄호를 사용하여 표현식을 그룹화하세요.

으아아아

테스트 가능한 예시를 확인해 보세요:

으아아아

이 결과는 다음과 같습니다(go 놀이터에서 시도해 보세요):

으아아아

위 내용은 조건이 있는 경우 템플릿 이동의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 stackoverflow.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제