Execute repeated blocks in variable?

56 views
Skip to first unread message

Martin M

unread,
Jun 17, 2017, 4:46:29 AM6/17/17
to Fat-Free Framework
Ok, i hope i can put things the way it is understandable.

In my routing i have the folliowing route:

$f3->route('GET /dogs',
function($f3) {
$f3->get('DB')->exec('SELECT * FROM dogs');
$f3->set('content','dogs.htm');
echo View::instance()->render('layout.htm');
}
);

The view that is rendered is basically the main layout for the website and includes at one point 

<?php echo $this->render(Base::instance()->get('content')); ?>

As you can see dogs.htm is read from file and rendered in layout.htm
Right now i try to figure out how to fill a table with SQL data inside dogs.htm.
If i simply follow the example code

      <repeat group="{{ @result }}" value="{{ @dog}}">
          <span>{{ @dog.name  }}</span>
      </repeat>

all i get is "{{ @dog.name }}" rendered inside the layout...

I guess i have to render dogs.htm instead of just reading it into the variable - but how would i do this?
Thanks in advance!

bcosca

unread,
Jun 17, 2017, 6:06:24 AM6/17/17
to Fat-Free Framework
echo Template::instance()->render('layout.htm');

Martin M

unread,
Jun 17, 2017, 7:03:25 AM6/17/17
to Fat-Free Framework
i tried that... 

$f3->set('DB', new DB\SQL('sqlite:./database.sqlite'));

$f3->route('GET /dogs',
function($f3) {
$f3->set('result',$f3->get('DB')->exec('SELECT * FROM dogs'));
$f3->set('content','dogs.htm');
echo Template::instance()->render('layout.htm');
//echo View::instance()->render('layout.htm');
}
);

that way "content" is not rendered at all.

bcosca

unread,
Jun 17, 2017, 8:09:13 AM6/17/17
to Fat-Free Framework
Where is the variable @result populated? Your code doesn't show that, so there is nothing to iterate over.

Martin M

unread,
Jun 17, 2017, 8:27:42 AM6/17/17
to Fat-Free Framework
It is populated. Furthermore dogs.htm consists of more than just the code to show the SQL results.

$f3->set('DB', new DB\SQL('sqlite:./database.sqlite'));

$f3->route('GET /dogs',
function($f3) {
$f3->set('result',$f3->get('DB')->exec('SELECT * FROM dogs'));
$f3->set('content','dogs.htm');
echo Template::instance()->render('layout.htm');
//echo View::instance()->render('layout.htm');
}
);

Database:


dogs.htm:


<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="col-md-8">
<h3>
Create or Edit dog
</h3>
<form role="form">

<div class="form-group">
     <label for="name">
Name
</label>
<input type="text" class="form-control" id="name" />
</div>

            <div class="form-group">
<label for="dob">
Date of birth
</label>
<input type="text" class="form-control" id="dob" />
</div>

            <div class="form-group">
<label for="harness">
Size of the harness
</label>
<input type="text" class="form-control" id="harness" />
</div>



            <div class="checkbox">
              <label for="harness">
Sex
</label><br>
<label>
<input type="checkbox" id="male"/> Male
                <input type="checkbox" id="female"/> Female
</label>
</div>

<div class="form-group">
<label for="picUpload">
Picture(s)
</label>
<input type="file" id="picUpload" />
<p class="help-block">
Here you can upload pics of the dog
</p>
</div>

<button type="submit" class="btn btn-default">
Submit
</button>
</form>
</div>

<div class="col-md-4">
<h3>
Picture(s)
</h3>
          ............
</div>
</div>

        <hr>

<table class="table">
<thead>
<tr>
<th>
#
</th>
<th>
Name
</th>
<th>
Sex
</th>
<th>
Harness
</th>
</tr>
</thead>
<tbody>




</tbody>
</table>

      <repeat group="{{ @result }}" value="{{ @item }}">
          <span>{{ @item.name  }}</span>
      </repeat>

<div class="row">
<div class="col-md-12">
</div>
</div>
</div>
</div>
</div>



Layout.htm


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../favicon.ico">

    <title>Hundeboard</title>

    <!-- Bootstrap core CSS -->
    <link href="./css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="./dashboard.css" rel="stylesheet">
  </head>

  <body>
    <nav class="navbar navbar-toggleable-md navbar-inverse fixed-top bg-inverse">
      <button class="navbar-toggler navbar-toggler-right hidden-lg-up" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <a class="navbar-brand" href="#">Dashboard</a>

      <div class="collapse navbar-collapse" id="navbarsExampleDefault">
        <ul class="navbar-nav mr-auto">
          <li class="nav-item active">
            <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Settings</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Profile</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Help</a>
          </li>
        </ul>
        <form class="form-inline mt-2 mt-md-0">
          <input class="form-control mr-sm-2" type="text" placeholder="Search">
          <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
        </form>
      </div>
    </nav>

    <div class="container-fluid">
      <div class="row">
        <nav class="col-sm-3 col-md-2 hidden-xs-down bg-faded sidebar">
          <ul class="nav nav-pills flex-column">
            <li class="nav-item">
              <a class="nav-link active" href="/">Übersicht <span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="/dogs">Hunde</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="/dates">Termine</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="/shifts">Schichtplanung</a>
            </li>
<li class="nav-item">
              <a class="nav-link" href="/logs">Logbücher</a>
            </li>
          </ul>



          <ul class="nav nav-pills flex-column">
            <li class="nav-item">
              <a class="nav-link" href="#">Nav item </a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">Nav item </a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">Another nav item</a>
            </li>
          </ul>
        </nav>

        <main class="col-sm-9 offset-sm-3 col-md-10 offset-md-2 pt-3">
<?php echo $this->render(Base::instance()->get('content')); ?>
      </main>
      </div>
    </div>

    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
    <script>window.jQuery || document.write('<script src="./js/vendor/jquery.min.js"><\/script>')</script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
    <script src="./js/bootstrap.min.js"></script>
    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <script src="./js/ie10-viewport-bug-workaround.js"></script>
  </body>
</html>





So.. at this moment the code returns nothing where normally content should be.

ikkez

unread,
Jun 17, 2017, 8:43:41 AM6/17/17
to Fat-Free Framework
you cannot put <?php tags into templates parsed by the \Template engine.. it'll remove inline php for security reasons.. use the <include> tag instead

<main class="col-sm-9 offset-sm-3 col-md-10 offset-md-2 pt-3">

   
<include href="{{@content}}">
</main>

Martin M

unread,
Jun 17, 2017, 8:52:24 AM6/17/17
to Fat-Free Framework
Thank you! That did the trick! 


I like the security concearns that blocked me from doing what i wanted - took a while but makes sense :)
Reply all
Reply to author
Forward
0 new messages