/******************** CODE ********************/
$renderer =& $mform->defaultRenderer();
$template = <<<EOT
<table border="0">
<tr>
<td>
<div class="fitem {advanced}<Unable to render embedded object: File (-- BEGIN required --> required<) not found.-- END required -->"><div class="fitemtitle" style="text-align:left; width:80%"><label>{label} <!-- BEGIN required -->{req}<!-- END required -->{advancedimg}</label></div> {help}</div>
</td>
</tr>
<tr>
<td>
<div style="width:100%;" class="felement {type}<Unable to render embedded object: File (-- BEGIN error --> error<) not found.-- END error ->"><!- BEGIN error --><span class="error">{error}</span><br /><!-- END error -->{element}</div>
</td>
</tr>
</table>
EOT;
$templateleft = '<table width="100%" border="0"><tr><td align="center">{content}</td>';
$templateright = '<td align="center">{content}</td></tr></table>';
// Change the element's template
$mform->addElement('text', 'element1', 'Label 1', 'size="40"');
$mform->addRule('element1', $strrequired, 'required', null, 'client');
$mform->setHelpButton('element1', array('helpwithelement1', get_string('element1')), true);
$mform->setType('element1', PARAM_TEXT);
$renderer->setElementTemplate($template, 'element1');
// Wrap the element with supplied html
$mform->addElement('text', 'element2', 'Label 2', 'size="20"');
$mform->addRule('element2', $strrequired, 'required', null, 'client');
$mform->setType('element2', PARAM_TEXT);
$renderer->setElementTemplateWrap($templateleft, 'element2');
$mform->addElement('text', 'element3', 'Label 3', 'size="20"');
$mform->addRule('element3', $strrequired, 'required', null, 'client');
$mform->setType('element3', PARAM_TEXT);
$renderer->setElementTemplateWrap($templateright, 'element3');
/**************************************************/
From the code above, element1 will display the Label 1 above the text box and the help button to top right, i.e. a call to setElementTemplate() function will change the default template for an element. On the other hand, for element2 and elemtent3 the template does not change, instead it wraps the element's template with either $templateleft or $templateright.
Also, given that {req} substitution gets replace in MoodleQuikFrom_Renderer.startFrom() for _elementTemplates, then the following line should be included to also updated any submitted templates:
$this->_templates = str_replace('{req}', $this->_reqHTML, $this->_templates);
Committed a patch to 1.8 Stable and HEAD. Commit comment : "MDL-11318 formslib now respects a template for an element if it is set. But since formslib uses the templates to insert help buttons for setAdvanced functionality etc. if you use templates you will lose this functionality."