Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ViewModel with twig templates
  • Hi. I can load and parse a twig template with View::forge, now I am trying to use the default action_hello (that calls ViewModel::forge) with a twig template. I added the ".twig" extension to "hello" (first parameter of forge), like this:
    return Response::forge(ViewModel::forge('welcome/hello.twig'));
    

    but I get an error 'OutOfBoundsException [ Error ]: ViewModel "View_Welcome_hello.twig" could not be found.' I also tried to rename the app/classes/view/hello.php to app/classes/view/hello.twig.php, but it it still not working. Am I missing something?
    Thank you.
  • Thank you and sorry for the lateness, I didn't notice the new replies.
    This is what I need. Thank you very much.
  • For non PHP templates you have to manually set the View name in the ViewModel: http://docs.fuelphp.com/general/viewmodels.html#/changing_view Currently it's not supported, but it may be doable to allow you to pass the View extension through the ViewModel forge. Remember though that the ViewModel forge is about creating the ViewModel and very much not the View. Thus the parameter you pass is in relation to the ViewModel classname and has no relation to the View name other than that convention will translate the ViewModel classname to the View path when none is set.
  • Here is an example of using Twig in ViewModel. Transform the welcome/hello.php to a twig tamplate.
    class View_Welcome_Hello extends ViewModel
    {
        public function view()
        {
            $this->name = $this->request()->param('name', 'World');
        }
    
        protected function set_view()
        {
            $this->_view = View::forge('welcome/hello.twig');
        }
    }
    

    The set_view function will be used to load a View object from a parsed twig template. Update Feb 24, 2012 2:50pm: The param is not loading. Will keep digging and post here. For now, $this->name doesn't pass to the view. Update Feb 24, 2012 3:05pm: Stupid me. Forgot to change the PHP echo statement to Twig statement. Everything works now. Hope this helps.
  • As the docs that I mentioned tell, though the wording has gotten a bit twisted: You don't have to create a method, the following will work just as well (using a property).
    class View_Welcome_Hello extends ViewModel
    {
        protected $_view = 'welcome/hello.twig';
    
        public function view()
        {
            $this->name = $this->request()->param('name', 'World');
        }
    }
    

    The property is meant as the convention name overwrite. The overwriting the set_view() should only be done if you want some other way of determining the template name or when you want to use some other View-like class instead of View.

Howdy, Stranger!

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

In this Discussion