Skip to content

Examples

rzig edited this page Feb 25, 2017 · 9 revisions

Carpet has many different ways to render a model's field with Redcarpet. These examples will hopefully guide you when you use Carpet in your model. Before trying out the examples, make sure you have installed Carpet.


Render a field with Redcarpet's default renderer.

  class Post < ApplicationRecord
    redcarpetable :body
  end

By adding this line to your model, the body field can now accept markdown. To get the rendered markdown, use this code:

  somepost.rendered_body # Returns the body of the post as HTML

This will return the body of the post as HTML. If you were to use this in a view, it would look like this:

  <%= @post.rendered_body %>

Render multiple fields with Redcarpet's default renderer.

  class Post < ApplicatonRecord
    redcarpetable :body, :intro
  end

When this line is added to the model, it creates a method to show the rendered markdown for the body, and a method to show the rendered markdown for the intro. To view the rendered content, do this:

  somepost.rendered_body # Returns the body of the post as HTML
  somepost.rendered_intro # Returns the intro of the post as HTML

To get the HTML for these fields in a view, do this:

  <%= @post.rendered_body %> <!-- Render the post's body in the HTML document -->
  <%= @post.rendered_intro %> <!-- Render the post's intro in the HTML document -->

Render a field with Redcarpet's default renderer, and provide options for the default renderer.

  class Post < ApplicationRecord
    redcarpetable :body, render_opts: {no_links: true}
  end

By adding this code to your model, the body field can now accept markdown, but will not render links. Access the rendered markdown as you normally would.


Render multiple fields with Redcarpet's default renderer, and provide options for the default renderer.

  class Post < ApplicationRecord
     redcarpetable :body, :intro, render_opts: {no_links: true}
  end

This will generate two methods that generate the rendered body and intro, however they will not render links. You can get the rendered markdown as you normally would.


Render multiple fields in a model with Redcarpet's default renderer, and provide different options to the default renderer for each field.

  class Post < ApplicationRecord
    redcarpetable :body, render_opts: {no_links: true}
    redcarpetable :intro, render_opts: {generate_toc_data: false}
  end

This will generate a method to render the body and intro, and that method will call the default Redcarpet renderer with the options specified. Get the rendered content as you normally would.


Access the rendered field with a different name than rendered_field.

  class Post < ApplicationRecord
    redcarpetable :body, as: ["cool_body", "rendered_body"]
  end

This will allow you to get the rendered body like this:

  somepost.cool_body # Returns the rendered body because you specified cool_body in the as array
  somepost.rendered_body # Returns the rendered body because you specified rendered_body in the as array

Render multiple fields in your model, with a different prefix than rendered_

  class Post < ApplicationRecord
    redcarpetable :body, :intro, prefixes: ["cool", "neat", "rendered"]
  end

With this code, you can now access the rendered text with

  somepost.cool_body
  somepost.neat_body
  somepost.rendered_body

In a view, access it like this:

  <%= somepost.cool_body %>
  <%= somepost.neat_body %>
  <%= somepost.rendered_body %>

Render a field in a model with your own Redcarpet renderer.

    class Post < ApplicationRecord
        redcarpetable :body, renderer: :blog_renderer
    end

To use the blog_renderer, you'll have to generate a new renderer. Once that renderer has been generated, edit the renderer in app/redcarpet_renderers/blog_renderer.rb. Here you can add all of your methods for rendering markdown with Redcarpet. You can access the custom rendered markdown as you normally would.


Render multiple fields in a model with your own Redcarpet renderer.

    class Post < ApplicationRecord
        redcarpetable :body, :intro, renderer: :blog_renderer
    end

Just like in the above example, you'll have to generate and edit the renderer. Once you have done that, you can access the markdown that was rendered with your custom renderer as you would normally.


The options that were shown above can be mixed and matched, so you could, for example use a custom renderer for the body field and then use the as option to access the rendered body with somepost.html_body.