Skip to content

Commit 27bba9b

Browse files
authored
Merge pull request #41 from solidify-project/feature/model
Feature/model
2 parents a6a386a + 74a8a56 commit 27bba9b

7 files changed

Lines changed: 193 additions & 32 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<img src="{{ Model.banner.url }}"/>
2+
3+
{{ Page.Content }}
4+
5+
<table>
6+
<tr>
7+
<th>Title</th>
8+
<th>Price</th>
9+
</tr>
10+
{{# Page.Model.goods }}
11+
<tr>
12+
<td>{{ title }}</td>
13+
<td>{{ price }}</td>
14+
</tr>
15+
{{/ Page.Model.goods }}
16+
</table>

_help/src/Pages/folders-structure/pages.md

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ title: Pages folder structure
99

1010
### Pages
1111

12-
Inside `Pages` folder there will be all page files. Each page file is a markdown markup file with some mandatory metadata at the start of the file.
12+
Inside `Pages` folder will be all page files. Each page file is a markdown markup file with some mandatory metadata at the start of the file.
1313

14-
Page file look like this:
14+
Page file looks like this:
1515

1616
```markdown
1717
url: index.html
@@ -31,7 +31,7 @@ custom.logo.url: http://my.com/logo.png
3131

3232
#### url
3333

34-
The `url` attribute is mandatory. It shows under which path output html file will be saved. If you want to save in some specific folder, you can do it by providing the following value for `url` attribute:
34+
The `url` attribute is mandatory. It shows under which path output html file will be saved. If you want to save it in some specific folder, you can do it by providing the following value for `url` attribute:
3535

3636
```markdown
3737
url: blog/posts/2018/Jan/my-awesome-post.html
@@ -41,7 +41,7 @@ url: blog/posts/2018/Jan/my-awesome-post.html
4141

4242
The `template` attribute is mandatory. It shows which template should be used to render current page. For now only flat structure is supported, so you can reference templates only from root of `Layout` folder.
4343

44-
You can use other synonimous for `template` attribute which are `TemplateId`, `Layout`, `LayoutId`. All those synonymous are case insensitive.
44+
You can use other synonims for `template` attribute which are `TemplateId`, `Layout`, `LayoutId`. All those synonyms are case-insensitive.
4545

4646
> More details about templates / layouts can be found in the dedicated [layout](/folders-structure/layout.html) section.
4747
@@ -54,33 +54,16 @@ The `title` attribute is not mandatory, but we recommend to use it for setting h
5454

5555
If predefined attributes are not enough and you want to add some extra attributes to your page, you can acheive that by using custom attributes section. The name of custom attribute should start with `custom` followed by `.`. After that you should add at least one character that will represent the name of your attribute.
5656

57+
> More advanced details about how to work with custom attributes can be found in [page custom attributes](/folders-structure/pages/attributes.html) section.
5758
58-
#### Page object
59+
### Data model
5960

60-
All attributes can be accessible through global `Page` object. That object is accessible from each and every template and it always has a context of current page.
61+
In case you want to use the same template, but use different data for it, you can use page data model. The scenario here is to have different data (in terms of values) with the same structure on different pages. The name of page data model should start with `model` followed by `.`. After that you should add at least one character that will represent the name of your data model. The value of `model` should always point to valid `data` object.
6162

62-
You can access predefined page attributes via `Page.Title`, `Page.Url`, `Page.TemplateId` and `Page.Content` for page html content. All properties of global `Page` object are case sensitive.
63+
> More advanced details about how to work with data can be found in [page data model](/folders-structure/pages/model.html) section.
6364
64-
You can access custom page attributes via properties of `Page.Custom` object. It will have all the properties defibed in page metadata secrion.
65+
### Custom attributes vs data model
6566

66-
#### Example
67-
68-
If your page file looks like this:
69-
70-
```markdown
71-
url: index.html
72-
template: default.hjs
73-
title: Home
74-
75-
custom.header: header
76-
custom.description: this is my home page
77-
custom.logo.url: http://my.com/logo.png
78-
79-
---
80-
81-
# Welcome to my website!
82-
```
83-
84-
You can access logo url using the following statement `Page.Custom.logo.url`.
85-
86-
There is one important point to highlight here. `Page.Custom` is predifened and case sensitive, but `logo.url` was generated dynamicaly from page metadata and it's also case sensitive.
67+
The main difference between custom attributes and data model is how they interprete the values you provide.
68+
- **Custom attributes** always treat the values as strings.
69+
- **Data model** always treats the values as references to global `Data` model. It will copy all values from referenced `Data` object to current `Model` object.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
url: folders-structure/pages/attributes.html
2+
template: default.hjs
3+
4+
title: Pages custom attributes
5+
6+
---
7+
8+
[back to pages folder structure](/folders-structure/pages.html)
9+
10+
### Custom attributes
11+
12+
If predefined attributes are not enough and you want to add some extra attributes to your page, you can acheive that by using custom attributes section. The name of custom attribute should start with `custom` followed by `.`. After that you should add at least one character that will represent the name of your attribute.
13+
14+
15+
#### Page object
16+
17+
All attributes can be accessible through global `Page` object. That object is accessible from each and every template and it always has a context of current page.
18+
19+
You can access predefined page attributes via `Page.Title`, `Page.Url`, `Page.TemplateId` and `Page.Content` for page html content. All properties of global `Page` object are case-sensitive.
20+
21+
You can access custom page attributes via properties of `Page.Custom` object. It will have all the properties defined in page metadata section.
22+
23+
#### Example
24+
25+
If your page file looks like this:
26+
27+
```markdown
28+
url: index.html
29+
template: default.hjs
30+
title: Home
31+
32+
custom.header: header
33+
custom.description: this is my home page
34+
custom.logo.url: http://my.com/logo.png
35+
36+
---
37+
38+
# Welcome to my website!
39+
```
40+
41+
You can access logo url using the following statement `Page.Custom.logo.url`.
42+
43+
There is one important point to highlight here. `Page.Custom` is predifened and case-sensitive, but `logo.url` was generated dynamicaly from page metadata and it's also case-sensitive.
44+
45+
### Custom attributes vs data model
46+
47+
The main difference between custom attributes and data model is how they interprete the values you provide.
48+
- **Custom attributes** always treat all values as strings.
49+
- **Data model** always treats all values as references to global `Data` model. It will copy all values from referenced `Data` object to current `Model` object.
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
url: folders-structure/pages/model.html
2+
template: default.hjs
3+
4+
title: Pages data model
5+
6+
---
7+
8+
[back to pages folder structure](/folders-structure/pages.html)
9+
10+
### Data model
11+
12+
In case you want to use the same template, but use different data for it, you can use page data model. The scenario here is to have different data (in terms of values) with the same structure on different pages. The name of page data model should start with `model` followed by `.`. After that you should add at least one character that will represent the name of your data model. The value of `model` should always point to valid `data` object.
13+
14+
15+
#### Page object
16+
17+
All data models can be accessible through global `Page` object. That object is accessible from each and every template and it always has a context of current page.
18+
19+
You can access page data models via properties of `Page.Model` object. It will have all the properties defined in page metadata section.
20+
21+
#### Example
22+
23+
If your template file looks like this:
24+
25+
```handlebars
26+
{{ Data.FoldersStructure.Pages.Model.Template01 }}
27+
```
28+
29+
And your page file looks like this:
30+
31+
```markdown
32+
url: goods1.html
33+
template: goods.hjs
34+
title: Home
35+
36+
model.goods: Data.category1.goods
37+
model.banner.url: Data.banners.banner1.url
38+
39+
---
40+
41+
# Category 1
42+
```
43+
44+
And your data structure looks like this:
45+
46+
```none
47+
Data
48+
category1.yml
49+
category2.yml
50+
banners.yml
51+
```
52+
53+
`category1.yml`
54+
```yaml
55+
goods:
56+
-
57+
title: apple
58+
price: 10
59+
-
60+
title: pear
61+
price: 12
62+
```
63+
64+
`category2.yml`
65+
```yaml
66+
goods:
67+
-
68+
title: potato
69+
price: 6
70+
-
71+
title: tomato
72+
price: 15
73+
```
74+
75+
`banners.yml`
76+
```yaml
77+
banner1:
78+
url: "http://mywebsite.com/banner1.png"
79+
banner2:
80+
url: "http://other.com/something.png"
81+
```
82+
83+
Finally, the html rendered by Solidify Engine will look like this:
84+
85+
```html
86+
<img src="http://mywebsite.com/banner1.png"/>
87+
88+
<h1>Category 1</h1>
89+
90+
<table>
91+
<tr>
92+
<th>Title</th>
93+
<th>Price</th>
94+
</tr>
95+
<tr>
96+
<td>apple</td>
97+
<td>10</td>
98+
</tr>
99+
<tr>
100+
<td>pear</td>
101+
<td>12</td>
102+
</tr>
103+
</table>
104+
```
105+
106+
107+
There is one important point to highlight here. `Page.Model` is predifened and case-sensitive, but `goods` was generated dynamicaly from page metadata and it's also case-sensitive.
108+
109+
### Custom attributes vs data model
110+
111+
The main difference between custom attributes and data model is how they interprete the values you provide.
112+
- **Custom attributes** always treats all values as strings.
113+
- **Data model** always treats all values as references to global `Data` model. It will copy all values from referenced `Data` object to current `Model` object.

src/SolidifyProject.Engine.Infrastructure/Models/PageModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public sealed class PageModel : TextContentModel
2727
public string Url { get; set; }
2828

2929
public dynamic Custom { get; set; }
30+
public dynamic Model { get; set; }
3031

3132
/// <summary>
3233
/// </summary>
@@ -42,7 +43,6 @@ public sealed class PageModel : TextContentModel
4243
/// </summary>
4344
public string Content { get; set; }
4445

45-
public dynamic Model { get; set; }
4646

4747
public PageModel()
4848
{

src/SolidifyProject.Engine.Services/TemplateService/MustacheTemplateService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public Task<string> RenderTemplateAsync(string template, PageModel pageModel, Ex
3333
}
3434

3535

36-
var model = new { Page = pageModel, Data = dataModel, Model = pageModel.Model };
36+
var model = new { Page = pageModel, Data = dataModel };
3737

3838
var result = Render.StringToString(template, model, getTemplate, new RenderContextBehaviour
3939
{
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<div>
2-
<i>{{ Model.disclaimer }}</i>
2+
<i>{{ Page.Model.disclaimer }}</i>
33
</div>

0 commit comments

Comments
 (0)