Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Execute statement #160

Closed
wants to merge 4 commits into from
Closed

Conversation

DeiRex75
Copy link
Contributor

@DeiRex75 DeiRex75 commented Aug 5, 2020

Similar to the set statement i was in need of a execute statement.

The execute statement executes a given expression but doesn't render it.
This helps to call functions that just want to be executed.
For example, adding a debug function that dumps the object but doesn't change the result.

I would love to have this or something similar added.

{% execute a_dump_function(data) -%} Something was dumped.

Would just result in "Something was dumped."

This use case is just an example. Not a good one to be honest.
The issue is that currently no option is available to just call a function without rendering something.
Using an expression and returning "" doesn't allow for whitespace control.

@pantor
Copy link
Owner

pantor commented Aug 10, 2020

Thanks for your PR!

I think I would integrate your execution statement within the callback approach. I've added a void callback to the master branch, maybe you can have a look at this. An example for that would be:

// You can also add a callback without return variable, e.g. for debugging:
env.add_void_callback("log", 1, [greet](Arguments args) {
	std::cout << "logging: " << args[0] << std::endl;
});
env.render("{{ log(neighbour) }}", data); // Prints nothing to result, only to cout...

@DeiRex75
Copy link
Contributor Author

DeiRex75 commented Aug 10, 2020

Overall love your approach.
But the major part i wanted from this was the whitespace control.

I use template files.
Therefore i would have to write:

{{ log("A") }}{{ log("B") }}{{ log("C") }}Lorem Ipsum

To process the logging and write the text without additional lines.
What i would like to write is:

{{ log("A") }}
{{ log("B") }}
{{ log("C") }}
Lorem Ipsum

to result just in "Lorem Ipsum".
Whitespace control is only possible for statements.
Therefore another possibility to result not in multiple lines would be to add a statement with control at the end of each logging line.

Another idea could be to remove the newline after an expression if "-}" or "-}}" is used to close it.
This would have to be explicit and not be affected by trim_blocks

@pantor
Copy link
Owner

pantor commented Aug 11, 2020

Ah, I see.

I think we should follow jinja2 here and add whitespace control to variables as well. Btw, It is quite hidden in their documentation that it works for variables as well.

@DeiRex75
Copy link
Contributor Author

Thanks for the information again.
If it is in jinja2 i would love to see this implemented as well.
I will close this for now.

@DeiRex75 DeiRex75 closed this Aug 11, 2020
@DeiRex75 DeiRex75 deleted the execute_statement branch August 13, 2020 09:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants