以编程方式渲染 Umbraco 节点
我正在使用 Umbraco 4.5.2,并且我有一个带有多个子节点的节点。每个子节点代表将在控件中呈现的 HTML 片段。该控件循环遍历所有子节点并渲染它们。
目前,为了让事情顺利进行,我正在进行一些肮脏的黑客活动(对 Umbraco 来说仍然相当新),但我宁愿做得更好。
我现在的代码看起来像这样:
private string GetItemHtml(Node node)
{
// Work out the URL of the HTML fragment
string url = "http://" + Context.Request.Url.Host +
":" + Context.Request.Url.Port +
node.Url;
// Get the fragment by making a call to the page
WebRequest req = WebRequest.Create(url);
WebResponse res = req.GetResponse();
using (Stream stream = res.GetResponseStream())
{
StreamReader reader = new StreamReader(stream);
string result = reader.ReadToEnd();
return result;
}
}
正如你所看到的,它确实相当难看。我希望有某种方法可以得到这个,而不必进行许多 HTTP 调用,即使它循环回同一服务器 - 它不会非常有效。
I'm using Umbraco 4.5.2 and I have a node with a number of child nodes. Each child node represents a fragment of HTML that will be rendered in a control. The control loops over all the child nodes and renders them.
For the moment I have a bit of a dirty hack going in order to get the thing going (still fairly new to Umbraco) but I'd rather do this better.
The code I have at the moment looks like this:
private string GetItemHtml(Node node)
{
// Work out the URL of the HTML fragment
string url = "http://" + Context.Request.Url.Host +
":" + Context.Request.Url.Port +
node.Url;
// Get the fragment by making a call to the page
WebRequest req = WebRequest.Create(url);
WebResponse res = req.GetResponse();
using (Stream stream = res.GetResponseStream())
{
StreamReader reader = new StreamReader(stream);
string result = reader.ReadToEnd();
return result;
}
}
As you can see, it is really rather ugly. I'm hoping there is some way to get this without having to make many HTTP calls, even if it is looping back to the same server - it can't be very efficient.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 API 来实现您所要求的,尝试查看 umbraco.library.RenderTemplate 方法。它接受两个参数,第一个是要渲染的节点的 id,第二个是渲染节点时要使用的模板的 id。
You can use the API to achieve what you are asking, try looking at the umbraco.library.RenderTemplate method. It accepts two parameters, the first is the id of the node to render and the second is the id of the template to use when rendering the node.
在 umbraco 中使用 xslt 构建可能要容易得多。如果您想要执行 xslt 中无法完成的操作,您可以创建一个 XSLT 扩展函数(用 C# 实现,从 XSLT 调用)来执行此操作(请参阅 http://en.wikibooks.org/wiki/Umbraco/Create_xslt_exstension_like_umbraco.Library_in_C 了解更多信息)。
有关列出子页面的示例 XSLT,请参阅 umbraco 博客包中的 BlogListPosts.xslt:
http://blog4umbraco.codeplex.com/SourceControl/changeset/view/54177#916032
This is probably much easier to build using xslt in umbraco. If you want to do something that is not possible in xslt, you can create a XSLT extension function (implemented in C#, called from XSLT) to do that (see http://en.wikibooks.org/wiki/Umbraco/Create_xslt_exstension_like_umbraco.Library_in_C for more info).
For a sample XSLT that list child pages, see BlogListPosts.xslt in the umbraco blog package:
http://blog4umbraco.codeplex.com/SourceControl/changeset/view/54177#916032