Categories > TinyButStrong general >

sub template with some parameters

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Anonymous
Date: 2016-03-12
Time: 19:35

sub template with some parameters

Scroll,

I have made a lot of progress thanks to the pointers you have given me.

You will probably answer yes to this question as well... is it possible to use a sub template with some parameters. I think this is what 'subtpl' is for.

What I want to do is to put some conditional and often reused TBS stuff inside it, and then just get the merged data back. I want to use it to format the output strings depending on the type of data it represents. So if it is a float, i will present it with the 0.1234 format, if integer, with the frm='0' parameter.
So to the sub template I want to pass the datatype of the item, the raw string and then get back the merged and formatted string.

Roughly the stuff I want to put in the template are below. I want to pass it a type string, in this case the [constants.const_type] and the data string, in this case [constants.const_defaultvalue] and then just merge back the result.

Possible?
Pointers in the right direction?
[constants;block=begin;when [constants.const_type] == 'uint8']#define [constants.const_name] [constants.const_defaultvalue;frm='0']U //[constants.const_type] - [constants.const_description]
[constants; block=end]
[constants;block=begin;when [constants.const_type] == 'uint16']#define [constants.const_name] [constants.const_defaultvalue;frm='0']U //[constants.const_type] - [constants.const_description]
[constants; block=end]
[constants;block=begin;when [constants.const_type] == 'float']#define [constants.const_name] [constants.const_defaultvalue]F //[constants.const_type] - [constants.const_description]
[constants; block=end]

Thanks for all your help so far.
Ivor

PS: you really need to get a "Donate" button.
[constants;block=begin;default]#define [constants.const_name] [constants.const_defaultvalue;protect=no;strconv=no] //[constants.const_type] - [constants.const_description]
[constants; block=end]




By: Skrol29
Date: 2016-03-14
Time: 23:18

Re: sub template with some parameters

Hi Ivor,

Using sub-template is done for re-use a piece of template. But in your case I generally use another technical: parameter "ondata".

Since your parts are quite similar and are place inside the same block, here is what you can do:

At PHP side, use an "ondata" function to customize data beofre merging.
function f_my_ondata($BlockName,&$CurrRec) {

  static $types = array(
    'uint8' => array('u' => 'U', 'convint' => true,),
    'uint16' => array('u' => 'U', 'convint' => true,),
    'float' => array('u' => 'F', 'convint' => false),
  );

  $t = $CurrRec['type'];
  if (isset($types[$t])) {
     $CurrRec = array_merge($CurrRec, $types[$t]);
     if ($CurrRec['convint']) $CurrRec['const_defaultvalue'] = (integer) $CurrRec['const_defaultvalue'];
  } else {
     $CurrRec['u'] = '';
  }

}

And at the template side :
[constants;block=begin;ondata=f_my_ondata]#define [constants.const_name] [constants.const_defaultvalue;protect=no;strconv=no][constants.u] //[constants.const_type] - [constants.const_description]
[constants; block=end]

By the way, did you know "block=_" and "block=!div" ?
They are spacial marks that can facilitate you block bounding.
  See http://www.tinybutstrong.com/manual.php#html_block_prm_specialmarks
By: Ivor
Date: 2016-03-17
Time: 21:43

Re: sub template with some parameters

Scroll, thanks again for replying, i missed this reply because it went to "anonymous" for some reason. I will try it out as soon as possible.

Ivor
By: Ivor
Date: 2016-03-22
Time: 08:03

Re: sub template with some parameters

Scroll, your on_data function works perfectly, of course. Thanks again.

Oh, I just changed the line that had

$t = $CurrRec['type'];

to
$t = $CurrRec['const_type'];

Ivor