Skip to content

Commit

Permalink
Updated README
Browse files Browse the repository at this point in the history
* Added WithContent as a simpler first pipeline
* Improved comments in the snippets
  • Loading branch information
alanta committed Nov 6, 2020
1 parent 9c284a7 commit eb45081
Showing 1 changed file with 60 additions and 18 deletions.
78 changes: 60 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,6 @@ The KontentModelGenerator tool generates a C# class for each of the content type

The KomtentModelGenerator makes it easy to keep your code in sync with your Kontent project. You'll need to re-run it whenever you make changes to the content model in Kontent.

### Add a Razor view

* Put a Razor view named `Article.cshtml` into `/input`:

```cshtml
@model My.Models.Article
<h3>@Model.Title</h3>
@Model.BodyCopy
```
_As you can see, the Razor view uses the model generated by Kontent. If you open the project in Visual Studio, you'll get code completion and feedback to help you write your view._

### Setup Statiq

* Configure the Delivery client for your Kontent project
Expand Down Expand Up @@ -81,7 +68,62 @@ public static class Program
.RunAsync();
}
```
* For the pipeline, add the following code to your project:
### Output content straight to file

* Add a pipeline to your project. The pipeline will automatically detected and executed by Statiq.

```c#
using Kentico.Kontent.Delivery.Abstractions;
using Kontent.Statiq;
using Statiq.Common;
using Statiq.Core;
using My.Models; // <== your model namespace
public class ArticlesPipeline : Pipeline
{
public ArticlesPipeline(IDeliveryClient client)
{
InputModules = new ModuleList{
// Load all articles from Kontent,
new Kontent<Article>(client)
// Use the BodyContent field as the document content
.WithContent(page => page.BodyContent),

// Set the output path for each article
new SetDestination(Config.FromDocument((doc, ctx)
=> new NormalizedPath( $"article/{doc.AsKontent<Article>().UrlPattern}.html"))),
};

OutputModules = new ModuleList {
// Write each article to disk
new WriteFiles()
};
}
}
```

### Run the generator

* Run the generator: `dotnet run`

You should now see that for every Article in the Kontent site there's an html file in the `\output\article` folder.

### Rendering a Razor view

For more control over your output, you can feed the rich content model provided by Kontent into Razor views. This is very similar to an ASP.NET (Core) web application.

* Add a Razor view named `Article.cshtml` into `/input`:

```razor
@model My.Models.Article
<h3>@Model.Title</h3>
@Model.BodyCopy
```
The Razor view uses the c# model class generated by Kontent. If you open the project in Visual Studio, you'll get code completion and feedback to help you write your view.

* Replace the Articles pipeline from the previous section with the following code:

```c#
using Kentico.Kontent.Delivery.Abstractions;
Expand All @@ -104,7 +146,7 @@ public class ArticlesPipeline : Pipeline
};

ProcessModules = new ModuleList {
// Pull in the Razor view
// Pull in the Razor view, the path is relative to /input
new MergeContent(new ReadFiles(patterns: "Article.cshtml") ),
// Render the Razor view into the content of the document
new RenderRazor()
Expand All @@ -114,16 +156,16 @@ public class ArticlesPipeline : Pipeline
};

OutputModules = new ModuleList {
// Write each article to disk
new WriteFiles()
};
}
}
```
### Run the generator

* Run the pipeline `dotnet run`
* Run the generator again: `dotnet run`

You should now see that for every Article in the Kontent site there's an html file in the `\output\article` folder.
The generated html files are in the `\output\article` folder.

This is a very basic pipeline and gives you everything you need to get started with content served from a Kontent project. But Kontent has more advanced features and some nice extras that you can leverage to get better integration and more robust code.

Expand Down

0 comments on commit eb45081

Please sign in to comment.