I don’t know the proper names for these structures, will I guess now I do. So isset can’t work with an array basically because there are having no keys below the main one, without entering the array which I don’t think you can do.
Why would one use isset when it seems to operate and output the same as if ? And if throws a fewer warnings or errors and has fewer formatting requirements. An empty key returns false, a non-existent key returns false.
You are of course correct with respect to shortcodes. And I did try it. The params of a shortcode cannot be access with object dot notation. They have to be accessed with .Get "param". So what your shortcode is doing is checking if there are any Params, and then checking if the “x” param is set, and then getting it if so.
For layout templates, I did the below. And it seems if you want something that is falsy to return false, you should use if. And I don’t know how useful isset would be in such a situation. Especially since it returns true if the key exists at all, whether it has an empty value, null, or something.
I haven’t read this entire thread, but isset was added, I suspect, when if and with had some issues that is now fixed. I cannot remember the last time I used isset.
I use isset to check if a key exist regardless of its value. if and with will return false if the key is set but falsy. isset will only return false if the key is absent… Hope I’m not wrong
Gives the error shortcodes\foo.html:13:10": execute of template failed at <.Params.x>: can’t evaluate field x in type interface {}
Gives the error shortcodes\foo.html:14:3": execute of template failed at <index .Params "x">: error calling index: cannot index slice/array with type string
What am I missing?
shortcode is:
{{/* Set default */}}
{{ $x := true }}
{{/* Get param */}}
{{ if .Params }}
{{ if isset .Params "x" }}
{{ $x = .Get "x" }}
{{ end }}
{{ end }}
{{/* Display param */}}
{{ .Get "x" }}
{{ .Params.x }}
{{ index .Params "x" }}
{{ $x }}
Which is called in content file as you described above.
@jmooring I’m still not able to get what you described working from a short code. Only the Get works.
EDIT: I’m sorry, I realized just now testing it again why it wasn’t working. I had called the shortcode in one instance without the x= arg set, so those two methods would error out since it assumes it exists. Where get won’t error if it doesn’t exist.