【问题标题】:FuelPHP and Smarty - Variables not workingFuelPHP 和 Smarty - 变量不起作用
【发布时间】:2016-07-22 13:53:38
【问题描述】:

不确定 Smarty 是否正在加载,但它直接在页面上显示 {$title} 和 {$username} 而不是使用设置为变量的内容。

我添加到composer.json中

“聪明/聪明”:“*”

我运行php composer.phar updateinstall

我正在根据解析器在 config.php 文件中加载

'packages'  => array(
  'orm',
  'auth',
  'parser',
),

在我的控制器dashboard.php中

public function action_index()
    {
        $data = [
        'bodyclass' => "dashboard",
        'title' => "Dashboard",
        'username' => "James"
    ];
        $view = Response::forge(View::forge('dashboard/index.tpl', $data));

        $this->template->subnav = array('dashboard'=> 'active' );
        $this->template->content = $view;
    }

在我的 index.tpl 文件中有

{$title} {$username}

这只是为了测试,但似乎不起作用。

【问题讨论】:

  • 抱歉回复晚了,刚看到。除了您不应该将视图包装到响应中这一事实之外,我没有立即看到任何错误。您确定双括号用作分隔符,而不是单括号(这是默认设置,请参阅解析器包中的 parser.php 配置文件)。

标签: php smarty fuelphp


【解决方案1】:

FuelPHP 的 Parser 包使用模板引擎处理视图的呈现。

正如您已经完成的那样,您必须首先启用 fuel/app/config.php 中的 Parser 包,方法是确保将解析器包添加到 always_load

'always_load' => array(
    'packages' => array(
        'parser',
    ),
),

解析器使用文件的扩展名来确定要使用的解析器引擎。在您的情况下,您的文件 dashboard/index.tpl 使用典型的智能扩展 .tpl,但是 FuelPHP 没有为该扩展注册模板。

FuelPHP 默认使用.smarty

所以,你有两个选择。

  1. 更改模板的文件扩展名,遵循 FuelPHP 默认设置
  2. 更改 FuelPHP 的配置以将 Smarty 用于 .tpl 文件

幸运的是,两者都很容易。如果您选择使用选项 2,请查看default configuration definition

您可以使用位于fuel/app/config/parser.php 的配置文件覆盖默认值

return array(

    // Overrides default smarty extension
    'extensions' => array(
        'tpl' => 'View_Smarty',
    )
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-05
    相关资源
    最近更新 更多