Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Model custom data and Twig rendering
  • Hi there

    I'm stuck when using twig to render a simple view with model custom data, like :

    /* --- PHP --- */

    $my_model = Model_My_Model::query()->where('id', $my_id)->get_one();

    $my_model->my_custom_data = 'foo';

    Theme::instance()->get_template()->content = View::forge('my_view.twig', array('my_model' => $my_model); 

    /* --- Twig --- */

    {{ my_model.my_non_custom_data }} ---> work fine
    {{ my_model.my_custom_data }} ---> Twig_Error_Runtime [ Error ]: An exception has been thrown during the rendering of a template ("Call to undefined method Model_My_Model::my_custom_data()") in "my_view.twig" at line xx.

    I think I used custom datas with twig before without any trouble, weird.

    Thank
  • Why would it think my_custom_data is a method, and not a property?
  • That's exactly my question. When I do a Debug::dump in the twig view the custom data exists in the model. It's like there was bracket, if I do {{ my_model.my_custom_data() }}, the same error is triggered. I don't understand why.
  • I found out the problem. The property wasn't set for one item in my model array. So when you try to access to an ORM object property which doesn't exists, twig try to call it as a method ? How can this be solved ?
  • Must be a Twig thing (and I don't use Twig).

    The ORM throws an OutOfBoundsException if you request a property on an ORM object that does not exist.

Howdy, Stranger!

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

In this Discussion