This is:
Being able to put conditions in a template block rather than in the code.
Current macros only support replacement and repeats (clone, replace, set).
I want to start a discussion about this before I might implement it and send a PR.
I guess it would look something like that:
In the docx "template":
Guests:
${IF GUESTS>3}
Don't forget to make a special arrangement for more than 3 guests!
${/ENDIF}
In the code:
$templateProcessor->setVariable('GUESTS', 4);
This could be extended by several operands (<, >, <=, >=, !=, ==) or even nested logical expressions.
Pro:
More transparent templates (view logic where it belongs)
Con:
More complexity in template processor
Why do you want to put this in the template file? I think this kind of logic is fairly "edge-case" and to implement this level of conditional logic into the library is a bit hectic and will add to maintenance complexity.
Doing this yourself in the current code is quite easy:
Guests:
${MORE_THAN_3_GUESTS_BLOCK}
Don't forget to make a special arrangement for more than 3 guests!
${/MORE_THAN_3_GUESTS_BLOCK}
PHP
if(count($guests) > 3) {
$templateProcessor->cloneBlock('MORE_THAN_3_GUESTS_BLOCK', 1);
} else {
$templateProcessor->deleteBlock('MORE_THAN_3_GUESTS_BLOCK');
}
Not sure how many cases you have where you'd need this kind of conditional logic, but I'm pretty sure it will be easier to manage the code in your controller than trying to implement it into the library and your templates.
I support this request.
We create different PHP Sources for documents and create different documents out of one source.
So it's easier to create conditions in the template and not in php code.
I support this request.
We create different PHP Sources for documents and create different documents out of one source.
So it's easier to create conditions in the template and not in php code.
I agree with @rkorebrits ,and @gruessung can fit your requirement with this way:
set a rule like "greater_books_3", they are separated by "_", the first part means "condition", it can be some values such as "greater", "equals". Second part means your variable, in this case, it means "books", third part means numbers, and that you can code your php with this rule
@icy2003 @rkorebrits Ofc I am fully aware how this can be done in the code rather than in the template (as mentioned in my original PR "...rather than in the code."). The reason why this makes sense is the same reason templating languages exist at all: separation of concerns.
You want to get your data together in the code and feed it to a template that executes the "presentation" logic. This way it's easier to debug, maintain and it's separated in modules by function. It also enforces a good programming practice by keeping business logic away from presentation logic.
Better
Guests:
${IF GUESTS>3}
Don't forget to make a special arrangement for more than 3 guests!
${/ENDIF}
or
Guests:
{IF $GUESTS>3}
Don't forget to make a special arrangement for more than 3 guests!
{/ENDIF}
To avoid mixing variables and functions.
Better
Guests: ${IF GUESTS>3} Don't forget to make a special arrangement for more than 3 guests! ${/ENDIF}or
Guests: {IF $GUESTS>3} Don't forget to make a special arrangement for more than 3 guests! {/ENDIF}To avoid mixing variables and functions.
is this the next version function?
Better
Guests: ${IF GUESTS>3} Don't forget to make a special arrangement for more than 3 guests! ${/ENDIF}or
Guests: {IF $GUESTS>3} Don't forget to make a special arrangement for more than 3 guests! {/ENDIF}To avoid mixing variables and functions.
I don't see the difference in the first one? Did you mean the first "or" the second one or the second one instead of the first one.
The reason why I did it that way was because of the way the Template Processor is written right now. Every macro must start with a Dollar sign. The additional logic would be quiet easy to add with a whitelist of key word (functions such as IF) and a check of operands for this function (as regex for example). Also an open/close sanity check (e.g. IF/ENDIF) would have to be added.
Any other issues someone sees with this feature?
Why do you want to put this in the template file? I think this kind of logic is fairly "edge-case" and to implement this level of conditional logic into the library is a bit hectic and will add to maintenance complexity.
Doing this yourself in the current code is quite easy:
Guests: ${MORE_THAN_3_GUESTS_BLOCK} Don't forget to make a special arrangement for more than 3 guests! ${/MORE_THAN_3_GUESTS_BLOCK}PHP
if(count($guests) > 3) { $templateProcessor->cloneBlock('MORE_THAN_3_GUESTS_BLOCK', 1); } else { $templateProcessor->deleteBlock('MORE_THAN_3_GUESTS_BLOCK'); }Not sure how many cases you have where you'd need this kind of conditional logic, but I'm pretty sure it will be easier to manage the code in your controller than trying to implement it into the library and your templates.
This doesn't seem to work within a numbered list.
@DIDoS any update on implementing this feature?
@troosan any idea how I could make your suggestion work with numbered lists?
Most helpful comment
Why do you want to put this in the template file? I think this kind of logic is fairly "edge-case" and to implement this level of conditional logic into the library is a bit hectic and will add to maintenance complexity.
Doing this yourself in the current code is quite easy:
PHP
Not sure how many cases you have where you'd need this kind of conditional logic, but I'm pretty sure it will be easier to manage the code in your controller than trying to implement it into the library and your templates.