Cotonti / Open Source PHP Content Management FrameworkContent Management Framework

Forums / Cotonti / Extensions / Support / Data to TPL with AJAX question

Twiebie
#1 2012-10-01 22:05

I'm trying to get data into a TPL from a PHP file with AJAX.

The code I currently have is as following:

This is the JavaScript bit:

$(document).ready(function() {
    function updateTesting(){
        ajaxSend({
            method: "GET",
            url: 'index.php?r=test&a=update',
            data: "",
            divId: 'test_div'
        });
    }
    updateTesting();

And test.ajax.php:

if ($a == 'update') {
	foreach ($res->fetchAll() as $row) {
		$t->assign(array(
			'TEST_USERNAME' => cot_build_user($row['test_userid'], htmlspecialchars($row['test_username'])),
			'TEST_MESSAGE' => cot_parse($row['test_message'], $cfg['plugin']['comments']['markup']),
			'TEST_DATE' => cot_date('M jS', $row['test_date']),
			'TEST_DATETIME' => cot_date('G:i', $row['test_date'])
		));
		$t->parse('MAIN.TEST_ROW');
	}
}

The TPL:

<!-- BEGIN: MAIN -->

<div id="test_div">
<!-- BEGIN: TEST_ROW -->
	{TEST_USERNAME}
	{TEST_MESSAGE}
	{TEST_DATE}
	{TEST_DATETIME}
<!-- END: TEST_ROW -->
</div>

<!-- END: MAIN -->

For some reason it just doesn't display what I assign to MAIN.TEST_ROW.
What am I missing here?

When I do a simple echo for testing like shown below it does output the data in the TPL with AJAX.

if ($a == 'update') {
	foreach ($res->fetchAll() as $row) {
		echo $row['test_message'];
	}
}

Thanks.

Added 1 hours later:

Hmm, it sort of works when I add to the foreach loop both parse and out:

$t->parse('MAIN.TEST_ROW');
$t->out('MAIN.TEST_ROW');

But I'm wondering if this is the correct way?

Added 2 days later:

Bump.


This post was edited by Twiebie (2012-10-03 17:46, 8 years ago)
GHengeveld
#2 2012-10-04 06:52

Within the foreach, you need to do a parse('MAIN.TEST_ROW'), not an out().

At the end of your file, you need to do this:

$t->parse('MAIN');
$t->out('MAIN');

Usually you use out() only once: for the MAIN block at the end of your php code.

You can omit the 'MAIN' in out(), because it's the default value. To seperate your html code for a=update you may want to add the UPDATE block. Also, you'll often want to be able to show a message if there aren't any results:

if ($a == 'update') {
    $rows = $res->fetchAll();
    if ($rows) {
        foreach ($rows as $row) {
            $t->assign(array(
                'TEST_USERNAME' => cot_build_user($row['test_userid'], htmlspecialchars($row['test_username'])),
                'TEST_MESSAGE' => cot_parse($row['test_message'], $cfg['plugin']['comments']['markup']),
                'TEST_DATE' => cot_date('M jS', $row['test_date']),
                'TEST_DATETIME' => cot_date('G:i', $row['test_date'])
            ));
            $t->parse('MAIN.UPDATE.ROWS.ROW');
        }
        $t->parse('MAIN.UPDATE.ROWS');
    } else {
        $t->parse('MAIN.UPDATE.NO_ROWS');
    }
    $t->parse('MAIN.UPDATE');
}
$t->parse('MAIN');
$t->out();

A complete tpl could look like this:

<!-- BEGIN: MAIN -->
<!-- BEGIN: UPDATE -->
<div id="update">
  <!-- BEGIN: ROWS -->
  <table>
    <thead>
      <tr>
        <th>Username</th>
        <th>Message</th>
        <th>Date</th>
        <th>Datetime</th>
      </tr>
    </thead>
    <tbody>
      <!-- BEGIN: ROW -->
      <tr>
        <td>{TEST_USERNAME}</td>
        <td>{TEST_MESSAGE}</td>
        <td>{TEST_DATE}</td>
        <td>{TEST_DATETIME}</td>
      </tr>
      <!-- END: ROW -->
    </tbody>
  </table>
  <!-- END: ROWS -->
  <!-- BEGIN: NO_ROWS -->
  None
  <!-- END: NO_ROWS -->
</div>
<!-- END: UPDATE -->
<!-- END: MAIN -->

This post was edited by GHengeveld (2012-10-04 07:37, 8 years ago)