using System; using System.Collections.Generic; using System.Linq; using System.Text; using umbraco.DataLayer; using umbraco.businesslogic; using umbraco.MacroEngines;
namespace UcommentHelper { public static class Comments { public static string GetCommentCountForNode(int nodeid) { ISqlHelper helper = DataLayerHelper.CreateSqlHelper(umbraco.GlobalSettings.DbDSN);
int? id = (int?)helper.ExecuteScalar<Int32>("select count(*) from comment where nodeid = @nodeid and spam != 1", new IParameter[] { helper.CreateParameter("@nodeid", nodeid) }); if(id.HasValue) { return id.ToString(); } return ""; }
Did you ever figure out the XSLTLibrary does not exist error you were getting? I get the same error which is why I'm asking. I'm just using a seperate razor file for my news feed and wanted to pull the number of comments associated with a post but I'm not sure how.
UComment and Razor
has anyone tried working with UComment and razor and want to share some code like number of comments of node and getting comments for a node?
I just created a class library project with a static class (see below). Then in one of my Templates I used the razor code:
@UcommentHelper.Comments.GetCommentCountForNode(@item.Id) Comment(s)
//static class UcommentHelper
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using umbraco.DataLayer;
using umbraco.businesslogic;
using umbraco.MacroEngines;
namespace UcommentHelper
{
public static class Comments
{
public static string GetCommentCountForNode(int nodeid)
{
ISqlHelper helper = DataLayerHelper.CreateSqlHelper(umbraco.GlobalSettings.DbDSN);
int? id = (int?)helper.ExecuteScalar<Int32>("select count(*) from comment where nodeid = @nodeid and spam != 1", new IParameter[] { helper.CreateParameter("@nodeid", nodeid) });
if(id.HasValue)
{
return id.ToString();
}
return "";
}
}
}
I've translated ucommentlisctcomment to razor
@inherits umbraco.MacroEngines.DynamicNodeContext
@using umbraco.MacroEngines
@using UComment
@using System.Xml.XPath
@{
var title = Model.Name;
XPathNodeIterator nodesroot = XSLTLibrary.GetCommentsForNode(Model.Id);
XPathNodeIterator nodes = nodesroot.Current.Select("//comment");
int cnt = nodes.Count;
}
@functions {
}
@if (cnt > 0)
{
nodes.MoveNext();
<h3>Comments for "@title": @cnt.</h3>
<ol class="commentlist">
@for (int i = 0; i < cnt; i++)
{
XPathNavigator cur = nodes.Current;
string url = cur.SelectSingleNode("./website").Value;
string name = cur.SelectSingleNode("./name").Value;
string email = cur.SelectSingleNode("./email").Value;
<li class="comment alt" id="[email protected]("id","")">
<div class="comment-author vcard">
<img class="photo avatar avatar-32 photo" width="32" height="32" src="@UComment.XSLTLibrary.getGravatar(email, 40, "")" alt="Gravatar of @name"/>
<span class="fn n">
@if (!String.IsNullOrWhiteSpace(url))
{@name}
else
{
<a class="url url" rel="external nofollow" href="@url">@name</a>
}
</span>
</div>
<em class="comment-meta">
Published @umbraco.library.LongDate(cur.GetAttribute("created", ""), true, " at ")
</em>
<p>
@Html.Raw(umbraco.library.ReplaceLineBreaks(cur.SelectSingleNode("./message").InnerXml))
</p>
</li>
nodes.MoveNext();
i++;
}
</ol>
}
and dont forget to change & to &
ucomment.library.base
public static string GetGravatarImage(string email, int size)
{
if (isValidEmail(email))
{
return string.Format("http://www.gravatar.com/avatar/{0}?s={1}&d=wavatar", umbraco.library.md5(email), size.ToString());
}
else
{
return "";
}
}
thank you both for your help
I'm also trying to use Razor with the uComment package but I'm getting the following error when saving the Razor file:
The type or namespace name 'XSLTLibrary' does not exist in the namespace 'UComment' (are you missing an assembly reference?)
The error is referencing the following line:
XPathNodeIterator nodesroot = UComment.XSLTLibrary.GetCommentsForNode(Model.Id);
The UComment.dll is in the Umbraco /bin directory and I've checked the UComment source but the XSLTLibrary should be there.
Am I missing something?
Did you ever figure out the XSLTLibrary does not exist error you were getting? I get the same error which is why I'm asking. I'm just using a seperate razor file for my news feed and wanted to pull the number of comments associated with a post but I'm not sure how.
Niek sensibly re-posted the question here:
http://our.umbraco.org/forum/developers/razor/28420-RazoruComment-missing-assembly-reference-problem
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.