简体   繁体   English

Umbraco-如何使用代码访问内容蓝图(模板)

[英]Umbraco - How do I access a Content Blueprint (template) in code

So I know that I am able to obtain a content node with the following code: 因此,我知道我可以使用以下代码获取内容节点:

Node contentNode = new Node(contentId);

I would like to access a content blueprint from within my code in a similar fashion, how would I go about this? 我想以类似的方式从代码中访问内容蓝图,我将如何处理?

Content templates are simply .cshtml view files on disk. 内容模板只是磁盘上的.cshtml查看文件。 You can get the path of the view file for a template, using the FileService in the Umbraco API (it has methods for templates, partial views, scripts & stylesheets...). 您可以使用Umbraco API中的FileService获取模板视图文件的路径(它具有模板,部分视图,脚本和样式表的方法...)。 The Name property should have the filename including extension. Name属性的文件名应包括扩展名。

Then it's just a matter of editing the file if that is what you wanted to do. 然后,如果您要这样做,则只需编辑文件即可。

The code editors in the backoffice are basically just getting and saving files from disk using the CodeFileController so you can have a look at that for inspiration if you have to do something similar. 后台的代码编辑器基本上只是使用CodeFileController从磁盘上获取和保存文件,因此如果您必须执行类似的操作,则可以从中获得启发。

I clearly didn't understand what a content template was, so thank you Claus for explaining this to me. 我显然不明白什么是内容模板,因此感谢克劳斯向我解释一下。

I did eventually find the solution to my problem. 最终,我确实找到了解决问题的方法。 It turns out that content templates are also referred to as blueprints. 事实证明,内容模板也称为蓝图。 You can obtain the content template (AKA blueprint) with the following code: 您可以使用以下代码获取内容模板(AKA蓝图):

ApplicationContext applicationContext = ApplicationContext.Current;

// Obtain the Umbraco Content Service
var contentService = applicationContext.Services.ContentService;

// Obtain content template
var contentTemplate = contentService.GetBlueprintById(contentTemplateId);

If you then wanted to create a content node from this blueprint you could use: 然后,如果您想根据此蓝图创建内容节点,则可以使用:

// Create content node from content template
var content = contentService.CreateContentFromBlueprint(contentTemplate, "Content Node Name");

// Save and publish to new content node
Attempt<Umbraco.Core.Publishing.PublishStatus> publishStatus = contentService.SaveAndPublishWithStatus(content);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM