Extending template programatically

guilhermeaiolfi
Member | 3
+
0
-

Hi,

I am evaluating Latte and it so far so good, but there is something I can't figure it out by myself. The framework I am trying to use Latte is somehow alike Agavi Framework, the way it deals with templates is a bit different.

It defines the layout/extends outside the children templates, because I can reuse the same template in two different layouts without replicating the children template code. Because of that, I would had to define programatically the “parent”/“layout” template for the one I am compiling.

I couldn't found any direct way of doing that. In plates I can load the template and call:

$template->layout($layout);

and $template would extend $layout. Is it possible currently somehow?

David Matějka
Moderator | 6445
+
+1
-

Hi,
there is no simple API like setLayout, but you can implement it by setting “coreParentFinder” provider to Latte engine. see this test: https://github.com/…tName().phpt#L52

nanna888
Member | 1
+
0
-

ok

guilhermeaiolfi
Member | 3
+
0
-

That's seems to be exactly what I wanted. Thank you very much.

Is there a workaround to do something like that to include blocks in the template outside the .latte file? I mean, using the engine API.

For example, I have the compiled code stored in a variable and want Latte to {include } that code.

Currently I am passing that string to the params argument and echoing as {$var_containing_block_code}. It works but I would like to use the standard macro {include block_name}

guilhermeaiolfi
Member | 3
+
0
-

After taking a look at the code, it seems that it is only possible manipulating the template source text to produce what I want.

I mean, something like:

	$source = $loader->getContent();
$contentTemplateSource = {block content}' . $source . '{/block}';

Then render the formated template. Am I right? Or is there a better way?

David Grudl
Nette Core | 8218
+
0
-

I would had to define programatically the “parent”/“layout”

See https://latte.nette.org/en/develop#…

After taking a look at the code, it seems that it is only possible manipulating the template source text to produce what I want.

Hmm, I've never solved this, but (ab)using a loader seems to be the easiest way :)