Skip to content

Instantly share code, notes, and snippets.

@jhauge
Created September 6, 2012 12:26
Show Gist options
  • Save jhauge/3655731 to your computer and use it in GitHub Desktop.
Save jhauge/3655731 to your computer and use it in GitHub Desktop.
Razor / uQuery example
using umbraco.NodeFactory;
using umbraco.interfaces;
namespace NyborgBy.Repositories.Entities
{
public abstract class NodeItem
{
protected NodeItem()
{
}
protected NodeItem(INode node)
{
Id = node.Id;
NodeName = node.Name;
NodeType = node.NodeTypeAlias;
Url = node.Url;
UrlName = node.UrlName;
Node = (Node) node;
}
public int Id { get; set; }
public string NodeName { get; set; }
public string NodeType { get; set; }
public string Url { get; set; }
public string UrlName { get; set; }
public Node Node { get; set; }
}
}
namespace NyborgBy.Repositories
{
public class NodeRepository
{
public static List<TeaserItem> GetTeaserItemList(string idCsv)
{
if (string.IsNullOrWhiteSpace(idCsv))
return new List<TeaserItem>();
return uQuery.GetNodesByCsv(idCsv).Select(n => new TeaserItem(n)).ToList();
}
}
}
@using Eksponent.CropUp;
@using NyborgBy.Repositories
@using NyborgBy.Repositories.Entities
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
var teasers = NodeRepository.GetTeaserItemList(@Model.TopTeaserSlider.ToString());
}
@foreach (TeaserItem teaser in teasers)
{
<div class="danehofBannerSnippet">
<img src="@CropUp.GetUrl(teaser.PictureUrl, new ImageSizeArguments { CropAlias = "eventteaser" })" alt="@teaser.Header" />
<div>
<h2>@teaser.Header</h2>
<p>@Html.Raw(teaser.Description)</p>
</div>
</div>
}
using System.Collections.Generic;
using umbraco;
using umbraco.NodeFactory;
using umbraco.cms.businesslogic.media;
using umbraco.interfaces;
namespace NyborgBy.Repositories.Entities
{
public class TeaserItem : NodeItem
{
public TeaserItem(string nodeId) : this(int.Parse(nodeId))
{
}
public TeaserItem(int nodeId) : this(new Node(nodeId))
{
}
public TeaserItem(INode node) : base(node)
{
Header = Node.GetProperty<string>("header");
Description = Node.GetProperty<string>("description");
switch (Node.NodeTypeAlias)
{
case "Textpage":
Type = "text";
BodyText = Node.GetProperty<string>("abstract");
PictureUrl = CreatePictureUrl(Node.GetProperty<string>("pagePicture"));
break;
case "PictureTeaser":
Type = Node.GetProperty<string>("teaserType");
BodyText = Node.GetProperty<string>("bodyText");
Links = NodeRepository.GetLinks(Node.GetProperty<string>("links"));
PictureUrl = CreatePictureUrl(Node.GetProperty<string>("picture"));
break;
case "VideoTeaser":
break;
}
}
public string Type { get; set; }
public string Header { get; set; }
public string Description { get; set; }
public string BodyText { get; set; }
public string PictureUrl { get; set; }
public List<Link> Links { get; set; }
private string CreatePictureUrl(string mediaId)
{
int id;
if (string.IsNullOrWhiteSpace(mediaId) || !int.TryParse(mediaId, out id))
{
return "/images/no-picture.jpg";
}
var media = new Media(id);
return media.GetImageUrl();
}
}
}
@jhauge
Copy link
Author

jhauge commented Sep 6, 2012

I'm not at big fan of dynamic typing and a lot of code in my razor files,
so I've devised this scheme that uses a combination of hadn coded entity
classes, a repository class and that enables me to have strongly typed
entities in my razor files, and the possibility of creating lists of entities
using uQuery methods in the repository.

This approach is only possible in VS based Umbraco projects of course.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment