StringLoader includeblock

3 years ago

tivvit
Member | 36
+
0
-

Hi, is there any possibility to use includeblock from file with StringLoader?
i mean this:

$content = "{includeblock 'file.latte'}";
$template = $this->templateFactory->createTemplate($this);
$latte = $template->getLatte();
$latte->setLoader(new Latte\Loaders\StringLoader());
$out = $latte->renderToString($content, array());

3 years ago

CZechBoY
Member | 3211
+
0
-

Why you can't render that template file file.latte?
I think that include another file is impossible without absolute path.

3 years ago

tivvit
Member | 36
+
0
-

The example is simplified. Let's say that file.latte is some static menu, and I want to use that menu block in pages loaded from database, but not in all of them and I was lazy to do another template for them.
I tried absolute path, with interesting result: the path is printed, but the block is not loaded

3 years ago

Machy8
Member | 59
+
0
-

Have you tried some conditions in template?

{if $page === "somePage"}
    <div id=myMenu ></div>
{/if}

Way using loader (not shure if it is the best):

class BasePresenter extends \Nette\Application\UI\Presenter
    protected function createTemplate()
    {
        $template = parent::createTemplate();
        $template->getLatte()->setLoader(new \Path\To\Loader($this));
        return $template;
    }

class YourLoader extends \Latte\Loaders\FileLoader
{
    public function getContent ($file)
    {
        $content = parent::getContent($file);
        // Some process here - remove block (maybe regular expression?)
        // Return modified file content without the block you want to remove
        return $compiled;
    }
}

Last edited by Machy8 (2015-12-13 12:30)