diff options
| -rw-r--r-- | README.md | 28 | ||||
| -rw-r--r-- | ctemplates.c | 12 |
2 files changed, 34 insertions, 6 deletions
@@ -96,6 +96,34 @@ Adds a varlist that should be used one iteration through the loop Turns a template and varlist into a string. the returned char\* should NOT be freed. The returned char\* is only valid until TMPL\_render() is called again. If you need it even after TMPL\_render() is called, copy it. The length of the returned string is put into `length` to help in copying. +### Templating tags + + <TMPL_VAR name="variable_name" default="default_value"> + +Substitutes a variable, with an optional default value. + +Prints the variable named "variable\_name" in it's place, if no variable named "variable\_name" is found, the default printed. If there is not variable name, and no default value, an error is logged, and it is expanded to an empty string. (If you need to have nothing printed when the variable does not exist, you should use `default=""` to keep the error log clean. + + <TMPL_IF name="variable_name" value="check_value"> + : + <TMPL_ELSEIF name="variable_name" value="check_value"> + : + <TMPL_ELSE> + : + <TMPL_END> + +Branching statements, checks if a variable is equal to a constant value. + +Checks if the variable "variable\_name" contains the string "check\_value". There is currently no way to check if two variables are equal. Elseif and Else clauses are optional. All `TMPL_IF` statements must be closed with `TMPL_END`. + + <TMPL_LOOP name="loop_name"> + : + <TMPL_END> + +Loop statements, I bet you can't figure out what this does. + +Loops through a loop named "loop\_name", each iteration through the loop, only the varlist added to the loop is visible. That is, no other varlist, even the parent varlist, is visible while inside the loop. + <section id="Examples"></section> ## Examples diff --git a/ctemplates.c b/ctemplates.c index faf4334..dd981ac 100644 --- a/ctemplates.c +++ b/ctemplates.c @@ -680,10 +680,10 @@ parse_variable(struct TMPL_token* head, struct TMPL_buf* errbuf){ int attribs_length = head->length - TAG_VAR_LENGTH; int in_name = kmp(start_of_attribs,attribs_length,ATTRIBUTE_VARNAME,ATTRIBUTE_VARNAME_LENGTH); int in_default = kmp(start_of_attribs,attribs_length,ATTRIBUTE_DEFAULT,ATTRIBUTE_DEFAULT_LENGTH); - if(in_name == -1 && DEBUGGING){ - char buf[ERR_MSG_LEN]; - snprintf(buf,ERR_MSG_LEN,"Parsing error: Could not find name=\"name\" near %s\n",head->start); - bputs(errbuf,buf); + if(in_name == -1){ + bputs(errbuf,"Parsing error: Could not find \"name\" field in variable near"); + bputsn(errbuf,head->start,ERR_MSG_LEN); + bputs(errbuf,"\n"); } if(in_name >= 0){ char* start_of_name = start_of_attribs + in_name + ATTRIBUTE_VARNAME_LENGTH; @@ -830,10 +830,10 @@ compile(char* tmplstr){ ret->out = alloc_tmpl_buf(); ret->errout = alloc_tmpl_buf(); struct TMPL_token* tokens = TMPL_tokenize(tmplstr,slen); - print_tokens(tokens); struct TMPL_tagnode* ast = parse(tokens,ret->errout); - print_ast(ast); if(ast == NULL){ + //size_t dummy; + //printf("error: %s\n",bstringify(ret->errout,&dummy)); }else{ ret->roottag = ast; } |
