呈现表单时未找到块 "form"。在单独的 Symfony App 外部控制器中 class

No block "form" found while rendering the form. in Symfony App outside controller in separate class

我尝试创建某种形式生成器,它使用 symfony 表单扩展输出 html 生成的表单(使用所有好的东西,如验证、错误突出显示等)。

使用 symfony 5 和 twig 3.0

已创建class

class FormBuilder{
    private $twig;
    private $formFactory;

    public function __construct()
    {
        $defaultFormTheme = 'contact.html.twig';
    
        $loader = new FilesystemLoader($_SERVER['DOCUMENT_ROOT'].'../template/forms');
        $this->twig = new Environment($loader, ['cache' => $_SERVER['DOCUMENT_ROOT'].'../var/cache/'.$_ENV['APP_ENV'].'/twig']);
    
        $formEngine = new TwigRendererEngine([$defaultFormTheme], $this->twig);

        $this->twig->addRuntimeLoader(new FactoryRuntimeLoader([
            FormRenderer::class => function () use ($formEngine) {
                return new FormRenderer($formEngine);
            },
        ]));
        $this->twig->addExtension(new FormExtension());

        $this->formFactory = Forms::createFormFactoryBuilder()
            ->getFormFactory();
    }

    /**
     * @return string
     */
    public function getContactForm():string
    {
        $form = $this->formFactory->createBuilder()
            ->add('content', TextareaType::class)
            ->getForm();

        return $this->twig->render('contact.html.twig', [
            'contact_form' => $form->createView(),
        ]);
    }
}

并用

在其他地方调用它
$fb = new FormBuilder();
var_dump($fb->getContactForm());

模版长什么样都无所谓

{{ form_start(contact_form) }}
    {{ form_widget(contact_form) }}
    <input type="submit"/>
{{ form_end(contact_form) }}

{{ form(contact_form) }}

始终存在运行时错误:

呈现模板期间抛出异常(“呈现表单时未找到块“表单”。”)。

或者在第一个模板示例中,它大喊“form_start”而不是“form”。

现在已经搜索了几个小时,但我似乎无法找到丢失的位置.. 关于如何在 symfony 控制器之外的 twig 中包含表单函数有什么建议或技巧吗?

添加主题和(由于主题中的翻译)翻译现在可以使用

public function __construct()
{
    // name of theme template
    $defaultFormTheme = 'form_div_layout.html.twig';

    $appVariableReflection = new \ReflectionClass('\Symfony\Bridge\Twig\AppVariable');
    $vendorTwigBridgeDirectory = dirname($appVariableReflection->getFileName());

    //where the forms reside
    $viewsDirectory = realpath($_SERVER['DOCUMENT_ROOT'].'../template/forms');
    
    //init twig with directories
    $this->twig = new Environment(new FilesystemLoader([
        $viewsDirectory,
        $vendorTwigBridgeDirectory.'/Resources/views/Form',
    ]));
    
    //apply theme to twig/renderer
    $formEngine = new TwigRendererEngine([$defaultFormTheme], $this->twig);

    $this->twig->addRuntimeLoader(new FactoryRuntimeLoader([
        FormRenderer::class => function () use ($formEngine) {
            return new FormRenderer($formEngine);
        },
    ]));
    
    //add form extenstion
    $this->twig->addExtension(new FormExtension());

    // this is for the filter |trans
    $filter = new TwigFilter('trans', function ($context, $string) {
        return Translation::TransGetText($string, $context);
    }, ['needs_context' => true]);

    // load the i18n extension for using the translation tag for twig
    // {% trans %}my string{% endtrans %}
    $this->twig->addFilter($filter);
    $this->twig->addExtension(new Translation());

    //prepare factory for later use
    $this->formFactory = Forms::createFormFactoryBuilder()
        ->getFormFactory();
}

只需要额外的 php 扩展 'gettext' 和来自 https://github.com/JBlond/twig-trans 的 twig 的翻译扩展(原文似乎只支持 v2.x 之前的 twig)

再次感谢 Jakumi 的解决提示:)