Categories > TinyButStrong general >

if then else with megreField

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Lomiren
Date: 2009-06-17
Time: 21:24

if then else with megreField

Hi. ssory im not good speak englesh but i have problem:
This is work code(not full)
glob & stat is mergetField
data is meget block
<form action="[glob.lang]/[stat.id]">
<div>[data.id;block=div]</div>
</form>
[stat.id]

but if i use
[stat.norecords; if [val] == '1';then '
no records
';else '
<form action="[glob.lang]/[stat.id]">
<div>[stat.id][data.id;block=div]</div>
</form>

';]
[stat.id]

in PHP stat is array('id'=>some_variable, 'norecords' => 1/0)

if stat.norecords = 1

TBS output is OK

but if stat.records = 0

in output TBS not show [stat.id] in FORM "action" and in merget DIV
but then blok IF is end TBS show [stat.id]...  where is problem?

and is this sole IF use in TBS? how about
[var.name; if [val]='';then]
some actions
[else]
some actions
[endif]
By: Lomiren
Date: 2009-06-17
Time: 21:32

Re: if then else with megreField

OK - it means TBS is show [stat.id] value

and in form action and in DIV TBS output
&#91;stat.id]
By: Skrol29
Date: 2009-06-18
Time: 11:28

Re: if then else with megreField

Hi Lomiren;

I've tested your snippet with TBS 3.4.0 and it works ok form me when stat.norecords = 0. I have no
&#91;stat.id]
.

TBS is replacing "[" with "&#91;" in data injected into the template. This is a default behavior which can be stopped using parameter "protect=no".
But in your snippet, this protection should not happen since the data do not come a injected data (a merged field) but from piece of the template itself (the "then" parameter). I do not have to protection in my test, and I'm surprised you have it.
By: Lomiren
Date: 2009-06-18
Time: 12:42

Re: if then else with megreField

I do not understand is what - why the check " stat.norecords; if [val] == '1' " passes, TBS see value [stat.norecords], but do not insert the value [stat.id]

simple PHP
$TBS->mergeBlock('glob', array('lang'=>'rus');
$TBS->mergeBlock('stat', array('id'=>9, 'noerror'=>0));
$TBS->mergeField('data', array('id'=>8388);

TPL
[stat.norecords; if [val] == '1';then '
no records
';else '
<form action="[glob.lang]/[stat.id]">
<div>[stat.id][data.id;block=div]</div>
</form>

';]
[stat.id]


Output ( with comments in(!!) )

<form action="rus(!!correct repalse!!)/&#91;stat.id](!!not replase [stat.id]!!)">
<div>[stat.id](!!not replase [stat.id]!!)8388</div>
</form>

9(!!correct repalse!!)

why TBS replase mergetField 'glob' and not replase in 'form action' and in 'div'? but replase after form?
By: RwD
Date: 2009-06-18
Time: 13:16

Re: if then else with megreField

I think the problem lies in the else block where you define a field from the block you are merging. I assume that after tbs processed stat.norecords it only looks beyond that place in the source, so past the just inserted else text.

So assume:
- [tag] is replaced with "lala[anothertag]lala"
- |tbs>> = the current position in the source where tbs is looking at

Before the merge of [tag]:
Some text |tbs>> [tag] some more text

After merging [tag]:
Some text lala[anothertag]lala |tbs>> some more text

As you can see tbs passed the tag you insert. I looked into this specific piece of code just today, so I'm pretty certain this is the way tbs works, though skorl29 can probably explain what really happens with more confidence ;)
By: Skrol29
Date: 2009-06-18
Time: 13:32

Re: if then else with megreField

Hi,

I've tested your snippet (PHP and TPL) and it returns TBS errors.
I've tryed with PHP:
$TBS->mergeField('glob', array('lang'=>'rus'));
$TBS->mergeField('stat', array('id'=>9, 'norecords'=>0));
$TBS->mergeField('data', array('id'=>8388));

And it gives the result:
<form action="rus/[stat.id]">
<div>[stat.id]8388</div>
</form>


9

[stat.id] is not merged in this case. This is because it is embedded into a [stat;xxx] field. TBS is merging embedded fields only in certain cases:
- [var] fields embedded into parameters "file" , "script" , "subtpl" , "if" , "then" , "else" , "when"
- fields merged with MergeBlock() and no risk of infinite loop.

The following snippet is working for me:
PHP:
$TBS->mergeField('glob', array('lang'=>'rus'));
$TBS->mergeBlock('stat', array(array('id'=>9, 'norecords'=>0)));
$TBS->mergeField('data', array('id'=>8388));

TPL :
<div>
[stat.norecords;block=div; if [val] == '1';then '
no records
';else '
<form action="[glob.lang]/[stat.id]">
<div>[stat.id][data.id;block=div]</div>
</form>

';]
[stat.id]
</div>

I think the "&#91;" you've got comes from another process (TBS or not) you are doing before or after the test.