gpt4 book ai didi

php - 'Twig_Error_Syntax' 消息未知 "form_start"函数

转载 作者:可可西里 更新时间:2023-11-01 00:57:12 27 4
gpt4 key购买 nike

是否可以使用 symfony3 和 twig 制作一个独立表单?我无法克服这个错误:

Fatal error: Uncaught exception 'Twig_Error_Syntax' with message 'Unknown "form_start" function in "new.html.twig" at line 1

3.1 documentation引用 this example ,效果很好,但实际上使用的是 2.7

screenshot of example

我的简单项目是这样组织的:

.
├── composer.json
├── composer.lock
├── src
│   └── form.php
├── vendor
│   └── ...
└── views
└── new.html.twig

form.php

<?php
require_once __DIR__.'/../vendor/autoload.php';

use Symfony\Component\Form\Forms;
use Symfony\Bridge\Twig\Extension\FormExtension;
use Symfony\Bridge\Twig\Form\TwigRenderer;
use Symfony\Bridge\Twig\Form\TwigRendererEngine;

// the Twig file that holds all the default markup for rendering forms
// this file comes with TwigBridge
$defaultFormTheme = 'form_div_layout.html.twig';

$vendorDir = realpath(__DIR__.'/../vendor');
// the path to TwigBridge library so Twig can locate the
// form_div_layout.html.twig file
$appVariableReflection = new \ReflectionClass('\Symfony\Bridge\Twig\AppVariable');
$vendorTwigBridgeDir = dirname($appVariableReflection->getFileName());
// the path to your other templates
$viewsDir = realpath(__DIR__.'/../views');

$twig = new Twig_Environment(new Twig_Loader_Filesystem(array(
$viewsDir,
$vendorTwigBridgeDir.'/Resources/views/Form',
)));
$formEngine = new TwigRendererEngine(array($defaultFormTheme));
$formEngine->setEnvironment($twig);

// create your form factory as normal
$formFactory = Forms::createFormFactoryBuilder()
->getFormFactory();

use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\DateType;

$form = $formFactory->createBuilder()
->add('task', TextType::class)
->add('dueDate', DateType::class)
->getForm();

var_dump($twig->render('new.html.twig', array(
'form' => $form->createView(),
)));

new.html.twig

{{ form_start(form) }}
{{ form_widget(form) }}

<input type="submit" />
{{ form_end(form) }}

composer.json

{
"require": {
"symfony/form": "^3.1",
"symfony/twig-bridge": "^3.1",
"twig/twig": "^1.24"
}
}

完整的错误堆栈跟踪

Fatal error: Uncaught exception 'Twig_Error_Syntax' with message 'Unknown "form_start" function in "new.html.twig" at line 1.' in /var/www/a/forms/vendor/twig/twig/lib/Twig/ExpressionParser.php:574
Stack trace:
#0 /var/www/a/forms/vendor/twig/twig/lib/Twig/ExpressionParser.php(351): Twig_ExpressionParser-&gt;getFunctionNodeClass('form_start', 1)
#1 /var/www/a/forms/vendor/twig/twig/lib/Twig/ExpressionParser.php(144): Twig_ExpressionParser-&gt;getFunctionNode('form_start', 1)
#2 /var/www/a/forms/vendor/twig/twig/lib/Twig/ExpressionParser.php(84): Twig_ExpressionParser-&gt;parsePrimaryExpression()
#3 /var/www/a/forms/vendor/twig/twig/lib/Twig/ExpressionParser.php(41): Twig_ExpressionParser-&gt;getPrimary()
#4 /var/www/a/forms/vendor/twig/twig/lib/Twig/Parser.php(144): Twig_ExpressionParser-&gt;parseExpression()
#5 /var/www/a/forms/vendor/twig/twig/lib/Twig/Parser.php(100): Twig_Parser-&gt;subparse(NULL, false)
#6 /var/www/a/forms/vendor/twig/twig/lib/Twig/Environment.php(619): Twig_Parser-&gt;parse(Object(Twig_TokenStream))
#7 /var/www/a/forms/ in /var/www/a/forms/vendor/twig/twig/lib/Twig/ExpressionParser.php</b> on line 574

最佳答案

你试过了吗,as per the exemple you provided , 添加扩展 FormExtensionTwig_Environment?

在初始化 $twig$formEngine 变量后立即添加以下代码段:

$twig->addExtension(
new FormExtension(new TwigRenderer($formEngine))
);

然后您将收到有关 trans() 未知的错误。通过添加 symfony/translation 包更正此问题:$ composer require symfony/translation

然后像上面一样让Twig知道这个新的扩展:

$twig->addExtension(
new TranslationExtension(new Translator('en'))
);

不要忘记用途:

use Symfony\Bridge\Twig\Extension\TranslationExtension;
use Symfony\Component\Translation\Translator;

关于php - 'Twig_Error_Syntax' 消息未知 "form_start"函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38163866/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com