Skip to content

feat: silently ignore missing contentOf block #204

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Mido-sys
Copy link
Contributor

Improves contentOf to silently fail when undefined

This PR updates the contentOf helper to no longer throw errors on routes where the contentFor block is not defined. Instead, it will fail silently, allowing templates to render without requiring a default value.

Currently, to prevent errors, a default block must be defined for every use route, which adds unnecessary overhead and complexity. This change simplifies template development and improves developer experience.

More info:
https://gobuffalo.io/documentation/frontend-layer/helpers/#the-contentfor-and-contentof-helpers

@paganotoni paganotoni requested a review from Copilot May 23, 2025 14:57
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR improves the content rendering experience by allowing routes without a defined contentFor block to render without errors.

  • Added a nil check for the contentFor block that returns an empty HTML string instead of throwing an error.

@@ -14,6 +14,9 @@ import (
<%= contentOf("buttons", {"label": "Click me"}) %>
*/
func ContentOf(name string, data hctx.Map, help hctx.HelperContext) (template.HTML, error) {
if help.Value("contentFor:"+name) == nil {
Copy link
Preview

Copilot AI May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider caching the result of help.Value("contentFor:" + name) in a local variable to avoid duplicate lookups for better maintainability.

Copilot uses AI. Check for mistakes.

@Mido-sys
Copy link
Contributor Author

@paganotoni ,

Let's continue the discussion here. The only two failed tests are those that take a default and are undefined. But if it's defined, then it works as expected. I left the tests broken for your review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant