Categories > TinyButStrong general >

Problem with nested sub-blocks..

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Morrigan
Date: 2004-11-12
Time: 21:37

Problem with nested sub-blocks..

Hello, it's me again.
Here is my current problem, I was pretty sure I was doing it right but I keep getting an odd error.
Basically I have arrays of datas which contain keywords (for a lexicon) grouped by letters, and definitions.
It looks a bit like this:
$letters = array("A", "B", "C");   // etc
$WORDS["A"]["word1a"]        = "definition 1";
$WORDS["A"]["word2a"]        = "definition 2";
$WORDS["A"]["word3a"]        = "definition 3";
$WORDS["B"]["word1b"]        = "definition 1";
$WORDS["B"]["word2b"]        = "definition 2";
$WORDS["C"]["word1c"]        = "definition 1";
BUT, some (not all) words' definitions also have SUB-definitions, to display in a list, which would look like this:
$SUBWORDS["word2a"]["a"]     = "sub definition a";
$SUBWORDS["word2a"]["b"]     = "sub definition b";
There are even a few words with third levels of definitions, but I won't go into that now 'cause I can't even solve the second level. ;)

Anyway, so my code looks like this:
$TBS->MergeBlock("letters", "array", "letters");        // Main block: alphabetical letters
$TBS->MergeBlock("words", "array", "WORDS[%p1%]");    // Sub block: words & definitions grouped by letter
$TBS->MergeBlock("subwords", "array", "SUB_WORDS[%p1%]");    // Sub-sub block: lists of definitions under a word's definition
And my HTML is as follows:
[letters;block=begin]
<h4>[letters.val]</h4>
<ul>
    <li>
        <span class="lexiword">[lexicon.key;block=li;p1=[letters.val]]</span>
        <span class="lexidef"> ---[lexicon.val;htmlconv=no]</span>
        <ul class="lexidef">
            <li>[subwords.val;block=li;p1=[lexicon.key;htmlconv=no];magnet=ul;noerr]</li>
        </ul>
    </li>
</ul>
[letters;block=end]
The magnet tag is so that it doesn't display the <ul> in case there are NO sub-definitions. I want the end result to look like this:
A
* Word 1 --- definition 1
* Word 2 --- definition 2
      - subdef1
      - subdef2

B
* Word 1 --- def 1
* Word 2 --- def 2

etc...

The letters, and keywords/definitions, display properly, but not the subdefinitions. I get the following error:

"TinyButStrong Error (Block definition): [subwords.val] can not be defined because tag <li> or </li> is not found.

TinyButStrong Error (Array value): Can't merge [subwords.val] because there is no key named 'val'. This message can be cancelled using parameter 'noerr'."

So it doesn't find the <li> tag, or I'm doing something wrong?
Sorry if this is long-winded, or if I'm unclear. If I am just ask me what to explain.
Thanks!

(PS: there should be a Preview button on this forum!)
By: Skrol29
Date: 2004-11-13
Time: 01:08

Re: Problem with nested sub-blocks..

Hi Morrigan,

I've tested you snippet.
I had to change
   $TBS->MergeBlock("words"...
into
   $TBS->MergeBlock("lexicon"...
and
   $TBS->MergeBlock("subwords", "array", "SUB_WORDS[%p1%]");
into
   $TBS->MergeBlock("subwords", "array", "SUBWORDS[%p1%]");

But your bug does not come from that.
The merror message I had is different from yours (I have TBS 2.0):
TinyButStrong Error (MergeBlock [subwords]): Invalid query 'SUBWORDS[word1a]' because item 'SUBWORDS[word1a]' cannot be found.
This message is coherent. But I agree it would be smarter if TBS considere this case as an empty resultset instead of an error.

But anyway. This can not solve your problem with magnet.
Your magnet parameter can not work for two reasons. First: the value of subword.val is never nul or empty string. If there is no data then there is no section, it means no TBS fields to merge. And second: the magnet Html tags (ul) are outside the block (li) ; so TBS will not found the <ul>.

What I suggest to you is simply chnage the structure of $WORDS like this:
$WORDS["A"]["word1a"] = array('def=>'"definition 1",'subwords'=>array());
$WORDS["A"]["word2a"] = array('def=>'"definition 2",'subwords'=>array());
$WORDS["A"]["word3a"] = array('def=>'"definition 3",'subwords'=>array());
$WORDS["B"]["word1b"] = array('def=>'"definition 1",'subwords'=>array());
$WORDS["B"]["word2b"] = array('def=>'"definition 2",'subwords'=>array());
$WORDS["C"]["word1c"] = array('def=>'"definition 1",'subwords'=>array());

$WORDS["A"]["word1a"]["subwords"]["a"] = "sub definition a";
$WORDS["A"]["word1a"]["subwords"]["b"] = "sub definition b";