Creating a helper to render a variable which won't throw an exception

I want to be able to render a variable on an html page such that an error won’t be thrown even when a variable, which may be a structre, doesn’t exist or nil. How to create a helper method?

<%= render_variable_safe(my_var1) %>

<%= render_variable_safe(my_var2.field1.field2) %>

<%= render_variable_safe(my_var3.field1.field2.field3) %>

None of these should thrown an exception if a variable or field doesn’t exist or nil.

. access means it enforces the key is there, [...] access means it’s optional. my_var3.field1.field2.field3 requires all of field1, field2, and field3 to exist, where my_var3[:field1][:field2][:field3] is optional the whole way down. Even if field1 is nil, then it all returns nil.