Skip to content
This repository was archived by the owner on Aug 27, 2026. It is now read-only.
This repository was archived by the owner on Aug 27, 2026. It is now read-only.

FEATURE: Simplify template literals #33

Description

@grebaldi

As of right now, template literals are modeled after ECMAScript, e.g.:

export component Foo {
  bar: string

  return `
  Hello ${bar}
  `
}

Imho this causes two problems:

  1. The ${ sequence is a considerable complication for the parser (or better: The tokenizer)
  2. Template literals of this kind cause lots of unwanted space, that cannot be trimed away by the engine itself

I therefore suggest to change the template literal syntax to this:

export component Foo {
  bar: string

  return """
    Hello {bar}
    """
}

Rules are:

  1. Leading and trailing spaces are trimmed
  2. Indentation as per the closing delimiter is removed from every line (similar to PHP Heredoc syntax: https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc)

This will still allow for possible future tagged template literals, like:

export component Foo {
  bar: string

  return markdown"""
    # A markdown document
    
    ## Some headline: {bar}
    Lorem ipsum...
    """
}

Activity

  1. mhsdesign commented on Aug 7, 2023

    @mhsdesign
    Contributor

    I think it’s like pythons f strings, except the f ^^ - so yes I like it ;)

    also the ${ is less intuitive to write than a simple brace

  2. mhsdesign commented on Aug 7, 2023

    @mhsdesign
    Contributor

    Leading and trailing spaces are trimmed

    i like phps behaviour of nowdocs better, there the indentation of the closing defines where each line starts

  3. grebaldi commented on Aug 7, 2023

    @grebaldi
    MemberAuthor

    @mhsdesign

    Like this?:

    export component Foo {
      bar: string
    
      return """
          Hello {bar}
          """
    }
    
  4. mhsdesign commented on Aug 7, 2023

    @mhsdesign
    Contributor

    yes.

    taken from the php doc:

    The closing identifier may be indented by space or tab, in which case the indentation will be stripped from all lines in the doc string. [...]

    If the closing identifier is indented further than any lines of the body, then a ParseError will be thrown:

    // no indentation
    return """
          a
         b
        c
    """
    
    // 4 spaces of indentation
    return """
          a
         b
        c
        """
    

    output:

          a
         b
        c
    
      a
     b
    c
    
  5. grebaldi commented on Aug 8, 2023

    @grebaldi
    MemberAuthor

    @mhsdesign:

    i like phps behaviour of nowdocs better, there the indentation of the closing defines where each line starts

    Agreed. I adjusted the issue description accordingly.

  6. grebaldi commented on Aug 10, 2023

    @grebaldi
    MemberAuthor

    We'll need different delimiters, as """ cannot win against " in expressions.

    I'm currently trying:

    return ---
          Hello {bar}
          ---
    

    but, dunno....

    nvm... There needs to be an alternative to handling ambiguous tokens anyway, so """ can probably stay.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions