umbriel icon indicating copy to clipboard operation
umbriel copied to clipboard

Template variables

Open diego3g opened this issue 3 years ago • 3 comments

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 avatar Aug 02 '21 14:08 diego3g

@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: 'http://safe.com.br',
  url: 'http://url.com.br'
})

output

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

What do you think?

brunooomelo avatar Aug 13 '21 18:08 brunooomelo

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.

diego3g avatar Aug 14 '21 00:08 diego3g

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

not required example: {{ message_not_required? }}

brunooomelo avatar Aug 14 '21 02:08 brunooomelo