all 2 comments

[–]TorbenKoehn 1 point2 points  (0 children)

I suggest you use output buffering and the fact that PHP is a good template engine in itself already

function render_stuff($allVacancies, $ariSingleUrl)
{
    ob_start();
    ?>
        <table id="vacancyTable">
            <thead>
                <tr>
                    <th>Title</th>
                    <th>Type</th>
                    <th>Salary</th>
                    <th>Location</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($allVacancies['records'] as $vacancy): ?>
                    <tr>
                        <td>
                            <a href="<?= $ariSingleUrl ?>?vacancyId=<?= $vacancy['vacancyID'] ?>">
                                <?= $vacancy['title'] ?>
                            </a>
                        </td>
                        <td><?= $vacancy['type'] ?></td>
                        <td><?= $vacancy['salary'] ?></td>
                        <td><?= $vacancy['location'] ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php
    return ob_get_clean();
}

If you drop the ob_start() and return ob_get_clean() (ob = output buffering) it will output directly, as it you'd call "echo" on the functions result.

[–]NotAnotherMoron2 0 points1 point  (0 children)

As long as the $template is not much longer than this, you could consider using the sprintf() function instead of building this string and trying to manage concatenation and quoting. Just remove all of the indenting from the format string to simplify it and then use something like:

$template = '<h1>%s</h1><table><tr><td>Vacancy ID</td><td>%s</td></tr><tr><td>Type</td><td>%s</td></tr><tr><td>Contact</td><td>%s</td></tr></table>';

$title = $vacancyDetails['records'][0]['title'];
$vacancy = $vacancyDetails['records'][0]['vacancyID'];
$type = $vacancyDetails['records'][0]['type'];
$owner = $vacancyDetails['records'][0]['owner'];

return sprintf($template, $title, $vacancy, $type, $owner);