-
-
Notifications
You must be signed in to change notification settings - Fork 6
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
RFC: Should Includes helpers returning Html return HtmlStrings? #1734
Comments
GPT-4o ResponseFeedback on RFC: Should Includes Helpers Returning Html Return HtmlStrings?SummaryThe proposal suggests modifying the return type of HTML content from plain strings to Points in Favor
Points of Concern
Recommendations
Proposed Changes to the ClassHere’s an example of how the use Illuminate\Support\HtmlString;
class Includes
{
// ... other methods remain unchanged
/**
* Get the HTML contents of a partial file in the includes directory.
*
* @param string $filename The name of the partial file, with or without the extension.
* @param string|null $default The default value to return if the partial is not found.
* @return HtmlString|null The raw contents of the partial file, or the default value if not found.
*/
public static function html(string $filename, ?string $default = null): ?HtmlString
{
$path = static::path(basename($filename, '.html').'.html');
if (! file_exists($path)) {
return $default === null ? null : new HtmlString($default);
}
return new HtmlString(file_get_contents($path));
}
/**
* Get the rendered Markdown of a partial file in the includes directory.
*
* @param string $filename The name of the partial file, with or without the extension.
* @param string|null $default The default value to return if the partial is not found.
* @return HtmlString|null The rendered contents of the partial file, or the default value if not found.
*/
public static function markdown(string $filename, ?string $default = null): ?HtmlString
{
$path = static::path(basename($filename, '.md').'.md');
if (! file_exists($path)) {
return $default === null ? null : new HtmlString(Markdown::render($default));
}
return new HtmlString(Markdown::render(file_get_contents($path)));
}
/**
* Get the rendered Blade of a partial file in the includes directory.
*
* @param string $filename The name of the partial file, with or without the extension.
* @param string|null $default The default value to return if the partial is not found.
* @return HtmlString|null The rendered contents of the partial file, or the default value if not found.
*/
public static function blade(string $filename, ?string $default = null): ?HtmlString
{
$path = static::path(basename($filename, '.blade.php').'.blade.php');
if (! file_exists($path)) {
return $default === null ? null : new HtmlString(Blade::render($default));
}
return new HtmlString(Blade::render(file_get_contents($path)));
}
} This change ensures that the methods return |
If we move forward, the upgrade path should have instructions for how current usages can be simplified ( |
Considering that the use case for this is often to include HTML partials (literal, Markdown, or Blade), I think it makes sense for this to be HtmlStrings so they can be rendered without having to use |
Implemented in #1738 |
We have this class, where we document that "Both Markdown and Blade includes will be rendered to HTML". Wouldn't it then make more sense if these returned
HtmlString
objects instead ofstring
types?This is more semantic, but has the side effect that they will no longer be escaped.
Considering the usages though, I imagine that these are already being used within literal echo directives
{!! !!}
. It could be a breaking change, and it needs to be communicated clearly, so possibly something for v2.x?The text was updated successfully, but these errors were encountered: