Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
FuelPHP and Smarty - Variables not working
  • Not sure if samrty is even loading but its showing {$title} and {$username} directly on the page and not using whats set as the variable... little lost to what im doing wrong.

    I added into composer.json

    "smarty/smarty": "*"

    I run php composer.phar update and also install

    Im 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}

    Its just for testing.. however does not seem to be working and a little lost as new to php and this framework

    Hope someone can help me :)

    Thanks

  • You are using the wrong extension. By default, "tpl" is reserved for the Dwoo template engine.

    To address that, create a parser.php config file, and add:

    return array(

        'extensions' => array(
            'dwoo'     => 'View_Dwoo',
            'smarty'   => array('class' => 'View_Smarty', 'extension' => 'tpl'),
        ),
    );

    You are using Fuel v1.8.0+ ? Using Composer to install Smarty wasn't supported in older versions.
  • I added it to:

    fuel/packages/parser/config/parser.php but still does nto work, im new to using FuelPHP or do i need to place this somewhere else? also created parser.php in config folder just in case

    And yes it is using 1.8

    Thanks for your help, need to demo this later so dying to get it sorted this morning :)
  • It is pulling the templates btw, just the variables are not working... even {asset_css etc..

    Im also using in template.tpl {$content} to get the page pulled in, is this correct?

    Im loading like...

    class Controller_Dashboard extends Controller_Template
    {

    public $template = 'template.tpl';
  • Ah,ok.

    Have you made any changes to the rest of the default config?

        // SMARTY ( http://www.smarty.net/documentation )
        // ------------------------------------------------------------------------
        'View_Smarty' => array(
            'auto_encode' => true,
            'delimiters'  => array('left' => '{', 'right' => '}'),
            'environment' => array(
                'compile_dir'       => APPPATH.'tmp'.DS.'Smarty'.DS.'templates_c'.DS,
                'config_dir'        => APPPATH.'tmp'.DS.'Smarty'.DS.'configs'.DS,
                'cache_dir'         => APPPATH.'cache'.DS.'Smarty'.DS,
                'plugins_dir'       => array(),
                'caching'           => false,
                'cache_lifetime'    => 0,
                'force_compile'     => false,
                'compile_check'     => true,
                'debugging'         => false,
                'autoload_filters'  => array(),
                'default_modifiers' => array(),
            ),
        ),


  • And do internal variables work, like {$smarty.now}? i.e. is it a passing of data problem or a Smarty parsing problem?
  • Nope, just checked looks the same as the one you mentioned above and i added to template.tpl {$smarty.now} and it just showed {$smarty.now} so maybe smarty is not working? ive tried update and install again but done nothing

    php composer.phar install
    You are running composer with xdebug enabled. This has a major impact on runtime performance. See https://getcomposer.org/xdebug
    Loading composer repositories with package information
    Installing dependencies (including require-dev) from lock file
    Nothing to install or update
    Generating autoload files
    > php oil r install
            Made writable: C:\Websites\Annilla\Clients\Four04 Packaging\DEV\NEW\BUILD\fuel\app\cache
            Made writable: C:\Websites\Annilla\Clients\Four04 Packaging\DEV\NEW\BUILD\fuel\app\logs
            Made writable: C:\Websites\Annilla\Clients\Four04 Packaging\DEV\NEW\BUILD\fuel\app\tmp
            Made writable: C:\Websites\Annilla\Clients\Four04 Packaging\DEV\NEW\BUILD\fuel\app\config

  • Ok, so it doesn't seem like it is parsing the templates at all.

    It has been years since I've used Smarty, and never used v3. I need to setup a test environment to see if I can reproduce it.
  • Yep seems that way it seems, thanks for your help! hope we can get this sorted and working as got a meeting this afternoon :) is it maybe worth using another parser if we cant get smarty working.
  • Ive just got to pop to a meeting, ill be back in 45 mins mate. again thanks for all your help
  • Ok.

    Setup a new test site using the current develop version (shouldn't make a difference compared to your 1.8 in this case). Installed Smarty using "composer require smarty/smarty". Changed the action_index() of the Welcome controller to load the "welcome/index.smarty" view. Copied the welcome/index.php to welcome/index.smarty, and made it compatible like so: http://bin.fuelphp.com/snippet/view/Nf

    After I done this, I got an error about "asset_css" not being known. This turned out to be an omission in the default parser config, that doesn't load the extension class. This has been adressed in https://github.com/fuel/parser/commit/262378f93def45ffd7cdde0c1ff6dee873f5979e. After this fix, the template parsed correctly, including {$smarty.now}.

  • Getting a few error but think it is the template but using .smarty seems to have done the trick... :) Thanks for all your help mate
  • Onlt issue i am facing is the variables are not passing, am i doing those correctly? or should i be doing a different way?
  • I guess i need to set global variables to use in template.smarty and ones to use in the pages. what is the best way to do this? and thanks again for all your time so far.
  • No, that should be ok.

    Let me see why it doesn't want to use the tpl extension, and if I have a problem passing variables too.
  • Seems i got them working, needed to pass to template the global variables and in that data array local to the page content. If can let me know on the TPL thing would be awesome!
  • Fixed the extension overload problem: https://github.com/fuel/parser/commit/de6416ede782627621f10e11d2a202dc949298a2

    As to the variables, I changed my first test to

    return Response::forge(View::forge('welcome/index.tpl', array('name' => 'TEST')));

    and that displays "TEST" without problems when I use {$name} in the template.
  • Ahh great ill have a look now thanks mate, last quick one i promise :) you know how i can chnage the loading of images now?

    <?php echo Asset::img('users/james.jpg', 'img-circle img-responsive'); ?>

    of course does not work in template, ill try the .tpl thing now though :)
  • Variables are always local to the view, there is no automatic inheritance of data between views, unless you use global view variables which are available in all views, but that is something that should be avoided if possible.

  • Ok no worries, ill do as you suggest :) btw that fix for the tpl worked great! thanks for that mate.
  • You're welcome.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion