1

我只是停留在一点。

在 Smarty 模板中呈现表单。好吧,smarty 在我的 silex 项目中配置得很好。

这是我的控制器类中的代码。

$loginForm = $app['form.factory']
            ->createBuilder(new Form\UserLogin())
            ->getForm();

$app['smarty']->assign('loginForm', $loginForm->createView());


return $app['smarty']->render('login.tpl');

这是我的 tpl 文件中的代码

{block name="headline"}
<h1>User Login</h1>
{/block}


{block name="content"}
<div>
    {form_widget(loginForm)}
</div>
{/block}

我得到了这个例外。

SmartyCompilerException: 
Syntax Error in template "/home/Symfony/demo/App/View/login.tpl" on line 8
"{form_widget(loginForm)}" unknown function "form_widget"

编辑:

好的,我发现了问题,但没有得到解决方案。

以下是 SmartyServiceProvider 类。

<?php

namespace App\ServiceProvider;

use Silex\Application;
use \Symfony\Component\HttpFoundation\Request;
use Silex\ServiceProviderInterface;
use App\Classes\Smarty;
use NoiseLabs\Bundle\SmartyBundle\Form\SmartyRendererInterface;
use \NoiseLabs\Bundle\SmartyBundle\Extension\AbstractExtension;
use \NoiseLabs\Bundle\SmartyBundle\Extension\FormExtension;
use \NoiseLabs\Bundle\SmartyBundle\Form;

class SmartyServiceProvider implements ServiceProviderInterface
{


    public function register(Application $app)
    {
        $app['smarty.auto-render'] = true;
        $app['smarty.extension'] = $app->protect(
            function (AbstractExtension $extension, Smarty $smarty = null) use ($app) {
                if ($smarty == null) {
                    $smarty = $app['smarty'];
                }
                /** @var $plugin \NoiseLabs\Bundle\SmartyBundle\Extension\Plugin\AbstractPlugin */
                /** @var $filter \NoiseLabs\Bundle\SmartyBundle\Extension\Filter\AbstractFilter */
                foreach ($extension->getPlugins() as $plugin) {
                    //print $plugin->getName() . " | " . $plugin->getType() . "<br>";
                    $smarty->registerPlugin($plugin->getType(), $plugin->getName(), $plugin->getCallback());
                }
                foreach ($extension->getFilters() as $filter) {
                    $smarty->registerFilter($filter->getType(), $filter->getCallback());
                }
            }
        );
        $app['smarty.extensions'] = $app->protect(
            function (array $extensions, Smarty $smarty = null) use ($app) {
                foreach ($extensions as $extension) {
                    $app['smarty.extension']($extension, $smarty);
                }
            }
        );
        $app['smarty'] = $app->share(
            function () use ($app) {

                $app['directory.smarty.plugins'] = $app['directory.root.app'] . '/Classes/Smarty/Plugins';

                $smarty = isset($app['smarty.instance']) ? $app['smarty.instance'] : new Smarty(
                    $app,
                    isset($app['smarty.primary.template.dir'])
                            ? $app['smarty.primary.template.dir']
                            : $app['directory.root.view'],
                    false
                );

                if (isset($app["smarty.options"]) && is_array($app["smarty.options"])) {
                    foreach ($app["smarty.options"] as $smartyOptionName => $smartyOptionValue) {
                        $smarty->$smartyOptionName = $smartyOptionValue;
                    }
                }

                $smarty->assign("app", $app);

                if (isset($app['smarty.configure'])) {
                    $app['smarty.configure']($smarty);
                }

                $extensions = [];
                //$extensions[] = new \NoiseLabs\Bundle\SmartyBundle\Extension\FormExtension();
                $extensions[] = new \NoiseLabs\Bundle\SmartyBundle\Extension\RoutingExtension($app['url_generator']);
                $extensions[] = new \App\Classes\Smarty\HookExtension();
                $app['smarty.extensions']($extensions, $smarty);

                return $smarty;
            }
        );
    }


    public function boot(Application $app)
    {
    }


}

在那个类中,我在这里加载了 SmartyBundle 的扩展。在这里,我必须加载 FormExtensions。

$extensions = [];
$extensions[] = new \NoiseLabs\Bundle\SmartyBundle\Extension\FormExtension('Don't know how to get SmartyRendererInterface instance here');
$extensions[] = new \NoiseLabs\Bundle\SmartyBundle\Extension\RoutingExtension($app['url_generator']);
$extensions[] = new \App\Classes\Smarty\HookExtension();
$app['smarty.extensions']($extensions, $smarty);
4

1 回答 1

0

表单小部件的正确语法是:

{form_widget form=$loginForm}

从您的示例中,不清楚 $loginForm->createView() 的输出,并且您缺少通常围绕 Form 小部件的表单标签( ie )。如果 createView 包含这些,则不需要表单小部件,只需将整个表单 HTML 输出为:

{$loginForm}
于 2015-03-25T15:20:36.820 回答