Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Parser package - share views between Javascript and PHP
  • Hi everyone,

    have a project developped with fuelphp 1.7 in which am using basic PHP views for my pages in general (i.e.: fuel/app/views/dashboard.php).
    nd HTML handlebars views for the content details for each of these pages
    (i.e.: public/assets/templates/panelstats.html).

    My goal is to share public HTML templates with PHP with the help of the fuel Mustache parser for example.

    My problem is that I can’t give « public » template views to the « forge »
    method => View::forge('path/to/view’), in the doc I can read: « View files
    should be relative to the app/views directory and be supplied without the .php file extension. ».

    I have 2 questions:
    - How could I achieve the possibility to share templates between PHP and
    JavaScript in my application? (with mustache for example)

    - The most would be with the fuelphp Handlebar parser, but it’s not available in fuelphp 1.7, however is it possible to get it?

    Thank you very much for your help


  • HarroHarro
    Accepted Answer
    There isn't currently a Handlebars parser for Fuel, and I don't know of any third-party parser for it.

    There is a PHP implementation for Handlebars ( so it shouldn't be too difficult to make a parser class for it.

    If it is developed by us, it will be going to 1.9/dev, there is no feature support for older versions anymore. Upgrading from 1.7 to 1.8.x (or 1.9/dev, which is production-stable as well) shouldn't be too difficult, and you'll get PHP 7.x compatibility with it, which you will need soon.
  • Thanks for the fast answer,
    I will upgrade to fuelphp 1.9 very soon. so I will have the Handlebars parser available :-)

    My last encountered difficulty is that I need to give Public template files to render views.
    Example: View::forge('public/assets/templates/myview.html’), but it's not working :-/
    Impossible to forge Public view paths in fuelphp??
    (I need to keep these views Public so that I can have easy access with javascript on client side)

    Many thanks!
  • HarroHarro
    Accepted Answer
    No. I wrote "if it has to be developed by us, we will add it to 1.9/dev". ;-) I can add it to the todo list, but no guarantee as to when it will be available.

    The design philosophy of Fuel is that you keep as much as possible outside the docroot. 

    Every view parser is designed to work with the structure the parser itself requires, which is one of the reasons a special Handlebars parser class needs to be created, so it can access these files (parser classes extend the View class so parser specific functionality becomes available).

    As a workaround, you can manually add a search path before forging the View, and delete that path after, see You can use the list_files() method to check if after you've added the search path, the Finder can locate your html templates.

    This should also work in 1.7.
  • Ok, understood.
    Yes would be great if you could add "Handlebars" to the todo list :-) :-) !!
    Thank you again for your help and long life to FuelPHP ;-) !
  • Ok, will do. And thanks. ;-)
  • Hi, me again :-)
    I have a question:
    I installed fuelphp 1.9-dev recently,
    and I need to install the Handlebars parser for PHP.

    This parser is reallly necessary in my case:
    Because am using Handlebars.js on my client side,
    and I would like to re-use the same templates on the PHP side.

    What is the way to add this functionnality in my dev,
    I just need to add it to the composer.json?
    I found this:
    "require-dev": {
     "xamin/handlebars.php": "dev-master"

    and after that a "php composer.phar update"?

    Thank you for your help
  • If you also need it in your production site, I would use "require" and not "require-dev".

    Otherwise that should do it. The composer.json is in the root of your local fuel installation.
  • Thank you,
    it was installed successfully in fuel/vendor/xamin/handlebars.php/

    When I use it this way, it works:
    $engine = new \Handlebars\Handlebars;
    echo $engine->render(
        'Planets:<br />{{#each planets}}<h6>{{this}}</h6>{{/each}}',
            'planets' => array(

    But when I want to display "myview.html" using handlebars in fuelphp:
    View::forge('auth/myview.handlebars', $data, false);
    I got the error:
    Class 'View_Handlebars' not found
    PKGPATH/parser/classes/view.php @ line 101

    In my parser.php I added the following:
    'extensions' => array(
      'handlebars'  => array('class' => 'View_Handlebars', 'extension' => 'html'),

    'View_Handlebars' => array(
        'auto_encode' => true,
        'environment' => array(
            'cache_dir' => APPPATH.'cache'.DS.'handlebars'.DS,
            'partials'  => array(),
            'helpers'   => array(),
            'charset'   => 'UTF-8',

    What am missing in my installation procedure?
  • If you want to do that, you need the Parser package to implement support for Handlebars, which at the moment it hasn't (you not only need the config definition, you also need the driver class).

    If you're willing to test and provide feedback, I can have a look tomorrow if I can add expiremental support for it. 
  • That's what I am afraid of :-/
    Do you mean to add a "handlebars.php" file in "fuel/packages/parser/classes/view/"?
    Yes, I would be very grateful if you could add this Parser package, is it complicated?

    No worries, I will do a lot of testing and feedback ;-P
    Thank you again for your help
  • Yes, I'll have a look at it.
  • First version comitted.

    I've decided to use zordius/lightncandy instead of xamin/handlebars.php, which is much faster, so you need to modify your composer.json accordingly.

    I'm on the road, so didn't have a chance to test it. Let me know how it goes.
  • You are faster than me :-D, "lightncandy" is the library that I wanted to suggest for the reasons you gave.
    I'm going to install it and to do a lot a lot of tests (I have several JS helpers to adapt for PHP)......
    I will give my feedback how it goes!
    Thanks you very much again.
  • I got an error when I want to display "myview.html":
    $this->template->content = View::forge('auth/myview.handlebars', $data, false);

    Fuel\Core\PhpErrorException [ Warning ]:
    failed to open stream: No such file or directory
    PKGPATH/parser/classes/view/handlebars.php @ line 43

    My parser.php config is as followed (I did the same way than for mustache):
    'extensions' => array(
       'mustache'  => array('class' => 'View_Mustache', 'extension' => 'html'),
       'handlebars'  => array('class' => 'View_Handlebars', 'extension' => 'html'),


    'View_Handlebars' => array(
        'force_compile'   => true,
        'compile_dir'     => APPPATH.'tmp'.DS.'handlebars'.DS,
        'environment'     => array(
            'flags'           => LightnCandy::FLAG_ERROR_EXCEPTION | LightnCandy::FLAG_ELSE | LightnCandy::FLAG_HBESCAPE | LightnCandy::FLAG_JS,
            'helpers'         => array(),
            'helperresolver'  => function($cx, $name) { return; },

    With mustache it works:
    $this->template->content = View::forge('auth/myview.mustache', $data, false);
  • Now I have this error:
    Fuel\Core\InvalidPathException [ Error ]: Invalid basepath: "/Users/gwenael/Documents/www/myproject2/fuel/app/tmp/handlebars/", cannot create directory at this location.
    COREPATH/classes/file.php @ line 180

  • Arggghh... Stupid me. It sucks when you can't test your code...

  • Am testing for u :-)
    Now I have this error; I feel it's the last one:

    Fuel\Core\PhpErrorException [ Notice ]: Undefined variable: data
    PKGPATH/parser/classes/view/handlebars.php @ line 67

    If I rename "$data" with "$result", it seems working
  • Thank you it works :-) :-)
    I will do some more testings...
    Have a nice day

Howdy, Stranger!

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

In this Discussion