-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
tpl: Add default function #1943
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
👍 |
|
Hold up. I found an issue that I need to fix. |
|
I didn't planned to merge anything myself 😄 |
| func TestDefault(t *testing.T) { | ||
| for i, this := range []struct { | ||
| dflt interface{} | ||
| given interface{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need nil-variants for these.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
This commit fixes a few things: 1. `given` is now a variadic parameter so that piping works properly 2. add separate template tests to make sure piping works 3. support time values 4. add more tests of the dfault function
|
I had to rework the func signature to use a variadic parameter for Apparently, the Go template engine doesn't pass a nil(?) value through the pipe. I can rebase and squash if this looks good to everyone. |
|
Looks mostly good, but I have one question: The above testcase fails. And while I understand why it fails, it is confusing for the users that this works for maps, but not for slices. I use @derekperkins "list of images" syntax, and then have a rather clumsy construct to pick the first if found. I would rather use |
|
On an added note: I don't expect you to "hook into" the Golang internals to get this to work. I don't see how. But I'm just thinking out loud: If we could override the |
|
@bep, |
|
@moorereason push it with this PR, add my test case + reference the issue I just created. |
This commit adds a custom index template function that deviates from the stdlib simply by not returning an "index out of range" error if an array, slice or string index is out of range. Instead, we just return nil values. This should help make the new default function more useful for Hugo users. Fixes gohugoio#1949
|
Already pushed. 😄 I did pretty much everything you mention except I didn't leave the original code there as a comment; however, I did leave a comment about the deviation from stdlib and reference where I copied it from (there were 3 funcs total). |
|
It looks good. It just have to be obvious what to replace/change if they fix this at some point in the stdlib. I will have a look at and merge this later. |
|
Merged in 2d0650d Good work. |
|
👍🏻👍🏻👍🏻 |
|
Nevermind, this was merged. I should sleep more. /me walks away in shame. |
|
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
No description provided.