Forum

Thread tagged as: Question, Addons, Add-on-development

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.

Simon Rothenborg

Simon Rothenborg 0 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

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:

    function turnleft_polls_list($opts=array(), $return=false)
    {
        ...
        $API  = new PerchAPI(1.0, 'turnleft_polls');
        $Polls = new TurnLeft_Polls($API);
        ...
        $Template = $API->get('Template');
        $Template->set('polls/'.$opts['template'], 'polls');
        $r = $Template->render_group($list, true);
        if ($return) return $r;
        echo $r;
        return false;
    }

TurnLeft_Poll.class.php:

    public function to_array($template_ids=false)
    {
        $out = parent::to_array();
        $API  = new PerchAPI(1.0, 'turnleft_polls');
        $Lang = $API->get('Lang');
        $Form = $API->get('Form');
        $answerRepository = new TurnLeft_Answers();
        $answers = $answerRepository->get_for_poll($this->id());
        $out['answers'] = $answers;
        return $out;
    }

TurnLeft_Answers.class.php:

    public function get_for_poll($pollID, $Paging = false)
    {
        ...
        $sql .= ' *
                FROM '.$this->table.'
                WHERE pollID='.$this->db->pdb($pollID).'
                ORDER BY answerID DESC';
        ...
        return $this->return_instances($rows);
    }

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"> ??? </div>
        </div>
    <perch:after>
    </div>
    </perch:after>

templates/polls/answers.html:

    <perch:before>
    <ul>
    </perch:before>
        <li><perch:polls id="answerText" /></li>
    <perch:after>
    </ul>
    </perch:after>

Am I structuring this the wrong way? Thanks again.

Drew McLellan

Drew McLellan 2638 points
Perch Support

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.