问题
Is it possible to make a standalone form with symfony3 and twig? I can't get past this error:
Fatal error: Uncaught exception 'Twig_Error_Syntax' with message 'Unknown "form_start" function in "new.html.twig" at line 1
The 3.1 documentation cites this example, which works fine, but it's actually using 2.7
My simple project is organized like this:
.
├── 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"
}
}
Full Error Stack Trace
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->getFunctionNodeClass('form_start', 1)
#1 /var/www/a/forms/vendor/twig/twig/lib/Twig/ExpressionParser.php(144): Twig_ExpressionParser->getFunctionNode('form_start', 1)
#2 /var/www/a/forms/vendor/twig/twig/lib/Twig/ExpressionParser.php(84): Twig_ExpressionParser->parsePrimaryExpression()
#3 /var/www/a/forms/vendor/twig/twig/lib/Twig/ExpressionParser.php(41): Twig_ExpressionParser->getPrimary()
#4 /var/www/a/forms/vendor/twig/twig/lib/Twig/Parser.php(144): Twig_ExpressionParser->parseExpression()
#5 /var/www/a/forms/vendor/twig/twig/lib/Twig/Parser.php(100): Twig_Parser->subparse(NULL, false)
#6 /var/www/a/forms/vendor/twig/twig/lib/Twig/Environment.php(619): Twig_Parser->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
回答1:
Have you tried, as per the exemple you provided, to add the extension FormExtension
to Twig_Environment
?
Add the following snippet right after you've initialized $twig
and $formEngine
variables:
$twig->addExtension(
new FormExtension(new TwigRenderer($formEngine))
);
You'll then get an error about trans()
being unknown. Correct this by adding symfony/translation
package : $ composer require symfony/translation
.
Then let Twig know about this new extension in the same way as above:
$twig->addExtension(
new TranslationExtension(new Translator('en'))
);
Don't forget the uses:
use Symfony\Bridge\Twig\Extension\TranslationExtension;
use Symfony\Component\Translation\Translator;
来源:https://stackoverflow.com/questions/38163866/twig-error-syntax-with-message-unknown-form-start-function