phpsmartyfuelphp

FuelPHP and Smarty - Variables not working


Not sure if Smarty is even loading but its showing {$title} and {$username} directly on the page and not using what's set as the variable.

I added into composer.json

"smarty/smarty": "*"

I run php composer.phar update and also install:

I'm loading in the config.php file the parser as per

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

In my controller 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;
    }

and in my index.tpl file I have

{$title} {$username}

It's just for testing, however does not seem to be working.


Solution

  • FuelPHP's Parser package handles the rendering of views using template engines.

    As you've already done, you must first enable the Parser package in fuel/app/config.php by making sure the parser package is added to always_load

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

    Parser uses the file's extension to determine which parser engine to use. In your case your file, dashboard/index.tpl uses a typical smarty extension .tpl, however FuelPHP doesn't have a template registered for that extension.

    FuelPHP uses .smarty by default.

    So, you have 2 options.

    1. Change your template's file extension, adhering to the FuelPHP default
    2. Change FuelPHP's configuration to use Smarty for .tpl files

    Fortunately both are easy. If you choose to go with option 2, check out the default configuration definition.

    You can override the defaults using a config file located at fuel/app/config/parser.php

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