Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
template parsers
  • The projects I've built with Fuel thus far have used native PHP templates.
    I've just started a new project using Smarty - which I really liked pre-Fuel.
    But, I'm finding that I miss using PHP templates because it's so cumbersome to use Fuel's cool classes with Smarty. Any suggestions for more Fuel-Friendly parsers would be appreciated. Basically, I would be happy with a parser that simply makes the following replacements:
    '{' ='<?php '
    '{$' = '<?php echo '
    '}' = '; ?>' Or, something similar.
  • There are parsers repository under https://github.com/fuel/parser
    have you checked it? it even has simple tags, which do really simple tags implemetation for heavier things - you can use twig or smth like that
  • Yeah, in the Fuel parser package you can find support for Twig, Smarty, Dwoo, Jade, ... For some parsers ( also Smarty ), you can set delimiters in package config file :
    https://github.com/fuel/parser/blob/develop/config/parser.php For performance reasons I discourage you to use Smarty ... if you need an advanced template parser ( as suggested by @huglester ) I can suggest Twig. Take a look to this Fabien Potencier's blog article with benchmarks :
    http://fabien.potencier.org/article/34/templating-engines-in-php
  • Thanks for the suggestions, Guys.
    And, thanks for recommending Twig @Davide Bellini. I will give it a try.
  • How does one take advantage of Fuel's classes such as \Form::open() within a Twig or Smarty template? For example, I really like using Form::open() knowing that it will output the following HTML:
    <form action="CURRENT_URI" accept-charset="utf-8" method="post">
    

    Looking ahead to when HTML8 is about to be deprecated, I can trust that the Fuel developers will modify Form::open() to output a browser-friendly opening form tag for that day. Otherwise, I will have to edit all my templates to comply.
  • I believe you you should write a plugin, or smth. not sure really.
    I remmeber when I needed some funcctionality in smarty - I was making plugins
  • Exactly ...
    You can create a Twig extension that use Fuel Form class ... Read this for how to create an extension :
    http://www.twig-project.org/doc/extensions.html
  • Thanks @Huglester & @Davide Bellini! As you two suggested, Twig is clean and fast. I've spent the day auditioning every vendor in Fuel's parser package. I found that I could use multiple parsers simply by adding their file extension to the view filepath. WoW! My problem is solved.
    When I want to use Fuel's Html/Input/Fieldset or whatever class in my template, I put that in a partial view that uses the .php file extension.
  • I think I have Twig like I want it now.
    I can use code like this in my templates - taking advantage of Fuel's classes.
    {{ Html('hr') }}
    
    <p>
            {{ Form('open', {}, ['type', 'id']) }}
            {{ Form('input', 'value', value, {'style': 'border:solid 1px;'}) }}
            {{ Form('submit') }}
            {{ Form('close') }}
    </p>
    
    Thanks again for pointing me in the right direction.
  • And you can use Fuel Debug::dump() method like a Twig filter with : Twig_ext.php
    <?php
    
    class Fuel_Twig_Extension extends Twig_Extension
    &#123;
     // Project name
     public function getName()
     &#123;
      return 'fuel_twig';
     }
    
     // Filters
     public function getFilters()
     &#123;
      return array(
       'dump' => new Twig_Filter_Function(__CLASS__.'::filter_dump'),
      );
     }
    
     //--------------------------------------------------------------------------
     // FILTERS
     //--------------------------------------------------------------------------
     /**
      * Debug a variable
      * @param any  Variable to debug
      * @return void Debug dump
      */
     public static function filter_dump($var)
     &#123;
      return \Debug::dump($var);
     }
    
    }
    

    In controller :
    $twig = \Parser\View_Twig::parser();
    include_once APPPATH.'vendor'.DS.'Twig_ext.php';
    $twig->addExtension(new \Fuel_Twig_Extension());
    

    In templates :
    &#123;&#123; variable|dump }}
    
    

Howdy, Stranger!

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

In this Discussion