Closed simara-svatopluk closed 6 years ago
Hi, I have no idea how would that work.
Do you have a prototype in mind?
{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.
I see
Is that secure?
Is that possible in Latte?
If so, it can be added to Statie :)
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.
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
@TomasVotruba
Hm... This wasn't the right idea. Latte {include }
expects and evaluates latte syntax. Problem is that the included file has php syntax.
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}
What about using {$var|noescape}
filter?
Or own filter includePhpSnippet
, that would do exactly what you need?
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.
Conclusion: It is possible to include a latte. Not any other file type.
Thanks for your time put investigation. I thought about this myself so I'm glad I know more now
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.