Forum OpenACS Q&A: Response to if/else tags in adp

Posted by Lars Pind on

I didn't know about this. Thanks for catching and letting us know.

When troubleshooting things like this, I usually copy the packages/acs-templating/www/doc/demo/compile.tcl script to the directory where my templates are, and then request compile?file=foo.adp. This gives you the code being generated by from the ADP template, and makes it a lot easier to troubleshoot what's going on.

In this example, I have this template:

<% set foo 1 %>

<if @foo@ true>Something</if>

Here's a bit of html.

<else>Something else.</else>

Which is translated into:

set __adp_output ""

set foo 1

if {[template::util::is_true "${foo}"]} {

append __adp_output "Something"


append __adp_output "

Here's a bit of html.

" else {

append __adp_output "Something else."

set __adp_output

So you can see what's happening: The <else> tag just appends " else {...}" to whatever line happens to be the latest one written so far. Since that in this case is the "append __adp_output" command, the else block is simply appended.

Here's the implementation of the tag:

template_tag else { chunk params } {

  template::adp_append_code "else {" -nobreak

  template::adp_compile_chunk $chunk

  template::adp_append_code "}"


So what would it take to fix? We'd have to save either the expression or the result of evaluating the expression from the <if> tag onto some sort of stack, and then pop the stack in the else tag.

Not impossible. And the current implementation of the <else> tag is, to say the least, not very robust.

Of course, it's easy to change the ADP file to not do this. You can just have two <if> chunks with inverted arguments.

Comments, others?