Skip to content

Commit

Permalink
Add SuperVariableTag
Browse files Browse the repository at this point in the history
  • Loading branch information
HaniAmmar committed Jan 21, 2024
1 parent 02b43ed commit 53c6bc0
Show file tree
Hide file tree
Showing 7 changed files with 951 additions and 97 deletions.
98 changes: 98 additions & 0 deletions Documentation/Template.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- [Variable](#variable)
- [Raw Variable](#raw-variable)
- [Math](#math)
- [Super Variable](#super-variable)
- [Inline If](#inline-if)
- [If Condition](#if-condition)
- [Loop](#loop)
Expand Down Expand Up @@ -194,6 +195,103 @@ int main() {
}
```
## Super Variable
```txt
{svar:var, sub_var_0, sub_var_1, sub_var_2 ... , sub_var_9}
```

Super variable tag is like a tiny template engine engineered for basic text replacement inside a phrase; it can accept a variable tag, a raw variable tag, and a math tag. The maximum number of sub-variables is 10, starting at 0 and ending at 9.

```txt
svar_value: when {0} met with {1}, he saw {2}, {3}, {4}, {5}, {6}, {7} and {8} but not {9}.
```

The order of values does not matter, and every value can be reused at any time.

```txt
svar_value: when {5} met with {3}, he saw {2}, {1}, {4}, {0}, {6}, {7} and {8} but not {9}.
{3} and {5} were at the market when they met.
```

### Super Variable Example 1

```cpp
#include "Template.hpp"
#include "JSON.hpp"

#include <iostream>

using Qentem::JSON;
using Qentem::StringStream;
using Qentem::Template;
using Qentem::Value;

int main() {
Value<char> value;

value["phrase"] = "Welcome {0} to {1}. You have {2} points. "
"Your points will become {3} next month. "
"Your points will be here when you login to {1} every {4}.";

value["username"] = "X";
value["site_name"] = "Y";
value["points"] = 10U;
value["points_html"] = R"(<span id="pid">10U</span>)";
value["time"] = "week";

const char *content =
"{svar:phrase, {var:username}, {var:site_name}, {raw:points_html}, {math: {var:points} * 2}, {var:time}}";

StringStream<char> stream;

Template::Render(content, value, stream);
std::cout << stream << '\n';
/*
Output: Welcome X to Y. You have <span id="pid">10U</span> points. Your points will become 20 next month. Your
points will be here when you login to Y every week.
*/

//////////////////////////////////////
value.Reset();
stream.Clear();

const char *json_data = R"(
{
"phrase": "user {0} logged in {1}.\n",
"list": [
{
"name": "X",
"last_seen": "today"
},
{
"name": "Y",
"last_seen": "yesterday"
},
{
"name": "Z",
"last_seen": "last week"
}
]
}
)";

value = JSON::Parse(json_data);

content = R"(<loop set="list" value="val">{svar:phrase, {var:val[name]}, {var:val[last_seen]}}</loop>)";

Template::Render(content, value, stream);
std::cout << '\n' << stream << '\n';

/*
Output:
user User1 logged in today.
user User2 logged in yesterday.
user User3 logged in last week.
*/
}
```

## Inline If

- Logic operations:
Expand Down
73 changes: 73 additions & 0 deletions Examples/Template/Template18.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include "Template.hpp"
#include "JSON.hpp"

#include <iostream>

using Qentem::JSON;
using Qentem::StringStream;
using Qentem::Template;
using Qentem::Value;

int main() {
Value<char> value;

value["phrase"] = "Welcome {0} to {1}. You have {2} points. "
"Your points will become {3} next month. "
"Your points will be here when you login to {1} every {4}.";

value["username"] = "X";
value["site_name"] = "Y";
value["points"] = 10U;
value["points_html"] = R"(<span id="pid">10U</span>)";
value["time"] = "week";

const char *content =
"{svar:phrase, {var:username}, {var:site_name}, {raw:points_html}, {math: {var:points} * 2}, {var:time}}";

StringStream<char> stream;

Template::Render(content, value, stream);
std::cout << stream << '\n';
/*
Output: Welcome X to Y. You have <span id="pid">10U</span> points. Your points will become 20 next month. Your
points will be here when you login to Y every week.
*/

//////////////////////////////////////
value.Reset();
stream.Clear();

const char *json_data = R"(
{
"phrase": "user {0} logged in {1}.\n",
"list": [
{
"name": "X",
"last_seen": "today"
},
{
"name": "Y",
"last_seen": "yesterday"
},
{
"name": "Z",
"last_seen": "last week"
}
]
}
)";

value = JSON::Parse(json_data);

content = R"(<loop set="list" value="val">{svar:phrase, {var:val[name]}, {var:val[last_seen]}}</loop>)";

Template::Render(content, value, stream);
std::cout << '\n' << stream << '\n';

/*
Output:
user User1 logged in today.
user User2 logged in yesterday.
user User3 logged in last week.
*/
}
Loading

0 comments on commit 53c6bc0

Please sign in to comment.