Search code examples
fat-free-framework

Fatfree Framework nested Templates


Trying to wrap my head around Fatfree and how I can nest templates using data from the DB. So far I have a home page loading that loads three templates. So good so far and everything works.

Main class

function homePage($f3){
   $f3->set('slider','slider.html');
   $f3->set('testimonials','testimonials.html');
   $f3->set('cardContainer','cardContainer.html');
   echo Template::instance()->render('home.html');
}

Home.html

<include href="{{ @slider }}" />
<include href="{{ @testimonials }}" />
<include href="{{ @cardContainer }}" />

CardContainer needs to load cards containing images and other text that are stored in the DB. I can get those rows from the DB no problem in the Main class and var_dump them to the view.

How ever I'm not understanding how I add that data to a card template and then insert that card into the cardContainer? I'm not even sure what I need to be looking for to make this possible. Any direction would be much appriciated. If this was done in straight up PHP I would be done by now.

Thanks for any help or guidance you can provide.


Solution

  • You would pull them out of your database and do one of 2 things:

    <?php
    // option 1
    function homePage($f3){
       $cards = $f3->db->getYourCardsOrWhatever();
       $f3->set('cards', $cards);
       $f3->set('slider','slider.html');
       $f3->set('testimonials','testimonials.html');
       // then in here you would reference @cards in a <repeat> element
       $f3->set('cardContainer','cardContainer.html');
       echo Template::instance()->render('home.html');
    }
    
    // option 2
    ?>
    <include href="{{ @slider }}" />
    <include href="{{ @testimonials }}" />
    <include href="{{ @cardContainer }}" with="cards={{ @cards_from_somewhere_to_inject }}" />