Forum
Add-on app, render related "model"
Hi, I am in the process of developing an add-on for Perch. It's a pretty basic poll app, with the following structure currently:
TurnLeft_Answer.class.php TurnLeft_Answers.class.php TurnLeft_Poll.class.php TurnLeft_Polls.class.php TurnLeft_Vote.class.php TurnLeft_Votes.class.php
The admin CRUD interface works fine, I can create, edit polls, add answers to polls and so forth. Now I am trying to create the runtime functions that prints the active Polls along with the Answers as simple checkboxes. In runtime.php I have added the turnleft_polls_list() which looks exactly like the example add-on app, and ends with:
$r = $Template->render_group($list, true);
if ($return) return $r;
echo $r;
return false;
The template file is where I can't seem to wrap my head around how to render the related Answers to a Poll.
This is my turnleft_polls/templates/polls/list.html:
<perch:before>
<div class="polls">
</perch:before>
<div class="poll-item">
<h4 href="polls.php?s=<perch:polls id="pollID" />"><perch:polls id="pollTitle" /></h4>
<p><small class="poll-end-at">Poll ends <perch:polls id="pollEndAt" /></small></p>
<p><perch:polls id="pollDesc" /></p>
<div class="poll-answers">
<perch:polls id="answers" /> <-- ???
</div>
</div>
<perch:after>
</div>
</perch:after>
How do I render the Answers as simple checkboxes from the polls template? By extending the to_array and inject the rendered HTML form fields? I feel there must be a simple solution to this. Thanks in advance.
You'd use the template class for that: https://docs.grabaperch.com/api/reference/template/
I don't understand where to do this, in my Poll.class? I already use render_group to render the Polls, but how do reference or render a group Answers from within the polls.html template?
runtime.php:
TurnLeft_Poll.class.php:
TurnLeft_Answers.class.php:
templates/polls/list.html:
templates/polls/answers.html:
Am I structuring this the wrong way? Thanks again.
You'd need to pre-render them and then pass them in, or alternatively, add your own answers tags with a template handler to take care of the rendering.