Skip to content
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

Closed
caendesilva opened this issue Jun 14, 2022 · 8 comments · Fixed by #245
Closed

Add _includes directory for quickly adding automatic snippets #29

caendesilva opened this issue Jun 14, 2022 · 8 comments · Fixed by #245

Comments

@caendesilva
Copy link
Member

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.

@caendesilva
Copy link
Member Author

caendesilva commented Jun 18, 2022

I might move the "Footer Text" setting to a Markdown/Blade include in an _includes directory

hydephp/framework#442 (comment)

@caendesilva
Copy link
Member Author

Remembered that I added the feature to not render files starting with _underscores for just this reason a long time ago. For example: pages/_footer.md

Benefit: we don't need an extra directory, drawback: includes would be specific for that page type.

@caendesilva
Copy link
Member Author

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.

@caendesilva caendesilva added the shelved-for-the-future Good ideas to revisit, but are not currently a priority. label Jul 2, 2022
@caendesilva
Copy link
Member Author

The complexity this adds should be weighed against the relative ease of publishing or modifying components in the resources/views directory

@caendesilva
Copy link
Member Author

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);
    }
}

@caendesilva caendesilva closed this as not planned Won't fix, can't repro, duplicate, stale Jul 16, 2022
@caendesilva
Copy link
Member Author

Thinking about adding sometime simple in the resources directory. Starting small, and we can expand from there.

@caendesilva caendesilva reopened this Jul 18, 2022
@caendesilva
Copy link
Member Author

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 Hyde::include() helper could use the automatic filetype detection.

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;
}

@caendesilva
Copy link
Member Author

Could be neat with an Includes::code() helper to render Torchlight blocks?

@caendesilva caendesilva removed the shelved-for-the-future Good ideas to revisit, but are not currently a priority. label Dec 11, 2022
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 a pull request may close this issue.

1 participant