Skip to content

docs: add builtin template function #330

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

Merged
merged 2 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/reference/model/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@ For relatively complex general logic, it is provided through the system modules.
<DocsCard header="file" href="file">
<p>Provides filesystem functions.</p>
</DocsCard>
<DocsCard header="template" href="template">
<p>Provides template functions.</p>
</DocsCard>
</DocsCards>
48 changes: 48 additions & 0 deletions docs/reference/model/template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
title: "template"
linkTitle: "template"
type: "docs"
description: template functions
weight: 100
---

## execute

`execute(template: str, data: {str:any} = {}) -> str`

Applies a parsed template to the specified data object and returns the string output. See https://handlebarsjs.com/ for more documents and examples.

```python3
import template

content = template.execute("""\
<div class="entry">
{{#if author}}
<h1>{{firstName}} {{lastName}}</h1>
{{/if}}
</div>
""", {
author: True,
firstName: "Yehuda",
lastName: "Katz",
})
```

## html_escape

`html_escape(data: str) -> str`

Replaces the characters `&"<>` with the equivalent html / xml entities.

```python3

import template

content = template.html_escape("""\
<div class="entry">
{{#if author}}
<h1>{{firstName}} {{lastName}}</h1>
{{/if}}
</div>
""")
```
9 changes: 9 additions & 0 deletions docs/user_docs/support/faq-install.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,12 @@ Please ensure that the following dependencies are in your PATH:
## Encountering exit status 0xc0000135 error on Windows platform

Please ensure that `.NET Framework` and `MSVC` are installed on your Windows. If not installed, you can install them and try again.

## Errors on Starting/Running KCL inside a Container for "Permission Denied" or "File Not Found"

The issue is due to KCL requiring write permissions for its default global configuration and global package cache at compile time. One solution is to set the global configuration and package directory to the `/tmp` folder. For detailed Dockerfile configuration, please see [here](https://github.com/kcl-lang/cli/blob/main/Dockerfile).

```dockerfile
ENV KCL_PKG_PATH=/tmp
ENV KCL_CACHE_PATH=/tmp
```
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ KCL 通过内置模块、系统库模块和插件模块提供工程化的扩展
<DocsCard header="yaml" href="yaml">
<p>提供了与 YAML 相关的编码/解码函数。</p>
</DocsCard>
<DocsCard header="file" href="yaml">
<DocsCard header="file" href="file">
<p>提供了与文件系统相关的函数。</p>
</DocsCard>
<DocsCard header="template" href="template">
<p>提供了与模版相关的函数。</p>
</DocsCard>
</DocsCards>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
title: "template"
linkTitle: "template"
type: "docs"
description: 模版操作
weight: 100
---

## execute

`execute(template: str, data: {str:any} = {}) -> str`

将解析过的模板应用于指定的数据对象,并返回字符串输出。查看 https://handlebarsjs.com/ 获取更多文档和示例。

```python3
import template

content = template.execute("""\
<div class="entry">
{{#if author}}
<h1>{{firstName}} {{lastName}}</h1>
{{/if}}
</div>
""", {
author: True,
firstName: "Yehuda",
lastName: "Katz",
})
```

## html_escape

`html_escape(data: str) -> str`

将字符 `&"<>` 替换为等效的 html / xml实体。

```python3

import template

content = template.html_escape("""\
<div class="entry">
{{#if author}}
<h1>{{firstName}} {{lastName}}</h1>
{{/if}}
</div>
""")
```
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,12 @@ xattr -rd com.apple.quarantine /path/to/kcl
## 在 Windows 平台上遇到 exit status 0xc0000135 错误

请确保您的 Windows 上安装了 `.NET Framework` 和 `MSVC`,如没有安装,可以安装并重试。

## 在容器中启动/运行 KCL 报没有权限或者找不到文件的错误

这是因为 KCL 在编译时的默认全局配置和全局包缓存需要写入权限,一种解决方式是将全局配置和包目录设置到 `/tmp` 文件夹,详见[这里](https://github.com/kcl-lang/cli/blob/main/Dockerfile) 的 Dockerfile 配置

```dockerfile
ENV KCL_PKG_PATH=/tmp
ENV KCL_CACHE_PATH=/tmp
```
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ weight: 100

## read

`read(filepath: string) -> str`
`read(filepath: str) -> str`

读取文件 `filepath` 中的内容,并返回一个字符串实例。

Expand All @@ -29,3 +29,15 @@ weight: 100
`workdir() -> str`

返回当前工作目录的路径。

## exists

`exists(filepath: str) -> bool`

判断文件路径是否存在。如果路径指向一个存在的实体,则返回 True。此函数会遍历符号链接以查询目标文件的信息。

## abs

`abs(filepath: str) -> str`

返回路径的规范化绝对形式,其中所有中间路径均已规范化并解析为符号链接。
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ KCL 通过内置模块、系统库模块和插件模块提供工程化的扩展
<DocsCard header="yaml" href="yaml">
<p>提供了与 YAML 相关的编码/解码函数。</p>
</DocsCard>
<DocsCard header="file" href="yaml">
<DocsCard header="file" href="file">
<p>提供了与文件系统相关的函数。</p>
</DocsCard>
</DocsCards>
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,12 @@ xattr -rd com.apple.quarantine /path/to/kcl
## 在 Windows 平台上遇到 exit status 0xc0000135 错误

请确保您的 Windows 上安装了 `.NET Framework` 和 `MSVC`,如没有安装,可以安装并重试。

## 在容器中启动/运行 KCL 报没有权限或者找不到文件的错误

这是因为 KCL 在编译时的默认全局配置和全局包缓存需要写入权限,一种解决方式是将全局配置和包目录设置到 `/tmp` 文件夹,详见[这里](https://github.com/kcl-lang/cli/blob/main/Dockerfile) 的 Dockerfile 配置

```dockerfile
ENV KCL_PKG_PATH=/tmp
ENV KCL_CACHE_PATH=/tmp
```
14 changes: 13 additions & 1 deletion versioned_docs/version-0.8/reference/model/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ weight: 100

## read

`read(filepath: string) -> str`
`read(filepath: str) -> str`

Read the contents of the file `filepath` and return a string instance.

Expand All @@ -29,3 +29,15 @@ Return the root path of the current KCL module (kcl.mod file path or single \*.k
`workdir() -> str`

Return the path of the current working directory.

## exists

`exists(filepath: str) -> bool`

Whether this file path exists. Returns true if the path points at an existing entity. This function will traverse symbolic links to query information about the destination file.

## abs

`abs(filepath: str) -> str`

Returns the canonical, absolute form of the path with all intermediate components normalized and symbolic links resolved.
9 changes: 9 additions & 0 deletions versioned_docs/version-0.8/user_docs/support/faq-install.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,12 @@ Please ensure that the following dependencies are in your PATH:
## Encountering exit status 0xc0000135 error on Windows platform

Please ensure that `.NET Framework` and `MSVC` are installed on your Windows. If not installed, you can install them and try again.

## Errors on Starting/Running KCL inside a Container for "Permission Denied" or "File Not Found"

The issue is due to KCL requiring write permissions for its default global configuration and global package cache at compile time. One solution is to set the global configuration and package directory to the `/tmp` folder. For detailed Dockerfile configuration, please see [here](https://github.com/kcl-lang/cli/blob/main/Dockerfile).

```dockerfile
ENV KCL_PKG_PATH=/tmp
ENV KCL_CACHE_PATH=/tmp
```
34 changes: 34 additions & 0 deletions versioned_docs/version-0.8/user_docs/support/faq-kcl.md
Original file line number Diff line number Diff line change
Expand Up @@ -2512,3 +2512,37 @@ worldString: world
s: Hello world
raw_s: Hello ${worldString}
```

## 59. How does KCL infer return types in lambdas?

For lambda(s),KCL automatically infers the return value type in the function body, although we can explicitly specify it. An example KCL code over here is:

```KCL
f1 = lambda t: Type1 {
Type2 {}
} # The type of f1 is (Type1) -> Type2

f2 = lambda t: Type1 -> Type2 {
Type2 {}
} # The type of f2 is (Type1) -> Type2
```

## 60. How to convert a list of lists to a single list in KCL?

To convert a list of lists into a single list, we use the `sum()` function. For example if we have a number of lists such as `[[1,2],[3,4]], [5,6]`, we use the KCL code given below to convert these three lists into a single list:

```KCL
final_list = sum([[1,2],[3,4]],[5,6])
```

The above KCL code gives the output:

```yaml
final_list:
- 5
- 6
- 1
- 2
- 3
- 4
```