Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Do not repeat yourself. So how can I reuse parts of my view?
  • I know I can call separate view source by doing

    <?php echo render('example/xxxx'); ?>

    But how can I do the same thing when the separate view source also has some valuables on it?

    Thanks.

  • I kind of sort of solved the problem on my own.

    Instead of doing 
    View::forge('example/aaaa', $data); 

    I set the valuable with 
    $this->template->set_global('data', $data, false);
    then in view,

    <?php echo render('example/otherviewfile'); ?>

    Now otherviewfile.php can have the valuable in it.
    Is this the right way to go about it?
  • HarroHarro
    Accepted Answer
    That is a solution, but not a very good one. It's not exactly good practice to use global variables, no matter if you're talking about Views, or about PHP itself.

    You can pass variables on from a parent to a child view using

    <?php echo render('example/otherviewfile', array('var' => $value)); ?>

    or if you're using PHP 5.5+

    <?php echo render('example/otherviewfile', $this->get()); ?>

    to pass all data. For < 5.5, you could use $__data for that, but that is not officially supported.

    However, hardcoding child views isn't exactly good practive either. A view should not contain code (apart from some flow-control, like if's and loops). Either pass a child View as an object from the controller (and assign your variables there like you do for the parent View), use a Presenter (and have it pass the View object), or use the Theme class, which will do all the assigning for you.
  • Hello Harro.

    I can't thank you enough. That's the exact solution I needed.

    I don't exactly hard code child view. I have the same HTML (a table with some values on it) and it has to be shown in several different actions. So I cut the table out to a separate view file. I didn't know render() can pass valuable that way. Thanks again. 
  • HarroHarro
    Accepted Answer
    http://fuelphp.com/docs/classes/view.html#/function_render

    It is procedural alias for View::forge().

Howdy, Stranger!

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

In this Discussion