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

Template variables #80

Open
diego3g opened this issue Aug 2, 2021 · 3 comments
Open

Template variables #80

diego3g opened this issue Aug 2, 2021 · 3 comments
Labels
enhancement New feature or request
Milestone

Comments

@diego3g
Copy link
Owner

diego3g commented Aug 2, 2021

For now, the template only received one variable: {{ message_content }}, but it would be good to have other variables as:

{{ message_content }} - The full content of a broadcast or sequence message
{{ address }} - Your company's address, as set in your account settings
{{ unsubscribe_url }} - Unsubscribe URL (use to customize the link text)
{{ subscriber_preferences_url }} - Update Subscriber Preferences URL (use to customize the link text)
{{ subscriber.first_name }} - The first name of the subscriber
{{ subscriber.email_address }} - The email address of the subscriber

@diego3g diego3g added the enhancement New feature or request label Aug 2, 2021
@diego3g diego3g added this to the v1.0 milestone Aug 9, 2021
@brunooomelo
Copy link
Contributor

@diego3g I thought about that


class Content {
  private readonly content: string
  public params: []
  private template: string

  get value(): string {
    return this.content
  }

  public compose(paramsentry = {}) {
    
    if (!this.validateParams(paramsentry)) {}
    this.template = this.value
    for (let key in paramsentry) {
      const rgx = new RegExp(`{{ *(${key}) *}}`,'g')
      this.template = this.template.replace(rgx, paramsentry[key])
    }
    return this.template
  }

  private validateParams (params: []) {
    // TODO: validar todos os params passados
    return 
  }

  private constructor(content: string) {
    this.content = content
    this.params = this.extract(content)
  }

  static validate(content: string): boolean {
    if (!content) {
      return false
    }
    return true
  }

  private extract(content: string) {
    return content.match(/{{(.*?)}}/g).map(cnt => cnt.replace('{{', '').replace('}}', '').trim())
  }

  static create(content: string): Either<InvalidContentError, Content> {
    if (!this.validate(content)) {
      return false
    }

    return new Content(content)
  }
}

const template = Content.create('{{ message_content }} {{ safe_url }} {{url}}')

template.compose({
  message_content: 'Um conteudo bem bacana',
  safe_url: 'https://safe.com.br',
  url: 'https://url.com.br'
})

output

'Um conteudo bem bacana https://safe.com.br https://url.com.br'

What do you think?

@diego3g
Copy link
Owner Author

diego3g commented Aug 14, 2021

Yeah, I think it's a good approach, we'll just have to define what are the required variables to check when creating the template.

@brunooomelo
Copy link
Contributor

if we declare the variable, we can define it required or not.

not required example:
{{ message_not_required? }}

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

No branches or pull requests

2 participants