Page 1 of 1

nested repetitions

Posted: Wed Nov 30, 2005 3:49 pm
by ford
Hey folks!

I have a question regarding nested repetitions. I have an array of data that I want to output to a template (settings.html : see code below).

Code: Select all

<mos:tmpl name="settings">

<table class="contentpane">

<mos:tmpl name="parameters" type="condition" conditionvar="FIELD_TYPE">

<mos:sub condition="list">
<tr align="center" valign="middle">
<td align="left" valign="top">{ROW_LBL}</td>
<td align="left" valign="top">{ROW_PARAM}</td>
<td align="left" valign="top">{ROW_DESC}</td>
</tr>
</mos:sub>

<mos:sub condition="textarea">
<tr align="center" valign="middle">
<td align="left" valign="top">{ROW_LBL}</td>
<td align="left" valign="top" colspan="2">{ROW_PARAM}</td>
</tr>
</mos:sub>

</mos:tmpl>

</table>

</mos:tmpl>
As you can see there is a template called "parameters" which is a conditional template, controlled by the variable FIELD_TYPE. The template has two subconditions, depending on which variable is fed to FIELD_TYPE.

Here is the code that sets up the $data array:

Code: Select all

$data[] = array();
$data[] = array( 'type' => 'list', 'lbl' => _PMS_NOTIFY, 'param' => $lists['notify'], 'desc' => _PMS_NOTIFY_DESC );
$data[] = array( 'type' => 'list', 'lbl' => _PMS_NOTIFY_MSG_INCLUDE, 'param' => $lists['ntfmsginc'], 'desc' => _PMS_NOTIFY_MSG_INCLUDE_DESC);
$data[] = array( 'type' => 'list', 'lbl' => _PMS_INCLUDE_SIGNATURE, 'param' => $lists['sig'], 'desc' => _PMS_INCLUDE_SIGNATURE_DESC);
$data[] = array( 'type' => 'textarea', 'lbl' => _PMS_SIGNATURE_TEXT, 'param' => $sigtextarea, 'desc' => '');
and so on and so on .......

The data inside the $data array is then parsed by the following code (the function createTemplate is not shown here, but is defined in the code):

Code: Select all

function &createTemplate() {

global $option, $mosConfig_absolute_path;
require_once( $mosConfig_absolute_path.'/includes/patTemplate/patTemplate.php' );

$tmpl =& patFactory::createTemplate( $option, true, false );
$tmpl->setRoot( dirname( __FILE__ ) . '/tmpl' );
return $tmpl;
}

$tmpl = &createTemplate();
$tmpl->setAttribute( 'body', 'src', 'settings.html' ); // load settings tmpl

$keys = array_keys($data);
	foreach ($keys as $k)
		{
			$tmpl->addVar( 'parameters', 'field_type', $data[$k]['type'] );
			$tmpl->addVar( 'parameters', 'row_lbl', $data[$k]['lbl'] );
			$tmpl->addVar( 'parameters', 'row_param', $data[$k]['param'] );
                        if ($data[$k]['desc']!='')
					{
						// only addVar if 'desc' isn't empty
						$tmpl->addVar( 'parameters', 'row_desc', $data[$k]['desc'] );
					}
                        $tmpl->parseTemplate ( 'parameters', 'a');
                }

$tmpl->displayParsedTemplate( 'form' );

But this doesn't work. The templating engine complains that the template "parameters" doesn't exist.

Code: Select all

pat-Warning: Template 'parameters' does not exist.
I want it to append new rows, one for each sub-array inside $data, choosing the correct sub-template depending on the variable 'type' and passed to the template via FIELD_TYPE.

Can anyone help? I'm stuck :(

Cheers from

Ford

Re: nested repetitions

Posted: Wed Nov 30, 2005 4:04 pm
by ford
Oh well. I solved it myself. Here's what I did. You just have to stick to addRows - that leaves all the dirty work up to patTemplate.

So the following is the way to do it (God, I'm really embarassed).

Change :

Code: Select all

$keys = array_keys($data);
	foreach ($keys as $k)
		{
			$tmpl->addVar( 'parameters', 'field_type', $data[$k]['type'] );
			$tmpl->addVar( 'parameters', 'row_lbl', $data[$k]['lbl'] );
			$tmpl->addVar( 'parameters', 'row_param', $data[$k]['param'] );
                        if ($data[$k]['desc']!='')
					{
						// only addVar if 'desc' isn't empty
						$tmpl->addVar( 'parameters', 'row_desc', $data[$k]['desc'] );
					}
                  }     
to

Code: Select all

$tmpl->addRows( 'parameters', $data, 'row_' );
And in the template, change

Code: Select all

<mos:tmpl name="parameters" type="condition" conditionvar="FIELD_TYPE">
to

Code: Select all

<mos:tmpl name="parameters" type="condition" conditionvar="ROW_TYPE">
In this way, $data[] is just fed to the template, and patTemplate figures out how to distribute the variables from there. Notice that addRows uses the prefix "row_" to pass the data on to all template variables beginning with "row_" (also the conditionvar)!

Maybe somebody finds the example above useful. Moral of the story : keep it simple stupid!

See ya

Ford