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

Variable names with space #48

Closed
gebes opened this issue Dec 7, 2024 · 17 comments
Closed

Variable names with space #48

gebes opened this issue Dec 7, 2024 · 17 comments
Assignees
Labels
bug Something isn't working enhancement New feature or request

Comments

@gebes
Copy link

gebes commented Dec 7, 2024

Hello!

Up until version 1.2.5, it was possible to use variable names with spaces in between, like the following, to make templates more readable for users:

{{Customer Firstname}} {{Customer Surname}}

Could you please bring this feature back? Every version after 1.2.5 no longer supports it.

@bobiverse
Copy link
Owner

bobiverse commented Dec 8, 2024 via email

@bobiverse bobiverse self-assigned this Dec 9, 2024
@bobiverse bobiverse added bug Something isn't working enhancement New feature or request labels Dec 9, 2024
@gebes
Copy link
Author

gebes commented Jan 16, 2025

Hey! Any chance you can fix this?

@bobiverse
Copy link
Owner

It's a bit tricky.

Don't you have control over the incoming data?
For example:

  1. Replace data in golang code incoming data key Customer Firstname --> CustomerFirstname.
  2. (Also before rendering, you can replace data key strings.Replace(..) as you wish)
  3. And then replace in .docx file {{Customer Firstname}} --> {{Customer.Firstname}} (or {{CustomerFirstname}})

If you chose to use struct option then document would contain variable like this {{Customer.Firstname}} (almost readable as space :D)

@gebes
Copy link
Author

gebes commented Jan 16, 2025

I could, but it will require migrating a lot of documents and communicating with people who are using those. This way, it's more readable to the average user, who is already struggling with using Word documents at all.

It would be great, but if not, I will have to stick to the older versions. Or maybe I can fix it? I’m not familiar with the code, though.

@bobiverse
Copy link
Owner

who is already struggling with using Word documents at all.
:D ok, i know this struggle.

I have a few ideas. Let me try them out, and I'll get back to you next week. 🏃

@bobiverse
Copy link
Owner

Hi @gebes and sorry!
I was on a roll to fix this issue, and after running tests it became clear that it's not possible to use space-separated param keys.

The problem is that there are now various possible param keys with flavors:

  • {{SimpleKey}}
  • {{My.Struck.Key}}
  • {{ListVariables / }} ❗ which is blocker for your issue fix
  • {{KeysWithTriggers :empty:remove:row}}
  • {{Mega.Complex.With.All - :empty:remove:row}}

❗ Separator feature

Which is necessary for any kind of slices: strings, numbers.

0. {{ListVariables}} – default list

Results in: ABCDE

1. {{ListVariables /}} – no space

Results in: A/B/C/D/E

2. {{ListVariables / }} – suffix space is part of the separator now

Results in: A /B /C /D /E

No solution

Because a space can be part of the separator, it becomes clear that param keys must not contain a space.

🙇

@gebes
Copy link
Author

gebes commented Jan 21, 2025

Oh, still, thanks for doing this, @bobiverse! I really appreciate it.

  1. {{ListVariables /}} – no space
    Results in: A/B/C/D/E
  1. {{ListVariables / }} – suffix space is part of the separator now
    Results in: A /B /C /D /E

Wouldn't it make more sense, to have {{ListVariables "/ "}}? Makes it clearer while reading

Could you create more detailed documentation on all of those features? It would be really helpful

Also, as an idea: is there a way to include if clauses? For example, if I want to show a paragraph only when a certain condition is met?

@bobiverse
Copy link
Owner

It all started with a single format, {{SingleKey}}. I agree that features were added incrementally along the way, and so far, there hasn’t been a quility refactoring.

Now I might add "/ " to the format, but for compatibility reasons, I’ve left some formats it as is. 🤷

There were a few suggestions to add conditional blocks, but I’m not a fan. I’d rather keep the logic outside the template and use simple triggers like {{MyKey :empty:remove:row}} (DOCUMENTATION!!!). Honestly, I'm waiting for someone else to implement it. Maybe after that, I'll warm up to the idea.


Thanks for the feedback—it's motivating me to:

  • Write proper documentation.
  • Rethink the placeholder format.

@bobiverse
Copy link
Owner

💡 Quick Idea:

In your template, do you only use a simple key format such as:

  • {{Simple}}
  • {{Customer Firstname}}

(without the separators and triggers I mentioned before)?

I could add something like template.UseBasicMode(), which would use placeholders exactly as they appear:

  • {{Simple}}
  • {{Customer.Firstname}}
  • {{Customer Firstname}} (would be replaced too)
  • {{Footer note on every page}} (would also be replaced)
    This would avoid more complex parameter matching.

@gebes
Copy link
Author

gebes commented Jan 21, 2025

That would work well! However, to make it a bit more well-rounded, I think creating a template.SetArgDelimiter, which defaults to a whitespace, would be a cleaner solution. For instance, I would set the delimiter to "," or something similar.

@bobiverse
Copy link
Owner

ah! Like {{Customer Name,/ ,:empty:bla:bla}} ?

@gebes
Copy link
Author

gebes commented Jan 21, 2025

Exactly

@gebes
Copy link
Author

gebes commented Jan 21, 2025

While playing around with the new version, I noticed an issue with tables like these:

Name;Age
{{E27.Firstname}} {{E27.Surname}};{{E27.Age}}{{E27.Birthday}}

or

{{E.Firstname}} {{E.Surname}};{{E.Age}}{{E.Birthday}}

These tables worked well in the previous version 1.2.5, but in 1.4.4 they don't work at all somehow? Did something change?

Additionally, I have a feature idea: E is the array of objects, and E27 means the table should have at least 27 lines. If I only have 10 rows, it would add 17 empty rows. This is currently solved by adding 50 E arrays (from 1 to 50).

@bobiverse
Copy link
Owner

See: https://github.com/bobiverse/docxplate/tree/saving-private-gebes
I got an idea and looks good. Current tests passes, but noticed other issues.

Image

Could you test from that branch?

@bobiverse bobiverse reopened this Jan 21, 2025
@gebes
Copy link
Author

gebes commented Jan 21, 2025

saving-private-gebes

I see what you did there :D

I'll check it out now

@gebes
Copy link
Author

gebes commented Jan 21, 2025

It is working partially.
Can you add me on Discord @gebes?

@bobiverse
Copy link
Owner

It looks it's fixed "variables with spaces", but other issues let's move to new issues.

I wanted to fix at once, but there is such a mess. I'll better rewrite whole params replace logic 🤷

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants