Customization | laravel-feed-go | Spatie       

 SPATIE  

laravel-feed-go
===============

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-feed-go](https://spatie.be/docs/laravel-feed-go/v1)  Customization

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-feed-go/v1) 

  Customization    
- [ Introduction ](https://spatie.be/docs/laravel-feed-go/v1/introduction)
- [ Installation and setup ](https://spatie.be/docs/laravel-feed-go/v1/installation-and-setup)
- [ Usage ](https://spatie.be/docs/laravel-feed-go/v1/usage)
- [ HTTP integration ](https://spatie.be/docs/laravel-feed-go/v1/http)
- [ Customization ](https://spatie.be/docs/laravel-feed-go/v1/customization)
- [ Testing ](https://spatie.be/docs/laravel-feed-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/laravel-feed-go/v1/releasing)
- [ Changelog ](https://spatie.be/docs/laravel-feed-go/v1/changelog)
- [ Support us ](https://spatie.be/docs/laravel-feed-go/v1/support-us)

 Customization
=============

###  On this page 

1. [ Convert application models ](#content-convert-application-models)
2. [ Customizing your feed views ](#content-customizing-your-feed-views)
3. [ Automatically generate feed links ](#content-automatically-generate-feed-links)

Convert application models
--------------------------------------------------------------------------------------------------------------------------------------

Imagine you have a model named `NewsItem` that contains records you want displayed in the feed. Implement `Feedable` on that model. It expects one method, `ToFeedItem`, which returns an `Item`.

```
type NewsItem struct {
    ID, Title, Summary, Link, AuthorName string
    UpdatedAt time.Time
}

func (n NewsItem) ToFeedItem() feed.Item {
    return feed.Item{
        ID: n.ID,
        Title: n.Title,
        Summary: n.Summary,
        Updated: n.UpdatedAt,
        Link: n.Link,
        AuthorName: n.AuthorName,
    }
}
```

Next, create a function that returns all the items that must be displayed in the feed. Name it anything you like and do any query you want. Return `feed.ItemsOf(models)` to convert a `[]NewsItem`, or return `[]feed.Item` directly. The package owns no database models, jobs or profiles to replace. Your model and storage stay yours.

Capture arguments in a loader closure rather than using PHP class and method strings:

```
Items: func(ctx context.Context) ([]feed.Item, error) {
    articles, err := repository.Published(ctx, "news")
    if err != nil {
        return nil, err
    }
    return feed.ItemsOf(articles), nil
},
```

`repository` is your application-owned repository. Conversion is synchronous. Keep fallible work in the loader and pass its context to database and HTTP operations.

Customizing your feed views
-----------------------------------------------------------------------------------------------------------------------------------------

The package provides built-in serializers for Atom, RSS and JSON Feed. You can use a custom view per feed by setting `Feed.View`, such as `"feeds.news"` for `resources/views/feeds/news.html`.

The view receives a `Feed` value. `.Title`, `.Description`, `.URL`, `.HomePageURL`, `.Language`, `.Image`, `.Stylesheet`, `.Updated` and `.Items` are available. URLs are resolved and `.Updated` holds the latest entry timestamp. Each item has the same exported fields as `feed.Item`. Absent enclosures are nil and other optional strings are empty. Dates remain `time.Time` values for your template to format.

Laravel views use `html/template`, not an XML template engine. For custom XML output, use a renderer with `encoding/xml` or an application-owned `text/template` whose escaping you control. The renderer receives the same normalized `Feed` value:

```
Renderer: func(ctx context.Context, f feed.Feed) ([]byte, error) {
    return myXMLRenderer(ctx, f)
},
```

`myXMLRenderer` is your function. `Renderer` takes precedence over `View`. Both receive validated entries and preserve errors. `View` needs an active Laravel application context. A renderer may run without Laravel if it does not access framework services. The response keeps the format's media type unless `ContentType` overrides it. Empty Atom feeds still require `Updated`.

Automatically generate feed links
-----------------------------------------------------------------------------------------------------------------------------------------------------------

To discover a feed, feed readers look for a tag in the head section of your HTML documents:

```
<link rel="alternate" type="application/atom+xml" title="News" href="https://example.com/feed">
```

Add `{{feedLinks}}` inside your native Laravel layout's `<head>`. It replaces PHP's `feed::links` partial and `<x-feed-links />` component. The helper returns trusted, escaped markup in configuration order, without running item loaders.

From Go, call `feed.From(r).Links(r)`. For standalone feeds, call `feed.DiscoveryLinks(atom, rss, jsonFeed)`. Both return `(template.HTML, error)`. Pass the result directly as view data rather than marking unescaped application strings as trusted HTML.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
