Menu ▾ ▴

#26 Errors with {foreach} loops

open
nobody
Templates (6)
5
2015-01-22
2001-08-25
No

Submitted by Noel Welsh on Fri, 17 Sep 1999 10:10:34
+0100

I'm getting very near to finishing some C to HTML
templates, but I've run into error processing if's
within foreach loops.

I have loops like

{foreach typedef sort "$a cmp $b;"}
{if first}
<h3 class=toc-section-title><a
name="types">Types</a></h3>
<ul>
{endif}
<li>
<p><span class=toc-entry><a
href="#type-{name}"><strong>{name}</strong></a></span><br>
<span
class=toc-summary><em>{brief}</em></span></p>
</li>
{if last}
</ul>
{endif}
{next}

to generate the table of contents. The first or
last directive is skipped, apparently at random,
leading to lists within lists that crawl towards
the right of the screen. It's probably my
stupidity, but I've been staring at it too long to
see what's wrong. Either that or it's gremlins. Or
there is a bug in Perceps. Attached below is the
full template. I plan to release these soon, in
case anyone is interested.

---

{keephtml}
<html>
<head>
<title>Description of {module}</title>
<link rel="stylesheet" type="text/css"
href="perceps.css" title="Default
Style">
</head>

<body>

<table width=100%>
<tr>
<td class=topic>
<h1>
Documentation
</h1>
</td>
<td class=title>
<h1>
{module}
</h1>
</td>
</tr>
</table>

<!-- Setup the quick links to the table of contents
-->
<p class=quicklink><a href="#macros">macros</a> <a
href="#types">types</a> <a
href="#variables">variables</a> <a
href="#functions">functions</a></p>

<!-- Get the rest of the module information. It must
be associated
with the #define MODULE -->
{foreach macro}
{if name /MODULE/}
<!-- <h2>Overview</h2> -->
<p class=overview><em>{brief}</em></p>
<p class=detail>{detail}</p>
{endif}
{next}

<hr class=section-separator>

<!-- Generate the table of contents -->
<!-- <h2>Table of Contents</h2> -->
{foreach macro}
{if first}
<h3 class=toc-section-title><a
name="macros">Macros</a></h3>
<ul>
{endif}
{if !name /MODULE/}
<li>
<p><span class=toc-entry><a
href="#macro-{name}"><strong>{name}</strong></a></span><br>
<span
class=toc-summary><em>{brief}</em></span></p>
</li>
{endif}
{if last}
</ul>
{endif}
{next}

{foreach typedef sort "$a cmp $b;"}
{if first}
<h3 class=toc-section-title><a
name="types">Types</a></h3>
<ul>
{endif}
<li>
<p><span class=toc-entry><a
href="#type-{name}"><strong>{name}</strong></a></span><br>
<span
class=toc-summary><em>{brief}</em></span></p>
</li>
{if last}
</ul>
{endif}
{next}

{foreach global sort "$a cmp $b;"}
{if first}
<h3 class=toc-section-title><a
name="variables">Variables</a></h3>
<ul>
{endif}
<li>
<p><span class=toc-entry><a
href="#variable-{name}"><strong>{name}</strong></a></span><br>
<span
class=toc-summary><em>{brief}</em></span></p>
</li>
{if last}
</ul>
{endif}
{next}

{foreach func sort "$a cmp $b;"}
{if first}
<h3 class=toc-section-title><a
name="functions">Functions</a></h3>
<ul>
{endif}
<li>
<p><span class=toc-entry><a
href="#function-{name}"><strong>{name}</strong></a></span><br>
<span
class=toc-summary><em>{brief}</em></span></p>
</li>
{if last}
</ul>
{endif}
{next}
<!-- end of table of contents -->

<!-- start of detail -->
<h2 class=detail-section-title>Macros Detail</h2>
{foreach macro sort "$a cmp $b;"}
{if public}
<p class=detail-entry><span
class=detail-scope>public
</span><a
name="macro-{name}"><strong>{name}</strong></a></p>
<p class=detail-summary><em>{brief}</em></p>
<p class=detail-text>{detail}</p>
<p class=detail-backlink><a href="#macros">Return
to
Macro Summary</a></p>
<hr class=subsection-separator>
{endif}
{next}

{foreach macro sort "$a cmp $b;"}
{if private}
<p class=detail-entry><span
class=detail-scope>private
</span><a
name="macro-{name}"><strong>{name}</strong></a></p>
<p class=detail-summary><em>{brief}</em></p>
<p class=detail-text>{detail}</p>
<p class=detail-backlink><a href="#macros">Return
to
Macro Summary</a></p>
<hr class=subsection-separator>
{endif}
{next}

<h2 class=detail-section-title>Types Detail</h2>
{foreach typedef sort "$a cmp $b;"}
{if public}
<p class=detail-entry><span
class=detail-scope>public
</span><a
name="type-{name}"><strong>{name}</strong></a></p>
<p class=detail-summary><em>{brief}</em></p>
<p class=detail-text>{detail}</p>
<p class=detail-backlink><a href="#types">Return
to Type
Summary</a></p>
<hr class=subsection-separator>
{endif}
{next}

{foreach typedef sort "$a cmp $b;"}
{if private}
<p class=detail-entry><span
class=detail-scope>private
</span><a
name="type-{name}"><strong>{name}</strong></a></p>
<p class=detail-summary><em>{brief}</em></p>
<p class=detail-text>{detail}</p>
<p class=detail-backlink><a href="#types">Return
to Type
Summary</a></p>
<hr class=subsection-separator>
{endif}
{next}

<h2 class=detail-section-title>Variables Detail</h2>
{foreach global sort "$a cmp $b;"}
{if public}
<p class=detail-entry><span
class=detail-scope>public
</span><a
name="variable-{name}"><strong>{name}</strong></a></p>
<p class=detail-summary><em>{brief}</em></p>
<p class=detail-text>{detail}</p>
<p class=detail-backlink><a
href="#variables">Return to
Variable Summary</a></p>
<hr class=subsection-separator>
{endif}
{next}

{foreach global sort "$a cmp $b;"}
{if private}
<p class=detail-entry><span
class=detail-scope>private
</span><a
name="variable-{name}"><strong>{name}</strong></a></p>
<p class=detail-summary><em>{brief}</em></p>
<p class=detail-text>{detail}</p>
<p class=detail-backlink><a
href="#variables">Return to
Variable Summary</a></p>
<hr class=subsection-separator>
{endif}
{next}

<h2 class=detail-section-title>Functions Detail</h2>
{foreach func sort "$a cmp $b;"}
{if public}
<p class=detail-entry><span
class=detail-scope>public
</span><a name="function-{name}"><strong>{type}
{name}({args})</strong></a></p>
<p class=detail-summary><em>{brief}</em></p>
{if return}
<h3 class=detail-return>Return Value:</h3>
<p class=detail-return-text>{return}</p>
{endif}
{if see}
<h3 class=detail-see-also>See Also:</h3>
<p class=detail-see-also-text>{see}</p>
{endif}
<p class=detail-text>{detail}</p>
<p class=detail-backlink><a
href="#functions">Return to
Function Summary</a></p>
<hr class=subsection-separator>
{endif}
{next}

{foreach func sort "$a cmp $b;"}
{if private}
<p class=detail-entry><span
class=detail-scope>private
</span><a name="function-{name}"><strong>{type}
{name}({args})</strong></a></p>
<p class=detail-summary><em>{brief}</em></p>
{if return}
<h3 class=detail-return>Return Value:</h3>
<p class=detail-return-text>{return}</p>
{endif}
{if see}
<h3 class=detail-see-also>See Also:</h3>
<p class=detail-see-also-text>{see}</p>
{endif}
<p class=detail-text>{detail}</p>
<p class=detail-backlink><a
href="#functions">Return to
Function Summary</a></p>
<hr class=subsection-separator>
{endif}
{next}
<!-- end of detail -->

</body>
</html>

Discussion

  • Thomas Bryan

    Thomas Bryan - 2001-08-25

    Sample input and output

     
  • Thomas Bryan

    Thomas Bryan - 2001-08-25

    Logged In: YES
    user_id=1197

    Submitted by Noel Welsh on Fri, 17 Sep 1999 14:34:44 +0100

    Hi Tom,

    > It may be a bug in PERCEPS. Note, though, that
    > you must write "sort" if you plan to use 'if
    > first' and 'if last'. For example, you have a
    > {foreach macro} followed by an {if first}. You
    > should say, {foreach macro sort} before you use
    > the {if first}. Otherwise, I don't know how the
    > if first and if last will behave. Fix that
    > first.

    Ok, it's fixed, but the lists still don't get
    closed. I've attached the latest version, plus
    sample input and output.

     

Log in to post a comment.