Skip to content

Commit f25d3f1

Browse files
committed
refactor(template): restructure examples with real-world use cases and organized views
- remove old basic template examples (filters, includes, loops, etc.) - introduce structured examples with consistent naming (01–12) - add real-world use cases: - shop dashboard - blog home and post page - documentation page - marketing landing page - admin dashboard - add examples/views/ directory with layout, includes, and templates - improve readability and learning progression from simple to advanced - add examples/README.md for guidance examples are now production-oriented and reflect real application patterns
1 parent 5a32537 commit f25d3f1

46 files changed

Lines changed: 2100 additions & 440 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

examples/01_render_basic.cpp

Whitespace-only changes.

examples/02_loop_features.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
*
3+
* @file 02_loop_features.cpp
4+
* @author Gaspard Kirira
5+
*
6+
* Copyright 2025, Gaspard Kirira.
7+
* All rights reserved.
8+
* https://github.com/vixcpp/vix
9+
*
10+
* Use of this source code is governed by a MIT license
11+
* that can be found in the License file.
12+
*
13+
* Vix.cpp
14+
*
15+
*/
16+
// Run:
17+
// vix run examples/templates/02_loop_features.cpp
18+
//
19+
// Then open:
20+
// http://localhost:8080
21+
//
22+
// Expected template file:
23+
// examples/templates/views/02_loop/index.html
24+
25+
#include <vix.hpp>
26+
27+
using namespace vix;
28+
29+
int main()
30+
{
31+
App app;
32+
app.templates("./examples/templates/views/02_loop");
33+
34+
app.get("/", [](Request &, Response &res)
35+
{
36+
vix::template_::Context ctx;
37+
ctx.set("title", "Template Features");
38+
39+
vix::template_::Array features;
40+
features.emplace_back("Blazing fast C++");
41+
features.emplace_back("Template engine built-in");
42+
features.emplace_back("Modern Jinja-style syntax");
43+
features.emplace_back("Simple integration with App");
44+
45+
ctx.set("features", features);
46+
47+
res.render("index.html", ctx); });
48+
49+
app.run(8080);
50+
}

examples/03_if_else.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
*
3+
* @file 03_if_else.cpp
4+
* @author Gaspard Kirira
5+
*
6+
* Copyright 2025, Gaspard Kirira.
7+
* All rights reserved.
8+
* https://github.com/vixcpp/vix
9+
*
10+
* Use of this source code is governed by a MIT license
11+
* that can be found in the License file.
12+
*
13+
* Vix.cpp
14+
*
15+
*/
16+
// Run:
17+
// vix run examples/templates/03_if_else.cpp
18+
//
19+
// Then open:
20+
// http://localhost:8080
21+
//
22+
// Expected template file:
23+
// examples/templates/views/03_if/index.html
24+
25+
#include <vix.hpp>
26+
27+
using namespace vix;
28+
29+
int main()
30+
{
31+
App app;
32+
app.templates("./examples/templates/views/03_if");
33+
34+
app.get("/", [](Request &, Response &res)
35+
{
36+
vix::template_::Context ctx;
37+
ctx.set("title", "Conditional Rendering");
38+
ctx.set("logged_in", true);
39+
ctx.set("user", "Gaspard");
40+
41+
res.render("index.html", ctx); });
42+
43+
app.run(8080);
44+
}

examples/04_layout_extends.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
*
3+
* @file 04_layout_extends.cpp
4+
* @author Gaspard Kirira
5+
*
6+
* Copyright 2025, Gaspard Kirira.
7+
* All rights reserved.
8+
* https://github.com/vixcpp/vix
9+
*
10+
* Use of this source code is governed by a MIT license
11+
* that can be found in the License file.
12+
*
13+
* Vix.cpp
14+
*
15+
*/
16+
// Run:
17+
// vix run examples/templates/04_layout_extends.cpp
18+
//
19+
// Then open:
20+
// http://localhost:8080
21+
//
22+
// Expected template files:
23+
// examples/templates/views/04_extends/base.html
24+
// examples/templates/views/04_extends/index.html
25+
26+
#include <vix.hpp>
27+
28+
using namespace vix;
29+
30+
int main()
31+
{
32+
App app;
33+
app.templates("./examples/templates/views/04_extends");
34+
35+
app.get("/", [](Request &, Response &res)
36+
{
37+
vix::template_::Context ctx;
38+
ctx.set("title", "Template Inheritance");
39+
ctx.set("user", "Gaspard");
40+
41+
res.render("index.html", ctx); });
42+
43+
app.run(8080);
44+
}

examples/05_include_partial.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
*
3+
* @file 05_include_partial.cpp
4+
* @author Gaspard Kirira
5+
*
6+
* Copyright 2025, Gaspard Kirira.
7+
* All rights reserved.
8+
* https://github.com/vixcpp/vix
9+
*
10+
* Use of this source code is governed by a MIT license
11+
* that can be found in the License file.
12+
*
13+
* Vix.cpp
14+
*
15+
*/
16+
// Run:
17+
// vix run examples/templates/05_include_partial.cpp
18+
//
19+
// Then open:
20+
// http://localhost:8080
21+
//
22+
// Expected template files:
23+
// examples/templates/views/05_include/index.html
24+
// examples/templates/views/05_include/header.html
25+
26+
#include <vix.hpp>
27+
28+
using namespace vix;
29+
30+
int main()
31+
{
32+
App app;
33+
app.templates("./examples/templates/views/05_include");
34+
35+
app.get("/", [](Request &, Response &res)
36+
{
37+
vix::template_::Context ctx;
38+
ctx.set("title", "Template Include");
39+
ctx.set("user", "Gaspard");
40+
41+
res.render("index.html", ctx); });
42+
43+
app.run(8080);
44+
}

examples/06_filters_and_escape.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
*
3+
* @file 06_filters_and_escape.cpp
4+
* @author Gaspard Kirira
5+
*
6+
* Copyright 2025, Gaspard Kirira.
7+
* All rights reserved.
8+
* https://github.com/vixcpp/vix
9+
*
10+
* Use of this source code is governed by a MIT license
11+
* that can be found in the License file.
12+
*
13+
* Vix.cpp
14+
*
15+
*/
16+
// Run:
17+
// vix run examples/templates/06_filters_and_escape.cpp
18+
//
19+
// Then open:
20+
// http://localhost:8080
21+
//
22+
// Expected template file:
23+
// examples/templates/views/06_filters/index.html
24+
25+
#include <vix.hpp>
26+
27+
using namespace vix;
28+
29+
int main()
30+
{
31+
App app;
32+
app.templates("./examples/templates/views/06_filters");
33+
34+
app.get("/", [](Request &, Response &res)
35+
{
36+
vix::template_::Context ctx;
37+
ctx.set("title", "Filters and Escaping");
38+
ctx.set("name", "gaspard");
39+
ctx.set("html", "<b>safe?</b>");
40+
41+
vix::template_::Array items;
42+
items.emplace_back("C++");
43+
items.emplace_back("HTTP");
44+
items.emplace_back("Templates");
45+
ctx.set("items", items);
46+
47+
res.render("index.html", ctx); });
48+
49+
app.run(8080);
50+
}

examples/07_shop_dashboard.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
*
3+
* @file 07_shop_dashboard.cpp
4+
* @author Gaspard Kirira
5+
*
6+
* Copyright 2025, Gaspard Kirira.
7+
* All rights reserved.
8+
* https://github.com/vixcpp/vix
9+
*
10+
* Use of this source code is governed by a MIT license
11+
* that can be found in the License file.
12+
*
13+
* Vix.cpp
14+
*
15+
*/
16+
// Run:
17+
// vix run examples/templates/07_shop_dashboard.cpp
18+
//
19+
// Then open:
20+
// http://localhost:8080
21+
//
22+
// Expected template files:
23+
// examples/templates/views/07_shop_dashboard/base.html
24+
// examples/templates/views/07_shop_dashboard/header.html
25+
// examples/templates/views/07_shop_dashboard/dashboard.html
26+
27+
#include <vix.hpp>
28+
29+
using namespace vix;
30+
31+
int main()
32+
{
33+
App app;
34+
app.templates("./examples/templates/views/07_shop_dashboard");
35+
36+
app.get("/", [](Request &, Response &res)
37+
{
38+
vix::template_::Context ctx;
39+
40+
ctx.set("page_title", "Softadastra Shop Dashboard");
41+
ctx.set("shop_name", "Softadastra Store");
42+
ctx.set("owner_name", "Gaspard");
43+
ctx.set("currency", "USD");
44+
ctx.set("is_shop_open", true);
45+
ctx.set("total_products", 128);
46+
ctx.set("total_orders", 42);
47+
ctx.set("monthly_revenue", 2450);
48+
49+
vix::template_::Array orders;
50+
51+
{
52+
vix::template_::Object order;
53+
order["id"] = "ORD-1001";
54+
order["customer"] = "Alice";
55+
order["status"] = "Paid";
56+
order["amount"] = 120;
57+
orders.emplace_back(order);
58+
}
59+
60+
{
61+
vix::template_::Object order;
62+
order["id"] = "ORD-1002";
63+
order["customer"] = "Bob";
64+
order["status"] = "Pending";
65+
order["amount"] = 75;
66+
orders.emplace_back(order);
67+
}
68+
69+
{
70+
vix::template_::Object order;
71+
order["id"] = "ORD-1003";
72+
order["customer"] = "Charlie";
73+
order["status"] = "Shipped";
74+
order["amount"] = 210;
75+
orders.emplace_back(order);
76+
}
77+
78+
ctx.set("recent_orders", orders);
79+
80+
vix::template_::Array alerts;
81+
alerts.emplace_back("2 products are low in stock");
82+
alerts.emplace_back("1 order requires manual review");
83+
ctx.set("alerts", alerts);
84+
85+
res.render("dashboard.html", ctx); });
86+
87+
app.run(8080);
88+
}

0 commit comments

Comments
 (0)