-
Notifications
You must be signed in to change notification settings - Fork 2
/
umbraco.cshtml
451 lines (356 loc) · 11.3 KB
/
umbraco.cshtml
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
@* Umbraco uses Razor for its pages; page.cshtml *@
@*
Installation:
1. Create project
2. nuget console or nuget store --> Install-Package UmbracoCms
2. or download umbraco on site and launch solution on Visual studio
3. Run to create umbraco solution and log in (whatever you like, but remember it)
4. Use SQL CE if no DB provided
5. Use no template
6. activate https --> SSL field to true in web.config + copy https localhost and paste in solution settings
Content:
Defined by a content type
Publish/Unpublish
Media:
Used for images
Settings:
Document types: create pages (metadata, content)
Templates: renders the content of the pages
Partials views: parts of pages reused in a lot of places
Languages: manages a multi-language site
Dictionary: store word in different languages
Media types: img settings
Developers:
Packages: find leblender, robots.txt, etc.
Data types: textbox, image uploader, etc. to use in document types
Macros: create parts to integrate later (better to use partials)
Users:
People accessing the backend via site.com/umbraco
They are part of user groups that define their permissions
Members:
People accessing the frontend via site.com/login
They are part of member group that define their page access
Create pages:
1. Create document type (check permission to set as root or set children)
2. Create content (click on the dots)
3. to display content, use a template (and link it via doc type)
*@
@* great intro to razot --> https://weblogs.asp.net/scottgu/introducing-razor *@
@{
Umbraco.Web.Mvc.UmbracoTemplatePage
Umbraco.Web.Mvc.UmbracoTemplatePage<Model>
Umbraco.Web.Mvc.UmbracoViewPage
Umbraco.Web.Mvc.UmbracoViewPage<Model>
UmbracoApplication
UmbracoContext
UmbracoHelper
}
@* Comments *@
@{
// comments in statement block
/*
multi line comments in statement block
*/
}
@* Variables *@
@{
var myNumber = 19;
<p>My number is @myNumber</p>
}
@* Conditional *@
@{
var howMuch = 19;
if(howMuch > 19) {
// do something
}else if(howMuch < 19) {
// do something else
}else{
// do something else else
}
}
@* Loop *@
@{
foreach(var item in itemList)
{
<p>item</p>
}
}
@DateTime.Now.Year;
@DateTime.Now.Month;
@DateTime.Now.Day;
@* Change 404 page with XPath; go into config/umbracoSettings.config; page is documentType and Not found is page name *@
@* <error404>//page[@nodeName='Not found']</error404> *@
@* create a sitemap *@
@* https://our.umbraco.com/documentation/Tutorials/Creating-an-XML-Site-Map/ *@
@* will implement the partial inheriting from current template *@
@RenderBody();
@* displays text without html tags keeping formating; use it with rich text editors; use it if you need nested javascript inside C# *@
@Html.Raw("<p>Simple sentence.</p>");
@* displays text without html tags without formating *@
@{"<p>Simple sentence.</p>".StripHtml()};
@Html.Partial("nameOfTheFile", ...);
@* pass a variable into the partial *@
@Html.Partial("Component/ImageSingle", new ViewDataDictionary { { "comp", component.Id } });
@* use helpers in sub partials *@
@foreach(var child in Model.Content.Children())
{
@Html.Partial("MyPartialName", child)
}
@Html.Action("nameOfAction", "nameOfController");
@using (Html.BeginUmbracoForm<App.LoginForm>("Login")) {}
@Html.LabelFor(m->m.Login);
@Html.EditorFor(m->m.Login);
@Html.RequiresCss("/path");
@Html.RenderCssHere();
@anyString.Replace(" ", "-");
@Umbraco.GetDictionnaryValue("ValueName");
@* truncates to 10 characters, and add ellipsis because true *@
@Umbraco.Truncate("I wish I was a tweet, at least then I get 140 chars", 10, true);
@* get site URL *@
@Request.Url.GetLeftPart(UriPartial.Authority);
@* get a page with its ID; specify its actual ID number *@
@Umbraco.Content(1000);
@* get a page ID from its name *@
@GetUmbracoPageId("pageName");
@* make something dynamic *@
@{
var linkChildren = ((IEnumerable<dynamic>)Umbraco.Content(linkId).Children).Where(x => x.DocumentTypeAlias == "page" || x.DocumentTypeAlias == "widepage");
}
@* find Homepage *@
@{
Model.Content.AncestorOrSelf(1);
@* OR *@
Model.Content.Site();
}
@* subpage listing from current page of doctype 'page' or 'article' *@
@{
foreach (var subPage in Model.Content.Children.Where(x => x.DocumentTypeAlias == "page" || x.DocumentTypeAlias == "article"))
{
<a href="@subPage.Url">@subPage.Name</a>
}
}
@* subpage listing from current page of doctype 'page' or 'article' and ordering by descending PostDate; use simply OrderBy for opposite order *@
@{
foreach (var subPage in Model.Content.Children.Where(x => x.DocumentTypeAlias == "page" || x.DocumentTypeAlias == "article").OrderByDescending(y => y.GetPropertyValue<DateTime>("PostDate")))
{
<a href="@subPage.Url">@subPage.Name</a>
}
}
@* subpage listing from root *@
@{
<ul>
@* needs true/false umbracoNaviHide to filter visible page; change documenttypealias to your own naming *@
@foreach (var page in Model.Content.AncestorOrSelf(1).Children.Where(x => x.IsVisible()).Where(x => x.DocumentTypeAlias == "page"))
{
<li><a href="@page.Url">@page.Name</a></li>
}
</ul>
}
@* get homepage descendants ID with a certain document type *@
@{
@foreach (var page in Model.Content.AncestorOrSelf("homepage").Descendants().Where(x => x.DocumentTypeAlias == "page"))
{
<p>page.Id</p>
}
}
@* get single link from related link; AVOID *@
@using Umbraco.Web.Models
@{
var links = Model.Content.GetPropertyValue<RelatedLinks>("links");
if (links != null)
{
var link = links.FirstOrDefault();
<p><a role="button" target="@(link.NewWindow ? "_blank" : null)" href="@link.Link">@link.Caption</a></p>
}
}
@* get list of links from related links; AVOID *@
@using Umbraco.Web.Models
@{
var navigation = Umbraco.TypedContentSingleAtXPath("//navigationSettings");
var typedRelatedLinksConverted = navigation.GetPropertyValue<RelatedLinks>("footerLinks");
if (typedRelatedLinksConverted.Any())
{
<ul>
@foreach (var item in typedRelatedLinksConverted)
{
<li><a href="@item.Link" target="@(item.NewWindow ? "_blank" : null)">@item.Caption</a></li>
}
</ul>
}
}
@* get single link from multi url picker *@
@using Umbraco.Web.Models
@{
var link = Model.Content.GetPropertyValue<Link>("link");
if (link != null)
{
<li><a href="@link.Url" target="@link.Target">@link.Name</a></li>
}
@* get ID with @link.Udi *@
}
@* get list of links from multi url picker *@
@using Umbraco.Web.Models
@{
var links = Model.Content.GetPropertyValue<IEnumerable<Link>>("links");
if (links.Any())
{
<ul>
@foreach (var link in links)
{
<li><a href="@link.Url" target="@link.Target">@link.Name</a></li>
}
</ul>
}
}
@* get current page name *@
@Model.Content.Name;
@* get current page url *@
@Model.Content.Url;
@* get current page name from partial *@
@Model.Content.GetPropertyValue("alias");
@* check if alias has value; usefull to check if RTE have content or not *@
@Model.Content.HasValue("alias");
@* quickly take one or another value *@
@{
var alias1or2 = Model.Content.HasValue("alias1") ? Model.Content.GetPropertyValue("alias1") : Model.Content.GetPropertyValue("alias2");
}
@* get site name from global settings *@
@{
Umbraco.TypedContentSingleAtXPath("//globalSettings").GetPropertyValue("websiteName");
@* OR if specified in home page *@
Model.Content.AncestorOrSelf(1).GetPropertyValue<IPublishedContent>("websiteConfigurationNode").Descendant("globalSettings").GetPropertyValue("websiteName");
}
@* list of tags; can create page listing by tag -> https://shermandigital.com/blog/get-umbraco-content-by-tag/ *@
@{
var tagList = Model.Content.GetPropertyValue<IEnumerable<string>>("tags");
if (tagList != null)
{
@foreach (var tag in tagList)
{
<span>@tag</span>
}
}
}
@* get each item from a checkbox list *@
@{
foreach(var item in Model.Content.GetPropertyValue<IEnumerable<string>>("alias"))
{
item;
}
}
@* get item from dropdown list *@
@{
if (Model.Content.HasValue("alias"))
{
var dropdownValue = Model.Content.GetPropertyValue<string>("alias");
}
}
@* get multiple items from dropdown list *@
@{
if (Model.Content.HasValue("alias"))
{
var dropdownValues = Model.Content.GetPropertyValue<string>("alias").Split(',');
<ul>
@foreach (var dropdownValue in dropdownValues)
{
<li>@dropdownValue</li>
}
</ul>
}
}
@* get true/false value *@
@Model.Content.GetPropertyValue<bool>("alias");
@* get a grid layout *@
@Html.GetGridHtml(Model.Content, "alias", "bootstrap3");
@* or *@
@Model.Content.GetGridHtml(Html, "alias", "bootstrap3");
@* get an image *@
@{
var image = Model.Content.GetPropertyValue<IPublishedContent>("alias");
if (image != null)
{
<img src="@image.Url" alt="@image.Name">
<!-- use cropping; first argument is element and second is crop alias -->
<img src="@Url.GetCropUrl(image, "cropAlias")" alt="@image.Name">
}
}
@* get multiple images *@
@{
var images = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("alias");
foreach (var image in images)
{
<img src="@image.Url" alt="@image.Name"/>
}
}
@* get cropped image with Id *@
@{
var image = Model.Content.GetPropertyValue<IPublishedContent>("alias");
if (image != null) {
var imageCrop = Umbraco.Media(image.Id).GetCropUrl("cropAlias");
<img src="@imageCrop" alt="@image.Name">
}
}
@* get content from a list *@
@{
List<IPublishedContent> allItems = Model.Content.GetPropertyValue<List<IPublishedContent>>("alias");
}
@* go through the list *@
@if (allItems != null && allItems.Count > 0)
{
foreach (IPublishedContent singleItem in allItems)
{
if (singleItem != null)
{
var link = singleItem.Url;
var date = singleItem.GetPropertyValue<DateTime>("alias").ToString("dd.MM.yyyy");
var time = singleItem.GetPropertyValue<DateTime>("alias").ToString("HH:mm:ss");
var dateTime = singleItem.GetPropertyValue<DateTime>("alias").ToString("dd.MM.yyyy HH:mm:ss");
}
}
}
@* check that a string isn't empty *@
@if (!string.IsNullOrWhiteSpace(stringName))
{
<p>@stringName</p>
}
@* check if member is logged; returns a bool *@
@Umbraco.MemberIsLoggedOn();
@* Member listing *@
@{
int totalRecords;
var members = ApplicationContext.Current.Services.MemberService.GetAll(0, int.MaxValue, out totalRecords);
foreach (var member in members)
{
<p>member.Name</p>
}
}
@* use dictionnary for multiple languages *@
@umbraco.library.GetDictionaryItem("Name of the dictionary item");
@* escape razor @ by adding another @ in front *@
@@media only screen and (min-wdith: 768px) {}
@* color picker *@
@{
var color = Model.Content.GetPropertyValue("alias");
if (color != null)
{
<div style="background-color: @color">colored text</div>
}
}
@* content picker element *@
@{
IPublishedContent item = Model.Content.GetPropertyValue<IPublishedContent>("alias");
if (item != null)
{
<a href="@item.Url">
<p>@item.Name</p>
</a>
}
}
@* go through multi node tree picker *@
@{
var typedMultiNodeTreePicker = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("alias");
foreach (var item in typedMultiNodeTreePicker)
{
<p>@item.Name</p>
}
}