Can a missing component in presenter-latte not throw exception

materix
Backer | 83
+
0
-

Is it possible to “mute” the exception that is thrown, if a control-tag exists in .latte-template, for a component that has not yet been added to the presenter?

e.g.
{control poll}

Or must I wrap the control in some conditional check?

Last edited by materix (2024-05-19 19:54)

Marek Bartoš
Nette Blogger | 1275
+
+2
-

If your intention is not to render component in case it does not exist, then check its existence with {if isset($control['pool'])}

materix
Backer | 83
+
0
-

Thanks for the reply.

I add the component in the presenter by using:

$this->addComponent($this->myControlFactory->create(), "mycontrol1");

and then in the template I do:

{if ($presenter->getComponent('mycontrol1', false))}
{control mycontrol1}
{/if}

When there are a lot of controls, the latte-files get a bit cluttered with all the conditionals. But I guess there is no other way?

Also I am wondering if there is some method in the presenter to set, in which order the components are displayed?

E.g.

{control1}
{control2}
{control3}

-->

{control2}
{control3}
{control1}

nightfish
Member | 518
+
+1
-

@materix wrote:
When there are a lot of controls, the latte-files get a bit cluttered with all the conditionals. But I guess there is no other way?

You could probably implement your own macro {controlIfExists}, based on existing {control} macro.
Or, maybe, if you render controls dynamically (for example when rendering widgets on a dashboard, when the set of visible widgets depends on user's settings), it would be easier to prepare a list of available components in your presenter and then pass this list to your template.

Also I am wondering if there is some method in the presenter to set, in which order the components are displayed?

E.g.

{control1}
{control2}
{control3}

vs.

{control2}
{control3}
{control1}

The components are displayed (“rendered”) in the same order that you “call” them ({control control1}) in your Latte files.

m.brecher
Generous Backer | 873
+
-3
-

@materix

Is it possible to “mute” the exception that is thrown, if a control-tag exists in .latte-template, for a component that has not yet been added to the presenter?

consider also this way:

{try}
    {control 'myComponent'}
{/try}
Marek Bartoš
Nette Blogger | 1275
+
+2
-

@mbrecher And ignore any exception that could happen in the component?
No.