Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to forge two files in View::forge()
  • It is possible to forge two files?

    I have one controller which is (page.php)
    and
    I have two views which is (page1.php and page2.php)

    Page2 is inside page1.

    So I have this in my page1.php

    ------------------
    <div>
    text about page 1
    more text about page 1

    <? echo View::forge("page2"); ?>
    <div>
    ------------------

    My problem is when I'm trying to call functions from controller (page),

    It's only working on page1 because on my controller (page), I have this code.

    $view = View::forge("page1", $data);
    $this->template->content = $view;

    But when I change my view to page2,

    $view = View::forge("page2", $data);
    $this->template->content = $view;

    page2 is now working.

    That's why I'm thinking if it's possible to have like this.

    $view = View::forge("page1", $data);
    $view = View::forge("page2", $data);
    $this->template->content = $view;

    Sorry if you find its not understandable explanation.
  • You mean you have an extra layer of views, like: template -> content-section -> sub-elements ?

    Not a problem, just do the same as you would do with the template:

    $view = View::forge("page1", $data);
    $view->content = View::forge("page2", $data);
    $this->template->content = $view;

    Then in your "page1", you have $content (or pick any other variable name) available which will contain the "page2" view.
  • I don't have any problem on displaying the view. I'm problem is using the the data from the controller.

    In my controller. I have this code.

    class Controller_Students_page1 extends Controller_Students
    {
    public function action_index()
    {
    $data["news"] = Model_News::find("all", [
    "where" => [
    ["deleted_at", 0],
    ["for_students", 1]
    ],
    "order_by" => [
    ["id", "desc"],
    ],
    "limit" => 5
    ]);

    $data["user"] = $this->user;

    $view = View::forge("page1", $data);
    $this->template->content = $view;
    }
    }
  • In my page1:

    <div>
    <p>Lorem Ipsum is simply dummy text of the
    printing and typesetting industry.
    Lorem Ipsum has been the industry's standard
    dummy text ever since the 1500s, </p>

    <? echo View::forge("page2"); ?>
    </div>

    In my page2:

    <div>
    <ul>
    <li>lorem1</li>
    <li>lorem2</li>
    <li>lorem3</li>
    </ul>
    </div>


    When I try to call the data["news"] in page1. It's working.
    page1:
    <?= count($news); ?> //working

    But in page2, it's not working
    page2:
    <?= count($news); ?> //not working


    When I update my controller
    from:
    $view = View::forge("page1", $data);
    to:
    $view = View::forge("page2", $data);

    page2 is working.

    Everything of the both view is displayed, only the results what I want from the controller is not responding.

    Thanks Harro, the one you replayed is not working.
  • HarroHarro
    Accepted Answer
    A View is an object, so it's quite logical that object Page2 can not access properties of object Page1. There is no automatic inheritance of properties if one view file includes another.

    Personally I don't think a view should contain code, it should only contain HTML and simple related display logic, like an if or a foreach.

    But if you insist, you can pass data along like so:

    <? echo View::forge("page2")->set('news', $news); ?>

    Or if you're on PHP 5.4+ and want to pass everything along:

    <? echo View::forge("page2")->set($this->get()); ?>

  • Yes, I'm just going to add some logic which is IF statement, and I need the data.
    Okay thanks. I'll try this one. Thank you Harro.
  • Thanks Harro, it's works.

Howdy, Stranger!

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

In this Discussion