سؤال

I'm trying to write a docpad plugin that will allow me to insert meta tags unique to each page, for example og:title or og:description. I've been able to accomplish this globally with the populateCollections event for global values, but have not been able to do this per page.

I'd like for this to work without the need for a template function so that the meta tag is inserted automatically based on the document's meta. One way might be to grab the contentRendered value in the writeBefore event and do string manipulation that way, but that seems hacky.

Any ideas?

هل كانت مفيدة؟

المحلول

This worked for what I needed. Basically, I'm getting the rendered content right before the file is written using the writeBefore event, and doing a very simple string replace which adds the meta tags and their unique values, which is pulled from the model in the collection.

writeBefore: (opts) ->
        docPad = @docPad
        templateData = docpad.getTemplateData()
        siteUrl = templateData.site.url

        for model in opts.collection.models
            if model.get('outExtension') == 'html'
                url = @getTag('og:url', siteUrl+model.get('url')) 
                title = @getTag('og:title', model.get('title'))
                content = model.get('contentRendered')
                if content
                    content = content.replace(/<\/title>/, '</title>'+url+title+description)
                    model.set('contentRendered', content)
# Helper
getTag: (ogName, data) ->
    return "\n    <meta property=\"#{ogName}\" content=\"#{data}\" />"

نصائح أخرى

Great answer David, leaving this one if someone faced the same issue I did.

Check if meta tag is broken, if it is - don't render:

renderBefore: (opts) ->
        for model in opts.collection.models
           if model.get('date').toLocaleDateString()=='Invalid Date'
              model.set('write', false)
              docpad.log model.get('title')+' has broken date format!\n\n\n\n\n' 
              false

I am using partials in with collections. Adding what is needed in the document like this:

```
title: Meetings and Events
layout: page
description: "This is my custom description."
tags: ['resources']
pageOrder: 3
pageclass: rc-events
```

I needed a custom CSS class by page. Then you can call it in your default template like this.

<div id="main" class="container <%= @document.pageclass %>">

Should be the same for meta

<meta name="description" content="<%= @document.description) %>" />

or check your docpad.coffee file and put together helper function for prepared content based off of a default site value combined with a @document value. Then you can just call something like the default:

<meta name="description" content="<%= @getPreparedDescription() %>" />

Which is built by this helper function:

# Get the prepared site/document description
getPreparedDescription: ->
    # if we have a document description, then we should use that, otherwise use the site's description
    @document.description or @site.description
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top