deprecated-packages / symplify

[DISCONTINUED] Check split packages in their own repositories :)
MIT License
621 stars 189 forks source link

[Statie] include a text file? #511

Closed simara-svatopluk closed 6 years ago

simara-svatopluk commented 6 years ago

Hi,

is it possible to include a content of any file into a text file?

I'm writing a post to pehapkari.cz that will be full of code. I would like to write code and tests only once - in separate files - and then include them into an article.

TomasVotruba commented 6 years ago

Hi, I have no idea how would that work.

Do you have a prototype in mind?

simara-svatopluk commented 6 years ago

{includeCode "path.php"} => echo file_get_contents("path.php");

A compiled file would contain the "path.php" content.

But I don't know how the whole Statie works, so maybe this is unreal.

TomasVotruba commented 6 years ago

I see

Is that secure?

Is that possible in Latte?

If so, it can be added to Statie :)

simara-svatopluk commented 6 years ago

Well, the furthest I get is https://latte.nette.org/en/macros#toc-file-including but it produces an exception because only some templates are available because of Symplify\Statie\FlatWhite\Latte\DynamicStringLoader

at this point, I stopped and went to ask if Statie should support include or not. Maybe I should have written this in the issue description.

TomasVotruba commented 6 years ago

In short, Statie is just bit smarter Presenter you know from Nette. Nothing extra.

This is how Latte works internally in Nette application, just exposed with specific classes.


Could you share exact use in your post? Short snippet would do.

DynamicStringLoader is like native Latte\StringLoader + it allows to modify templates. That's how posts can be renderered from md + latte to html.

So when you include file somewhere:

{include "head"}

It will call $stringLoader->getContent('head').

To use this "head" file, the file has to be added first. All files from _snippets and _layouts are added by default: https://github.com/Symplify/Symplify/blob/54d0f43027cb3a8d427e19bbb93c013d7c3fb374/packages/Statie/src/Application/StatieApplication.php#L98-L108

/**
 * @param SplFileInfo[] $layoutFiles
 */
private function loadLayoutsToLatteLoader(array $layoutFiles): void
{
    foreach ($layoutFiles as $layoutFile) {
        $name = $layoutFile->getBasename('.' . $layoutFile->getExtension());
        $content = file_get_contents($layoutFile->getRealPath());
        $this->dynamicStringLoader->changeContent($name, $content);
    }
}

So all you need to do is to add files from /src with their relative paths as names. In pseudo code:

$this->dynamicStringLoader->changeContent('src/MyFile.php', file_get_contents('src/MyFile.php'));

Then you can use it in latte:

{include "src/MyFile.php"}

Which will call:

$this->dynamicStringLoader->getContent('src/MyFile.php')

resulting in calling

file_get_contents('src/MyFile.php')

Is this clear so far to you? If so, I'll suggest specific place to implement this feature

simara-svatopluk commented 6 years ago

@TomasVotruba Hm... This wasn't the right idea. Latte {include } expects and evaluates latte syntax. Problem is that the included file has php syntax.

simara-svatopluk commented 6 years ago

I don't get it... I tried to use {php echo(file_get_contents(...))} but it still doesn't work.

Ok, if I try {var $xxx = 'Y'} {$xxx} The temp compiled file contains <?php $xxx = 'Y'; ?> <?php echo LR\Filters::escapeHtmlText($xxx) and also fails if the php code has some error But the output contains literally {var $xxx = 'Y'} {$xxx}

TomasVotruba commented 6 years ago

What about using {$var|noescape} filter?

Or own filter includePhpSnippet, that would do exactly what you need?

simara-svatopluk commented 6 years ago

Nope, it ends literally with {var $xxx = 'Y'} {$xxx|noescape}. If I would eventually try custom filter, it will end up with not evaluated code :-/

In the end, I decided not to include the code in the article because it would be enormous and difficult to navigate. So there will be just a link to GitHub.

simara-svatopluk commented 6 years ago

Conclusion: It is possible to include a latte. Not any other file type.

TomasVotruba commented 6 years ago

Thanks for your time put investigation. I thought about this myself so I'm glad I know more now