Просмотр исходного кода

add dict.get template tag helper

checktheroads 4 лет назад
Родитель
Сommit
013fbf79e9
1 измененных файлов с 11 добавлено и 0 удалено
  1. 11 0
      netbox/utilities/templatetags/helpers.py

+ 11 - 0
netbox/utilities/templatetags/helpers.py

@@ -1,6 +1,7 @@
 import datetime
 import json
 import re
+from typing import Dict, Any
 
 import yaml
 from django import template
@@ -238,6 +239,16 @@ def startswith(text: str, starts: str) -> bool:
     return False
 
 
+@register.filter
+def get_key(value: Dict, arg: str) -> Any:
+    """
+    Template implementation of `dict.get()`, for accessing dict values
+    by key when the key is not able to be used in a template. For
+    example, `{"ui.colormode": "dark"}`.
+    """
+    return value.get(arg, None)
+
+
 #
 # Tags
 #