Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing way to use control constants as snippet/snippetArea name #141

Closed
mstefan21 opened this issue Oct 12, 2016 · 6 comments
Closed

Missing way to use control constants as snippet/snippetArea name #141

mstefan21 opened this issue Oct 12, 2016 · 6 comments

Comments

@mstefan21
Copy link

mstefan21 commented Oct 12, 2016

We are using control constants to define control names for better orientation in the project code. Unfortunately currently there is no way to do same thing with the snippet. For example:

namespace Vendor\App;

class ExampleForm extends Nette\Application\UI\Form
{

    const CONTROL_SUBMIT = 'submit';
    const SNIPPET_FORM = 'form';

    public function __construct()
    {
        $this->addSubmit(self::CONTROL_SUBMIT, 'submit')
            ->onClick[] = array($this, 'handleSubmit');
    }

    public function handleSubmit(SubmitButton $button)
    {
        $this->onSubmit($button);
        $this->redrawControl(self::SNIPPET_FORM);
    }

}
{form $control::CONTROL_FORM}
    <div n:snippet="$form::SNIPPET_FORM">
        {input $form::CONTROL_SUBMIT}
    </div>
{/form}

or

{form \Vendor\App\ExampleControl::CONTROL_FORM}
    <div n:snippet="\Vendor\App\ExampleForm::SNIPPET_FORM">
        {input \Vendor\App\ExampleForm::CONTROL_SUBMIT}
    </div>
{/form}

This example unfortunately will throw the following exception:
Dynamic snippets are allowed only inside static snippet/snippetArea.

@TomasVotruba
Copy link
Contributor

TomasVotruba commented Oct 12, 2016

That probably means the code has to be wrapped in {snippet} macro:

{snippet}
    {form \Vendor\App\ExampleForm::CONTROL_FORM}
        <div n:snippet="$form::SNIPPET_FORM">
            {input $form::CONTROL_SUBMIT}
        </div>
    {/form}
{/snippet}

Could you try that?

@mstefan21
Copy link
Author

This was just an example. We need to redraw the specific part, not the whole control.

{form \Vendor\App\ExampleForm::CONTROL_FORM}
    <div n:snippet="$form::SNIPPET_FORM">
        {input $form::CONTROL_SUBMIT}
    </div>
    <div n:snippet="$form::SNIPPET_WE_DONT_WANT_REDRAW">
        {* some big satic data *}
    </div>
{/form}

@dg
Copy link
Member

dg commented Oct 12, 2016

It is dynamic snippet, because value of $form is unknown (ie. can be changed). Something like <div n:snippet="\Vendor\App\ExampleForm::SNIPPET_FORM"> could be maybe supported…

ping @matej21

@mstefan21
Copy link
Author

$form|$control is uknown, but we can use static constant like this, but it also doesn't work, because the \Vendor\App\ExampleControl::CONTROL_FORM is interpreted as a string instead of the constant value.

{form \Vendor\App\ExampleControl::CONTROL_FORM}
    <div n:snippet="\Vendor\App\ExampleForm::SNIPPET_FORM">
        {input \Vendor\App\ExampleForm::CONTROL_SUBMIT}
    </div>
{/form}

@dg
Copy link
Member

dg commented Nov 16, 2020

Possible solution is to pass variable $formName = Vendor\App\ExampleControl::class and then use <div n:snippet="$formName::SNIPPET_FORM">

@dg dg closed this as completed May 17, 2022
@micha-walter
Copy link

I have test this, but it's not working. Same Error as above with this tests:

<div n:snippet="\Vendor\App\ExampleForm::SNIPPET_FORM">

or

{var $formName = Vendor\App\ExampleControl::class}
<div n:snippet="$formName::SNIPPET_FORM">

My packages are:

latte/latte: v3.0.9
nette/application: v3.1.13

Is there a chance that this will be supported in the future?

I used the constants in my code to redraw the controls, so the strings of n:snippet in template and $this->getComponent(\Vendor\App\ExampleForm::SNIPPET_FORM)->redrawControl() in Presenter should always the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants