Search extension latte filters

Jan Kotalík
Member | 16
+
0
-

Hello,
I absolutely love the search extension for Nette 3.0.

I just wonder – is somehow possible to automatically register custom Latte filters as well?
For example, I have some text representations of database constants and I use it like this:

{$constant|textVersion}

and in config:

- addFilter(textVersion, [App\Constants\TextVersion, 'getLabel'])

I would like to have something like “All classes which extends the Constant class should have filter with its own name”.

Is there some best practice for that?

Thanks.

dkorpar
Backer | 96
+
+1
-

I don't think you can do this with search extension…
You're probably better in writing your own extension for that…
Although, Constant class doesn't sound like something that is registered to DIC
I think you could do this in Extension with a bit of reflection, but question is to generic, if U could provide more code and what constant classes are actually…

Jan Kotalík
Member | 16
+
0
-

Example:

class State extends ConstantList
{


	const OPENED = 'opened';
	const DELAYED = 'delayed';
	const CLOSED = 'closed';

	static function getLabel(string $constant): string
	{
		switch ($constant) {
			case self::OPENED:
				return 'otevřený';
			case self::DELAYED:
				return 'zdržený';
			case self::CLOSED:
				return 'zavřený';
		}
		return '';
	}


}

Usage:

$this->entityManager->getRepository(Task::class)->findBy(['state' => State::OPENED]);

config

- addFilter(taskState, [State, 'getLabel'])

Latte for user:

<span class="label">{$task->getState()|taskState}</span>

Would it be a bad practise if I would dynamically load constants with Finder in every request? Doesn't sounds very efficient to me.

dkorpar
Backer | 96
+
+3
-

Why not solving this with translator?

Jan Kotalík
Member | 16
+
0
-

dkorpar wrote:

Why not solving this with translator?

I never tought of that. Thanks, this is what i needed :)