Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Parse a string with twig?
  • Hi, I'm trying to parse a string with Twig (as apposed to loading a file as view::forge does).
    I can't see any fuel functions that would do this, and I cant work out how to do this direction with Twig? I think I need to be using something like this (form Twig docs), but PHP says Twig_Loader_String isnt found:
    $loader = new Twig_Loader_String(); If it makes a difference, I'm doing this in a task run from command line.
  • I think I need to be using something like this (form Twig docs), but PHP says Twig_Loader_String isnt found:
    $loader = new Twig_Loader_String();

    Because you haven't create a Twig instance ... so use the View_Twig::parser() method to create a get this. Try to use this code :
    $twig = \Parser\View_Twig::parser();
    $twig->setLoader(new Twig_Loader_String());
    
    $string = $twig->loadTemplate('{{ test_string }}')->render(array('test_string' => 'FuelPHP is awesome!!!'));
    \Debug::dump($string);
    
  • Davide Bellini wrote on Wednesday 14th of December 2011:
    I think I need to be using something like this (form Twig docs), but PHP says Twig_Loader_String isnt found:
    $loader = new Twig_Loader_String();

    Because you haven't create a Twig instance ... so use the View_Twig::parser() method to create a get this. Try to use this code :
    $twig = \Parser\View_Twig::parser();
    $twig->setLoader(new Twig_Loader_String());
    
    $string = $twig->loadTemplate('{{ test_string }}')->render(array('test_string' => 'FuelPHP is awesome!!!'));
    \Debug::dump($string);
    

    Thanks for this reply, that code didn't work though. I get the error
    "Error: Argument 1 passed to Twig_Environment::setLoader() must implement interface Twig_LoaderInterface, null given" I searched the whole project for 'Twig_Loader_String' and the only instance is where I'm using it... (I do have Twig installed properly, it's working everywhere else!)
  • Please post your code ... where are you put my example? in a controller?
    Have you add "parser" in config/config.php -> "always_load" => "packages" array?
  • Davide Bellini wrote on Wednesday 14th of December 2011:
    Please post your code ... where are you put my example? in a controller?
    Have you add "parser" in config/config.php -> "always_load" => "packages" array?

    I'm running this in a class loaded by a task, run from command line (it's for sending email as a cron job). This is basically all the code in the function:
    $tags = array();
    foreach ($params as $p) {
     foreach ($p as $key => $val) $tags[$key] = $val;
    }
    $twig = \Parser\View_Twig::parser();
    $twig->setLoader(new Twig_Loader_String());
    
    $string = $twig->loadTemplate( $string )->render( $tags );
    return $string;
    

    Yip, parser is in the always_load>packages array. The error is 'Argument 1 passed to Twig_Environment::setLoader() must implement interface Twig_LoaderInterface, null given', if I try to debug 'new Twig_Loader_String()' I get the error that 'Twig_Loader_String' does not exist. When I search for it, it doesn't seem to be in the project at all.
  • I'm pretty sure the Twig parser is not included in Fuel PHP, just the extension to enable its use. Get twig from: https://github.com/fabpot/Twig/tags Place it in: Fuel/packages/parser/vendor/Twig Hope that works This guide is a little out of date but should help:
    http://yasbas.com/docspro/2011/10/25/enabling-twig-in-fuelphp/

Howdy, Stranger!

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

In this Discussion