2013-01-25 37 views
2

我只是停留在一個點上。如何在Smarty模板中呈現Silex表單?

在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); 
+3

'form_widget'是樹枝的模板引擎的一個宏,不是嗎?所以你可能需要一個聰明的特定功能來達到這個目的。 –

+0

@LouisH。請檢查我在我的問題中進行了一些編輯。看看SmartyBundle的表單渲染方法[鏈接](https://github.com/noiselabs/SmartyBundle/blob/master/Extension/FormExtension.php),它與樹枝提供的完全相同。 – hardik

回答

0

的表單控件正確的語法是:

{form_widget form=$loginForm} 

從你的例子目前還不清楚從$失loginForm-> CreateView的()和你缺乏表單標籤(即)通常圍繞Form小部件。如果CreateView的包括這些,你可以做出不需要表單控件,只是需要輸出整個表單HTML爲:

{$loginForm}