-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlayout.dart
137 lines (120 loc) · 3.29 KB
/
layout.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import 'package:liquify/liquify.dart';
void main() async {
// Create our file system with templates
final fs = MapRoot({
// Base layout with common structure
'layouts/base.liquid': '''
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}Default Title{% endblock %}</title>
{% block meta %}{% endblock %}
<link rel="stylesheet" href="/styles.css">
{% block styles %}{% endblock %}
</head>
<body>
<header>
{% block header %}
<nav>
<a href="/">Home</a>
<a href="/about">About</a>
<a href="/contact">Contact</a>
</nav>
{% endblock %}
</header>
<main>
{% block content %}
Default content
{% endblock %}
</main>
<footer>
{% block footer %}
<p>© {{ year }} My Website</p>
{% endblock %}
</footer>
<script src="/main.js"></script>
{% block scripts %}{% endblock %}
</body>
</html>''',
// Blog post layout that extends base
'layouts/post.liquid': '''
{% layout "layouts/base.liquid", title: post_title, year: year %}
{% block meta %}
<meta name="author" content="{{ post.author }}">
<meta name="description" content="{{ post.excerpt }}">
{% endblock %}
{% block styles %}
<link rel="stylesheet" href="/blog.css">
{% endblock %}
{% block content %}
<article>
<h1>{{ post_title }}</h1>
<div class="metadata">
By {{ post.author }} on {{ post.date | date: "%B %d, %Y" }}
</div>
<div class="content">
{{ post.content }}
</div>
{% if post.tags.size > 0 %}
<div class="tags">
Tags:
{% for tag in post.tags %}
<span class="tag">{{ tag }}</span>
{% endfor %}
</div>
{% endif %}
</article>
{% endblock %}
{% block scripts %}
<script src="/blog.js"></script>
{% endblock %}''',
// Actual blog post using post layout
'posts/hello-world.liquid': '''
{% assign post_title = "Hello, World!" %}
{% layout "layouts/post.liquid", post_title: post_title, year: year %}
{%- block header -%}
<h1>HEADER CONTENT</h1>
{%- endblock -%}
{% block footer %}
{{ block.parent }}
<div class="post-footer">
<a href="/posts">Back to Posts</a>
</div>
{% endblock %}'''
});
// Sample post data
final context = {
'year': 2024,
'post': {
'title': 'Hello, World!',
'author': 'John Doe',
'date': '2024-02-09',
'excerpt': 'An introduction to our blog',
'content': '''
Welcome to our new blog! This is our first post exploring the features
of the Liquid template engine. We'll be covering:
- Template inheritance
- Layout blocks
- Custom filters
- And much more!
Stay tuned for more content coming soon!''',
'tags': ['welcome', 'introduction', 'liquid'],
}
};
print('\nRendering blog post with layout inheritance:');
print('----------------------------------------\n');
// Render the blog post
final template =
Template.fromFile('posts/hello-world.liquid', fs, data: context);
print(await template.renderAsync());
// Demonstrate dynamic layout names
print('\nDemo of dynamic layout names:');
print('----------------------------------------\n');
final dynamicTemplate = Template.parse(
'{% layout "layouts/{{ layout_type }}.liquid", title: "Dynamic Title" %}',
data: {
'layout_type': 'post',
},
root: fs);
print(await dynamicTemplate.renderAsync());
}