- <? if($variable) { ?>
Output/execute the text until "<? } ?>" only if $variable is boolean and true.
+- <? foreach($variable) { ?>
+ If $variable is an "IterableDataset"
+ Output/execute the text until "<? } ?>" repeated as $variable suggests.
+ Special variables that $variable defines can be used in the inner text.
+
+
+
\ No newline at end of file
--- /dev/null
+package org.cacert.gigi.output.template;
+
+import java.io.PrintWriter;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.cacert.gigi.Language;
+import org.cacert.gigi.output.Outputable;
+
+final class ForeachStatement implements Outputable {
+ private final String variable;
+ private final TemplateBlock body;
+
+ ForeachStatement(String variable, TemplateBlock body) {
+ this.variable = variable;
+ this.body = body;
+ }
+
+ @Override
+ public void output(PrintWriter out, Language l, Map<String, Object> vars) {
+ Object o = vars.get(variable);
+ if (o instanceof IterableDataset) {
+ IterableDataset id = (IterableDataset) o;
+ Map<String, Object> subcontext = new HashMap<String, Object>(vars);
+ while (id.next(subcontext)) {
+ body.output(out, l, subcontext);
+ }
+ }
+ }
+}
\ No newline at end of file
--- /dev/null
+package org.cacert.gigi.output.template;
+
+import java.util.Map;
+
+/**
+ * Represents some kind of data, that may be iterated over in a template.
+ */
+public interface IterableDataset {
+ /**
+ * Moves to the next Dataset.
+ *
+ * @param vars
+ * the variables used in this template. They need to be updated
+ * for each line.
+ * @return true, iff there was a data-line "installed". False of this set is
+ * already empty.
+ */
+ public boolean next(Map<String, Object> vars);
+}
long lastLoaded;
File source;
- private static final Pattern IF_PATTERN = Pattern.compile(" ?if\\(\\$([^)]+)\\) ?\\{ ?");
+ private static final Pattern CONTROL_PATTERN = Pattern.compile(" ?([a-z]+)\\(\\$([^)]+)\\) ?\\{ ?");
public Template(URL u) {
try {
buf.delete(buf.length() - 2, buf.length());
String com = buf.toString().replace("\n", "");
buf.delete(0, buf.length());
- Matcher m = IF_PATTERN.matcher(com);
+ Matcher m = CONTROL_PATTERN.matcher(com);
if (m.matches()) {
- final String variable = m.group(1);
- final TemplateBlock body = parse(r);
- commands.add(new IfStatement(variable, body));
+ String type = m.group(1);
+ String variable = m.group(2);
+ TemplateBlock body = parse(r);
+ if (type.equals("if")) {
+ commands.add(new IfStatement(variable, body));
+ } else if (type.equals("foreach")) {
+ commands.add(new ForeachStatement(variable, body));
+ } else {
+ throw new IOException("Syntax error: unknown control structure: " + type);
+ }
continue;
}
if (com.matches(" ?\\} ?")) {
import java.io.IOException;
import java.io.PrintWriter;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
+import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.cacert.gigi.Certificate;
+import org.cacert.gigi.User;
+import org.cacert.gigi.database.DatabaseConnection;
import org.cacert.gigi.output.ClientCSRGenerate;
+import org.cacert.gigi.output.template.IterableDataset;
import org.cacert.gigi.pages.LoginPage;
import org.cacert.gigi.pages.Page;
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
HashMap<String, Object> vars = new HashMap<String, Object>();
vars.put("CCA", "<a href='/policy/CAcertCommunityAgreement.html'>CCA</a>");
- getDefaultTemplate().output(resp.getWriter(), getLanguage(req), vars);
+
+ User u = LoginPage.getUser(req);
+ try {
+ PreparedStatement ps = DatabaseConnection.getInstance().prepare(
+ "SELECT `id`,`email` from `email` WHERE `memid`=? AND `deleted`=0");
+ ps.setInt(1, u.getId());
+ final ResultSet rs = ps.executeQuery();
+ vars.put("emails", new IterableDataset() {
+
+ @Override
+ public boolean next(Map<String, Object> vars) {
+ try {
+ if (!rs.next()) {
+ return false;
+ }
+ vars.put("id", rs.getString(1));
+ vars.put("value", rs.getString(2));
+ return true;
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ return false;
+ }
+ });
+ getDefaultTemplate().output(resp.getWriter(), getLanguage(req), vars);
+ rs.close();
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
}
@Override
<td><?=_Add?></td>
<td><?=_Address?></td>
</tr>
-<!-- For each mail -->
+<? foreach($emails) { ?>
<tr>
- <td><input type="checkbox" id="addid<=intval($row['id'])>" name="addid[]" value="<=intval($row['id'])>"></td>
- <td align="left"><label for="addid<=intval($row['id'])>"><=sanitizeHTML($row['email'])></label></td>
+ <td><input type="checkbox" id="addid<?=$id?>" name="addid[]" value="<?=$id?>"></td>
+ <td align="left"><label for="addid<?=$id?>"><?=$value?></label></td>
</tr>
-<!-- For each mail -->
+<? } ?>
+
<? if($points50) { ?>
<tr>
<td colspan="2" align="left">