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

Support string concatenation as an operator #222

Closed
lettenj61 opened this issue Oct 23, 2017 · 13 comments
Closed

Support string concatenation as an operator #222

lettenj61 opened this issue Oct 23, 2017 · 13 comments

Comments

@lettenj61
Copy link

Hello, I love Tera and Gutenberg. They're awesome!

I'm writing a Gutenberg theme, and I found that it will be useful if Tera supports string concatenation as we can in Jinja2, but I wonder is it acceptable or not.

I'm thinking of a template something like:

{{ "Hello" ~ " world" }}

I wrote some examples and unit tests on my fork, but I believe there's v0.11 coming so they may be outdated.

@Keats
Copy link
Owner

Keats commented Oct 24, 2017

The Jinja2 doc say the following:

+
Adds two objects together. Usually the objects are numbers, but if both are strings or lists, you can concatenate them this way. This, however, is not the preferred way to concatenate strings! For string concatenation, have a look-see at the ~ operator. {{ 1 + 1 }} is 2.

Any idea why + is not the preferred way?

I wrote some examples and unit tests on my fork, but I believe there's v0.11 coming so they may be outdated.

Yeah, any work should be done on the 0.11 branch. It's mostly waiting for pest to release a stable version but it almost ready otherwise

@lettenj61
Copy link
Author

@Keats

Any idea why + is not the preferred way?

I feel totally OK with +, I just blindly followed Jinja2 syntax.

To be honest, I have no experience of Python / Django / Jinja2, so I have no idea why they discourage + for strings. I guess one of the reasons is that Python normal operator fails at runtime when you try to add non-numbers.

For Tera internals, if we use + for strings, we must treat MathExprs specially at rendering when either of lhs or rhs would not be a number. My idea is based on introducing new Concat node for the AST, I'm not sure which way is the better.

I understood about next version. I'll work on v0.11 branch.

@Keats
Copy link
Owner

Keats commented Nov 3, 2017

Hmm thinking about it we should maybe use ~ so we get the same syntax highlighting as Jinja2

@Keats
Copy link
Owner

Keats commented Nov 7, 2017

@lettenj61 do you need help with that?

@lettenj61
Copy link
Author

@Keats Sorry for the late, I'm working on it but I couldn't get much time. I hope I can open a PR in a next few days.

I agree with ~ for syntax highlighting purpose.

@Keats
Copy link
Owner

Keats commented Nov 24, 2017

Let me know if you have any questions with pest or tera!

@verpeteren
Copy link

I like to use this feature in a project that I am working on.
(I want to concat two variables into one macro argument)

@lettenj61, I saw that you recently picked this up with the string-concat2 branch. Can I assist in some way?

@lettenj61
Copy link
Author

@verpeteren That's the reason why I wanted this in tera, too!

I'm working on adding StringConcat node to tera AST, and I have design question to @Keats about concatenation operators. I will post about that here as soon as I get organized my thoughts.

@verpeteren
Copy link

I encountered another situation where I could use such a concatination operator. I've resolved to a workaround but it is not so nice. :-(

@lettenj61:
I thought that a lot could be reused from your previous attempts.
What is on your mind?
Is there something that I can do to assist you with on this?

@Keats
Copy link
Owner

Keats commented Apr 13, 2018

@lettenj61 Any progress? I'd like to include that feature in the next release if possible

@travismiller
Copy link

This would be a great feature to have! 👍

In my own string-concat2 branch, I've picked up where the original string-concat2 branch leaves off to see where I could get. If it helps, feel free to lift what's there or I'm happy to submit a PR to @lettenj61's fork or to the upstream.

I'm new to Rust so I only barely know what I'm doing. 😄

@Keats
Copy link
Owner

Keats commented May 11, 2018

Ok let's get that done!
The current PR is outdated and I think the comments on it still stand (ie don't put the ~ as a math operator`)

@travismiller
Copy link

@Keats I've submitted the PR for your review and feedback.

@Keats Keats closed this as completed Jun 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants