Skip to content

Commit

Permalink
refactor: rename @init to @let (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
eugenioenko authored Apr 12, 2024
1 parent b34b753 commit 06a20c2
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 17 deletions.
10 changes: 5 additions & 5 deletions dist/kasper.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/kasper.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions live/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@
</div>
<div class="flex-grow bg-gray-700 p-6">
<div @if="post.value && user.value">
<kvoid @init="u = user.value">
<kvoid @let="u = user.value">
<div class="text-lg">Author</div>
<div class="flex flex-col pb-4">
<div class="text-lg font-bold">{{u.name}}</div>
<div class="text-sm text-gray-400">{{u.email}}</div>
</div>
</kvoid>
<kvoid @init="p = post.value">
<kvoid @let="p = post.value">
<div class="text-2xl font-bold">{{p.title}}</div>
<div class="text-sm text-gray-400">{{p.body}}</div>
</kvoid>
Expand Down
6 changes: 3 additions & 3 deletions live/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ <h4>Hobbies ({{person.hobbies.length}}):</h4>
</div>
<!-- evaluating code on element creation -->
<div @init="student = {name: person.name, degree: 'Masters'}; console.log(student.name)">
<div @let="student = {name: person.name, degree: 'Masters'}; console.log(student.name)">
{{student.name}}
</div>
Expand All @@ -67,15 +67,15 @@ <h4>Hobbies ({{person.hobbies.length}}):</h4>
</span>
<!-- while loop -->
<span @init="index = 0">
<span @let="index = 0">
<span @while="index < 3">
{{index = index + 1}},
</span>
</span>
<!-- void elements -->
<div>
<kvoid @init="index = 0">
<kvoid @let="index = 0">
<kvoid @while="index < 3">
{{index = index + 1}}
</kvoid>
Expand Down
33 changes: 31 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,35 @@ The template language should be cohesive and clean, ideally with no compromises
- html parser
- javascript like syntax parser and interpreter
- template renderer
- re-render on state update

## Getting started

To use kasper you will need to:

- Include the `kasper.js`.
- Add a `<template>` element
- Add a class that extends from `KasperApp`
- Render the app by calling `Kasper`

```
<html>
<head>
<script src="kasper.min.js"></script>
</head>
<body>
<template>
<div>{{myAppName}}</div>
</template>
<script>
class MyApp extends KasperApp {
myAppName = "MyAppName"
}
Kasper(MyApp);
</script>
</body>
</html>
```

## Conditional expression

Expand All @@ -41,12 +70,12 @@ The template language should be cohesive and clean, ideally with no compromises
</ul>
```

## Init expression
## Let expression

Evaluated during element creation

```
<div @init="student = {name: person.name, degree: 'Masters'}; console.log(student.name)">
<div @let="student = {name: person.name, degree: 'Masters'}; console.log(student.name)">
{{student.name}}
</div>
```
Expand Down
8 changes: 4 additions & 4 deletions src/transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class Transpiler implements KNode.KNodeVisitor<void> {
this.interpreter.scope = originalScope;
}

private doInit(init: KNode.Attribute, node: KNode.Element, parent: Node) {
private doLet(init: KNode.Attribute, node: KNode.Element, parent: Node) {
const originalScope = this.interpreter.scope;
this.interpreter.scope = new Scope(originalScope);
this.execute(init.value);
Expand Down Expand Up @@ -210,9 +210,9 @@ export class Transpiler implements KNode.KNodeVisitor<void> {
continue;
}

const $init = this.findAttr(node as KNode.Element, ["@init"]);
if ($init) {
this.doInit($init, node as KNode.Element, parent);
const $let = this.findAttr(node as KNode.Element, ["@let"]);
if ($let) {
this.doLet($let, node as KNode.Element, parent);
continue;
}
}
Expand Down

0 comments on commit 06a20c2

Please sign in to comment.