Giter Site home page Giter Site logo

Template variables about umbriel HOT 3 OPEN

diego3g avatar diego3g commented on July 28, 2024
Template variables

from umbriel.

Comments (3)

brunooomelo avatar brunooomelo commented on July 28, 2024

@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?

from umbriel.

diego3g avatar diego3g commented on July 28, 2024

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.

from umbriel.

brunooomelo avatar brunooomelo commented on July 28, 2024

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

not required example:
{{ message_not_required? }}

from umbriel.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.