static uintmax_t on_fortune_section(mustache_api_t *api, void *userdata, mustache_token_section_t *token) { fortune_ctx_t * const fortune_ctx = userdata; uintmax_t ret = 1; if (fortune_ctx->num_result) { assert(fortune_ctx->result); const list_t *iter = fortune_ctx->result; do { fortune_ctx->fortune_iter = H2O_STRUCT_FROM_MEMBER(fortune_t, l, iter); iter = iter->next; if (!mustache_render(api, fortune_ctx, token->section)) { ret = 0; break; } } while (iter); } return ret; }
uintmax_t tests_sectget(mustache_api_t *api, void *userdata, mustache_token_section_t *token) { if(strcmp("section", token->name) == 0) { // call render on inner section as many times as needed // 'api' and 'userdata' can be redefined return mustache_render(api, userdata, token->section); } return 0; // error }
int main(int argc, char** argv) { ///* pmustache m = mustache_init(); char *text = NULL; //text = text_tag_variable; //text = text_tag_variable_no_escape; //text = text_tag_variable_no_escape_andsign; //text = text_tag_comment; //text = text_tag_comment_multiline; //text = text_tag_variable_error; //text = text_tag_variable_error2; //text = text_tag_section; //text = text_tag_section_inverted; //text = text_big; text = text_tag_delimiter; mustache_set(m, "name", "António"); mustache_set(m, "value", "123.33"); mustache_set(m, "taxed_value", "50"); mustache_set(m, "html", "<script type=\"text/javascript\" src=\"acs.js\" />"); //mustache_set(m, "in_ca", "true"); mustache_load_txt(m, text); mustache_render(m); printf("Rendered text: %s\n", m->text_parsed); //*/ /* char *text = NULL; text = mustache_get_contents("src/tpl.mustache"); //text = mustache_get_contents("src/tpl_large.mustache"); printf("Text: %s\n", text); free(text); */ return (EXIT_SUCCESS); }
static result_return_t on_fortune_result(db_query_param_t *param, PGresult *result) { fortune_ctx_t * const fortune_ctx = H2O_STRUCT_FROM_MEMBER(fortune_ctx_t, param, param); int ret = DONE; const ExecStatusType status = PQresultStatus(result); if (status == PGRES_TUPLES_OK) { const size_t num_rows = PQntuples(result); ret = SUCCESS; for (size_t i = 0; i < num_rows; i++) { fortune_t * const fortune = h2o_mem_alloc_pool(&fortune_ctx->req->pool, sizeof(*fortune)); if (fortune) { memset(fortune, 0, sizeof(*fortune)); fortune->id.base = PQgetvalue(result, i, 0); fortune->id.len = PQgetlength(result, i, 0); fortune->message = h2o_htmlescape(&fortune_ctx->req->pool, PQgetvalue(result, i, 1), PQgetlength(result, i, 1)); fortune->l.next = fortune_ctx->result; fortune_ctx->result = &fortune->l; fortune_ctx->num_result++; if (!i) fortune->data = result; } else { send_error(INTERNAL_SERVER_ERROR, REQ_ERROR, fortune_ctx->req); ret = DONE; if (!i) PQclear(result); break; } } } else if (result) { LIBRARY_ERROR("PQresultStatus", PQresultErrorMessage(result)); send_error(BAD_GATEWAY, DB_ERROR, fortune_ctx->req); PQclear(result); } else { mustache_api_t api = {.sectget = on_fortune_section, .varget = on_fortune_variable, .write = add_iovec}; thread_context_t * const ctx = H2O_STRUCT_FROM_MEMBER(thread_context_t, event_loop.h2o_ctx, fortune_ctx->req->conn->ctx); const size_t iovcnt = MIN(MAX_IOVEC, fortune_ctx->num_result * 5 + 2); const size_t sz = offsetof(iovec_list_t, iov) + iovcnt * sizeof(h2o_iovec_t); char _Alignas(iovec_list_t) mem[sz]; iovec_list_t * const restrict iovec_list = (iovec_list_t *) mem; memset(iovec_list, 0, offsetof(iovec_list_t, iov)); iovec_list->max_iovcnt = iovcnt; fortune_ctx->iovec_list_iter = iovec_list; fortune_ctx->result = sort_fortunes(fortune_ctx->result); if (mustache_render(&api, fortune_ctx, ctx->global_data->fortunes_template)) { fortune_ctx->iovec_list = iovec_list->l.next; set_default_response_param(HTML, fortune_ctx->content_length, fortune_ctx->req); h2o_start_response(fortune_ctx->req, &fortune_ctx->generator); const h2o_send_state_t state = fortune_ctx->iovec_list ? H2O_SEND_STATE_IN_PROGRESS : H2O_SEND_STATE_FINAL; h2o_send(fortune_ctx->req, iovec_list->iov, iovec_list->iovcnt, state); } else send_error(INTERNAL_SERVER_ERROR, REQ_ERROR, fortune_ctx->req); } return ret; }