-
-
Notifications
You must be signed in to change notification settings - Fork 5
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
Add _includes directory for quickly adding automatic snippets #29
Comments
|
Remembered that I added the feature to not render files starting with _underscores for just this reason a long time ago. For example: Benefit: we don't need an extra directory, drawback: includes would be specific for that page type. |
I do like this idea, but I also don't want a bunch of bloated directories. Could be something to integrate with data collections? Shelving for now, would love to get some feedback on this. |
The complexity this adds should be weighed against the relative ease of publishing or modifying components in the resources/views directory |
Created this class, which works pretty nicely, but I'm not sure if it adds enough value to be justified. Going to close this for now. class RendersIncludedFile
{
public static string $includesPath = '_includes';
public static function render(string $file, ?string $default = null): string
{
$filepath = Hyde::path(static::$includesPath . '/' . $file);
if (! file_exists($filepath)) {
if ($default === null) {
throw new \Exception("Included file '$filepath' does not exist, and no default value was provided.");
}
return $default;
}
$extension = pathinfo($filepath, PATHINFO_EXTENSION);
return match ($extension) {
'md' => MarkdownConverter::parse(file_get_contents($filepath)),
'blade.php' => Blade::render($filepath),
'php' => include $filepath,
'html' => file_get_contents($filepath),
'css' => '<style>' . file_get_contents($filepath) . '</style>',
'js' => '<script>' . file_get_contents($filepath) . '</script>',
default => throw new \Exception("Unsupported file extension for includes: $extension"),
};
}
} Which can then be used in Blade views through the Hyde facade class Hyde
{
public static function include(string $file, ?string $default = null): string
{
return RendersIncludedFile::render($file, $default);
}
} |
Thinking about adding sometime simple in the resources directory. Starting small, and we can expand from there. |
Okay, I think this contract could be nice. It's rather simple and doesn't have any of the magic the previous implementation had. A interface IncludeFacadeContract
{
/**
* Get the raw contents of a partial file in the includes directory.
*
* @param string $partial The name of the partial file, including the extension.
* @param string|null $default The default value to return if the partial is not found.
* @return string|null The contents of the partial file, or the default value if not found.
*/
public static function get(string $partial, ?string $default = null): ?string;
/**
* Get the rendered Markdown of a partial file in the includes directory.
*
* @param string $partial The name of the partial file, without the extension.
* @param string|null $default The default value to return if the partial is not found.
* @return string|null The contents of the partial file, or the default value if not found.
*/
public static function markdown(string $partial, ?string $default = null): ?string;
/**
* Get the rendered Blade of a partial file in the includes directory.
*
* @param string $partial The name of the partial file, without the extension.
* @param string|null $default The default value to return if the partial is not found.
* @return string|null The contents of the partial file, or the default value if not found.
*/
public static function blade(string $partial, ?string $default = null): ?string;
} |
Could be neat with an Includes::code() helper to render Torchlight blocks? |
The way I'm thinking is to have a directory where you can just put partials to get them automatically included. For example, want to add some stuff to the
<head>
section? Just create a file_includes/_head.html/md/blade.php
. The contents will be rendered and/or injected before the end of the closing</head>
tag.The text was updated successfully, but these errors were encountered: