ABAP,Java,JavaScript里的字符串模板String Template-阿里云开发者社区

开发者社区> c位出道> 正文

ABAP,Java,JavaScript里的字符串模板String Template

简介: ABAP,Java,JavaScript里的字符串模板String Template
+关注继续查看

As an ABAP you probably be very familiar with String Template.


String Template in ABAP

A string template creates a string from literal text, embedded expressions, and control characters in a string expression. The most powerful feature I like is we can insert ABAP variable inside the template by wrapper variable with “{ }”, see one example below:


image.png

In the runtime, ABAP will replace the String Template content with actual value stored in that variable. This functionality is especially useful when you need to populate string dynamically based on some ABAP variable, for example you need to create some ABAP class or function module whose signature is only known in the runtime specified by consumer.


The below highlighted string is the calculated result of String Template | DATA: _{ -arg_name } LIKE { -arg_name }.|.


image.png

String Template is heavily used on my series of my blogs where I attempt to simulate some nice feature in other languages using ABAP:

Lazy Loading, Singleton and Bridge design pattern in JavaScript and in ABAP

Functional programming – Simulate Curry in ABAP

Simulate Mockito in ABAP


String Template in ES6

Say you have an array which contains name of several programming languages. And the requirement is to print lines of “Hello” plus language name stored in the array.

Pretty easy to implement, isn’t it?


image.png

image.png

Now try String Template in ES6. If you know how to use String Template in ABAP, it’s a piece of cake in ES6 as well:

image.png

See comparison below:

image.png

It seems that ABAP String Template wins in this round of comparison since lots of format_options are supported there.

image.png

String Template in Angular

Technically speaking it is not precise to name the below feature to be explained as “String Template” in Angular context, nevertheless I decide to continue using this name since it works as the same logic from end user perspective as String Template in ABAP and ES6 and also have the similar grammar.

Try this url in your browser:


image.png\Why? Check the source code of this hello world page, here I use a similar “String Template” as the example described in previous ES6 part: {{ name }} will be replaced with the value of Angular model “name” ( declared via directive ng-model ) in the runtime. But keep in mind, this replacement is done by Angular framework. This is the biggest difference compared with ABAP String Template and ES6 String Template, where the replacement is done by ABAP runtime and JavaScript execution engine accordingly.


image.png

The parse of “Hello {{name}}” itself is done by Angular as well:

image.png

Every time you type character in the input field, the content of String Template will react to this change and refresh itself. If you open F12 in Chrome development, you can observe the following trace when “Jerry” is typed.


image.png

If you would like to know in detail how Angular Data binding is implemented, see my blog Compare Data Binding mechanism: SAPUI5 and Angular.


String Template in React

image.png

If you would like to know more about debugging in React, see my blog How to get and debug converted source code in React.


Further reading

I have written a series of blogs which compare the language feature among ABAP, JavaScript and Java. You can find a list of them below:


Lazy Loading, Singleton and Bridge design pattern in JavaScript and in ABAP

Functional programming – Simulate Curry in ABAP

Functional Programming – Try Reduce in JavaScript and in ABAP

Simulate Mockito in ABAP

A simulation of Java Spring dependency injection annotation @Inject in ABAP

Singleton bypass – ABAP and Java

Weak reference in ABAP and Java

Fibonacci Sequence in ES5, ES6 and ABAP

Java byte code and ABAP Load

How to write a correct program rejected by compiler: Exception handling in Java and in ABAP

An small example to learn Garbage collection in Java and in ABAP

String Template in ABAP, ES6, Angular and React

Try to access static private attribute via ABAP RTTI and Java Reflection


版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
java多线程:并发包中ReentrantReadWriteLock读写锁的锁降级模板
写锁降级为读锁,但读锁不可升级或降级为写锁。 锁降级是为了让当前线程感知到数据的变化。 1 //读写锁 2 private ReentrantReadWriteLock lock=new ReentrantReadWriteLock(); 3 //读锁 4 private ReadLock readLock=lock.
714 0
创建卡券模板接口(alipay.pass.template.add)JAVA版本小样
官方接口文档:https://docs.open.alipay.com/199/105250/  基本操作流程  1.创建应用等流程不概述了,调用该接口主要第一步是模板设计:推荐使用卡券平(https://alipassprod.
463 0
JavaScript中字符串与Unicode编码的互相转换
JavaScript中字符串与Unicode编码的互相转换 这段代码演示了JavaScript中字符串与Unicode编码的转换: // 为了控制台的演示方便, 变量没有添加 var 定义 // 实际编程中请避免 // 字符串 str = "中文"; // 获取字符 char0 = str.
622 0
javascript中对字符串的操作总结
原文:javascript中对字符串的操作总结 没听过一句话吗?程序员的世界,不处理字符串就是处理数组。这是群里的一位前辈和我说的,显然这和我之前理解的DOM是javascript的核心的不同的,看了几个面试题的内容,感觉前辈说的话非常的有道理,因此总结下,javascript中对字符串的操作。
807 0
FreeMarker之根据模板生成Java代码
FreeMarker根据模板生成Java代码,光这句话,大家想必也知道它的应用了,比如流行的DRY原则,该原则的意思,可简单概述为"不要写重复的代码"。 比如Java中三层架构,数据访问层,业务逻辑层,表现层,光这三层就出现重复性的增删改查及其相关的界面代码。
2067 0
模板(templates)
通常情况下,我们不会像hello world一样直接在视图函数中编写返回内容,而是专门返回一个渲染过的模板。Flask使用Jinja2完成这一任务。 1.
514 0
+关注
2316
文章
0
问答
来源圈子
更多
+ 订阅
文章排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载