Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Is it possibe call or access Fuel classes from twig template view
  • Hi, 

    I got a trouble when using twig temaplate views.
    I would like to know if we able to call or access Fuel classes from twig template view.

    For instance in my twig view (list.twig):
    <a href="Router::get('dashboard/delete')">delete</a>


    Thanks in advance!


  • Ok, I think no.

    So I've writing a twig extension:

    This is what I have right now:
    fuel/app/modules/dashboard/classes/twig/extension/functions/router.php:

    use Fuel\Core\Router as FuelRouter;
    /**
    * Resolve named routes from inside twig templates
    *
    * @uses \Fuel\Core\Router
    */
    class FuelRouterTwigExtension extends Twig_Extension {

    public function getName() {
    return 'fuel_router';
    }

    public function getFunctions() {
    return array(
    'fuel_router' => new Twig_Function_Function(function($router_name, $params = array()) {
    return FuelRouter::get($router_name, $params);
    }),
    );
    }
    }

    Now I add in bootstrap.php:
    Autoloader::add_classes(array(
    'FuelRouterTwigExtension' => APPPATH . 'modules/dashboard/classes/twig/extension/functions/router.php',
    ));

    Fuel::init('config.php');

    # Add extension
    View_Twig::parser()->addExtension(new \FuelRouterTwigExtension());

    And it done! It works for me:

    /path/to/my/views/list.twig:
    {{ fuel_router("my_named_route", {"param": "value"}) }}

    that's same than:
    echo Router::get("my_named_route", array("param" => "value"))

  • There is already quite a list of twig extensions defined by default, but this one isn't.

    if you can create an issue for it, at https://github.com/fuel/core/issues, so it can be added to the core.
  • Hi, Havo!

    I've added some features and now I can to access not only router resources but others meaningful stuff.
    However, I need get way in order to access fuelphp resources by automatic way (maybe a bit Reflection), my code is a bit "hardy coded"...anyway...
    For while what I have works very well!

Howdy, Stranger!

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

In this Discussion