CodeIgniter4 icon indicating copy to clipboard operation
CodeIgniter4 copied to clipboard

feat(view): add default value support for renderSection()

Open gilberthtg opened this issue 3 months ago • 3 comments

This commit extends the View::renderSection() method to allow an optional default value parameter when a section is not found or is empty.

  • Added a new $defaultContent argument to renderSection()
  • If the requested section is missing or empty, the method will return the provided default value instead of an empty string
  • Preserves existing behavior when $defaultContent is not set

This improvement makes it easier to provide fallback content in layouts without requiring additional wrapper logic in user applications.

Example usage: = $this->renderSection('example', false, '

Default form content

') ?>

Checklist:

  • [ ] Securely signed commits
  • [ ] Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • [ ] Unit testing, with >80% coverage
  • [ ] User guide updated
  • [ ] Conforms to style guide

gilberthtg avatar Oct 01 '25 09:10 gilberthtg

Hi there, gilberthtg! :wave:

Thank you for sending this PR!

We expect the following in all Pull Requests (PRs).

[!IMPORTANT] We expect all code changes or bug-fixes to be accompanied by one or more tests added to our test suite to prove the code works.

If pull requests do not comply with the above, they will likely be closed. Since we are a team of volunteers, we don't have any more time to work on the framework than you do. Please make it as painless for your contributions to be included as possible.

See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/pull_request.md

Sincerely, the mergeable bot 🤖

mergeable[bot] avatar Oct 01 '25 09:10 mergeable[bot]

While I understand what this change does, I'm not sure in which situations it would be useful. Let's see what others think.

@gilberthtg Please follow the mergeable bot instructions.

  • New features should go to the 4.7 branch.
  • We need tests for this
  • We need an updated user guide, so that this will not become a secret feature
  • We need a changelog entry

michalsn avatar Oct 01 '25 11:10 michalsn

Is it much better than the condition?

<?php $this->renderSection('base') ?? 'Default HTML' ?>

The question is interesting. But the solution to insert a backup HTML should look different. My opinion. The problem is the poor readability of the default code when it has logic or contains a large amount of code.

neznaika0 avatar Oct 01 '25 13:10 neznaika0