mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-08-29 15:06:26 +02:00
Added safe_var function
This commit is contained in:
27
filter_plugins/safe_var.py
Normal file
27
filter_plugins/safe_var.py
Normal file
@@ -0,0 +1,27 @@
|
||||
# file: filter_plugins/safe_var.py
|
||||
|
||||
from jinja2 import Undefined
|
||||
|
||||
def safe_var(value):
|
||||
"""
|
||||
Returns the original value unless it is None or Jinja2‐Undefined.
|
||||
Catches all exceptions and returns an empty string on error.
|
||||
"""
|
||||
try:
|
||||
# If the value is an Undefined from Jinja2, treat it as missing
|
||||
if isinstance(value, Undefined):
|
||||
return ''
|
||||
# Treat None as missing as well
|
||||
if value is None:
|
||||
return ''
|
||||
# Otherwise return the actual value
|
||||
return value
|
||||
except Exception:
|
||||
# Catch any other errors and return empty string
|
||||
return ''
|
||||
|
||||
class FilterModule(object):
|
||||
def filters(self):
|
||||
return {
|
||||
'safe_var': safe_var
|
||||
}
|
Reference in New Issue
Block a user