including *.latte templates without caring the directory structure

medhi
Bronze Partner | 202
+
-2
-

This is ugly:

{include '../../../menu.latte'}
{* or *}
{embed '../../../menu.latte'}{/embed}

It's so annoying to search how deep in my directory structure I am, and it should not matter, why should I care?
Also, if the parent template is moved upper or lower, these paths stop working.

How about to put all these templates – that are used only to be included – to some predefined directory and then include them without caring about the directory structure?

Last edited by medhi (2020-11-06 22:52)

Rick Strafy
Nette Blogger | 6
+
0
-

So you want all templates that you include somewhere in one directory? It's sounds like very bad joke, projects with folders like Includes or Utils are considered as those with design flaws where author didn't make effort to make better project structure, but it's not always true, usually it's better use something like Article/@Common to have common templates that have to do something with articles.

But on the other hand, it would be cool to define template root, for instance my template root would be here https://github.com/…%40Templates so I could use {embed '/@Layout/Embeds/menu.latte'} anywhere, but until now I didn't have problem with paths, it's very rare when I move latte file to other directory.

Last edited by Rick Strafy (2020-11-06 23:13)

medhi
Bronze Partner | 202
+
0
-

I was not proposing any particular solution, but just a general idea to have more pleasant way to include a template.

Now I have something like this in my code:

{embed '../../../../templates/infobox.latte'}
...
{/embed}

Which is… difficult to read at least?

Absolute paths? Maybe, why not :)

dsar
Backer | 7
+
+1
-

IMHO it's not the nette-way.

Why not a menu component that render its own latte placed in the same folder?

Then:

{control menu}

Maybe it's overkilling for just a static menu, but one day you would need more control over it (such as ACL, database stuff, etc…)

medhi
Bronze Partner | 202
+
0
-

I am afraid the Control can't be used for my use case, which is to embed a modal window with a full html body, that is different for every use :( You won't pass a big chunk of HTML/latte into a Control.

filsedla
Member | 91
+
+2
-

You can actually define a constant in app/bootstrap.php

define('TEMPLATE_ROOT', __DIR__ . '/templates/@common');

and then in any template use

{include TEMPLATE_ROOT . '/subdir/someRepeatingWidget.latte'}
Šaman
Member | 2467
+
0
-

medhi wrote:

You won't pass a big chunk of HTML/latte into a Control.

Why? Control has own independent template. Even Presenter is Control.

David Grudl
Nette Core | 7250
+
+2
-

You can set your own root for the Latte and then all “absolute” paths will be relative to this root:

		$this->template->getLatte()->setLoader(
			new Latte\Loaders\FileLoader(__DIR__ . '/templates')
		);

The problem is that Presenter is not ready for this and use absolute paths in methods like formatTemplateFiles(). The easiest solution is therefore to use a variable $root or as he wrote @filsedla define constant.

medhi
Bronze Partner | 202
+
0
-

Šaman wrote:

medhi wrote:

You won't pass a big chunk of HTML/latte into a Control.

Why? Control has own independent template. Even Presenter is Control.

Because I need to pass different big chunks of HTML. A modal for example. It has its own template, but always has different body, which is not suitable for control to pass in.