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

Attach strings to variables in a template #3

Open
agarzola opened this issue Dec 23, 2014 · 0 comments
Open

Attach strings to variables in a template #3

agarzola opened this issue Dec 23, 2014 · 0 comments

Comments

@agarzola
Copy link

Hi! After using string-template on a project, I’d like to run an idea for a feature by you and gauge your interest in a pull request.

Our project is an SMS interface for a bus tracking API in our city. Sometimes we use one template in a variety of ways. Take the following string, for instance: “One #9 bus headed to East Lake will arrive at Main and Washington St in 6m. Another headed to Downtown will arrive in 25m.” Notice how the route number (#9), route type (bus — vs. shuttle), and stop name (Main and Washington St) are omitted from the second sentence. This is achieved with a single template:

destination: "{amount}{route}{type} headed to {destination} {verb}{stop} {eta}"

The template is used twice, the first time with values for route, type and stop, the second time without those values. However, we were forced to remove any space before these three variables (as you can see above) to avoid extraneous spaces in the second sentence. This also means that the values for these variables while constructing the first sentence needed to include a space in front. This could be easily addressed by using a regex to remove extra spaces, but it still leaves us with templates that, at a glance, look inconsistent.

Additionally, the value for the stop variable needs to include ' at ' in front. If that word ever needed to change in the future, our client would need to change it in the code, and not the template, which can be confusing to our client. We would much rather they edit these strings in the templates file, which is much friendlier.

I propose the following syntax:

destination: "{amount}{[ ]route}{[ ]type} headed to {destination} {verb}{[ at ]stop} {eta}"

We can use square brackets to attach strings to the front or back of a variable. If the variable is not assigned any value (undefined, empty string, etc.), then the entire thing is omitted (including anything that’s inside the square brackets). This would allow our code to be much cleaner (we wouldn’t need to keep track of which variables require spaces or other words attached to the front or back), and delegate more copywriting decisions to our templates.

Thoughts?

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

No branches or pull requests

1 participant