Asset snippets

The ERB files in core/text_formatting/views provide the default HTML snippets that are inserted into content_markup to replace asset tokens in content. It's important to note that this replacement occurs when the object is saved in the admin (not when rendering the public page).

You can use site rules to override these snippets. For example:

context('nws') do
  rule(:ingredient_for_asset_insertion) do |asset_type| 
    if asset_type == :video 
      'video' 
    elsif asset_type == :audio
      'audio' 
    end
  end
end

This rule says that for News pages and posts, if the asset type is :audio, then the site ingredient named audio.html.erb should be used. This will be found under ingredients/ in the site directory. Similarly, video.html.erb will be used for :video asset types. Since the rule returns nil otherwise, the default snippet will be used for any other asset type.

In your asset ingredient, you have access to a number of local variables:

  • asset - the Asset object that is being inserted
  • options - a hash of user-specified options (that were placed after the pipe in the <!--- 526f8e01ebae ---> token)
  • object_scope - the object whose content field is being processed
  • page - the page to which the object belongs
  • snippet - the default snippet in this directory that would be used to render the asset if you hadn't defined the ingredient rule.

You can use the snippet variable to fall back to the default behaviour if all the conditions for your special ingredient logic aren't met. For example:

<% if options[:my_special_flag] %>
  This is my special ingredient logic.
<% else %>
  <%= snippet.result(binding) %>
<% end %>

Here is a complete example of a custom audio ingredient that displays a flash player for mp3 audio files only:

<% 
  if asset.extension == "mp3" &&
    !options[:path_only] && 
    !options[:link_text] 
%>
  <object type="application/x-shockwave-flash"
    data="http://labs.inventivelabs.com.au/audio/player.swf"
    id="audioplayer1"
    height="24"
    width="290"
  >
    <param 
      name="movie"
      value="http://labs.inventivelabs.com.au/audio/player.swf"
    >
    <param 
      name="FlashVars"
      value="playerID=1&bg=0xf8f8f8&leftbg=0xeeeeee&lefticon=0x333333&
        rightbg=0xcccccc&rightbghover=0xeeeeee&righticon=0x333333&
        righticonhover=0x0000cc&text=0x666666&slider=0x0000cc&track=0xFFFFFF&
        border=0x666666&loader=0xdddddd&soundFile=<%= asset.public_filename %>"
    >
    <param name="quality" value="high">
    <param name="menu" value="false">
    <param name="wmode" value="transparent">
  </object>
<% else %>
  <%= snippet.result(binding) %>
<% end %>

See Editing text for details on the options that might be inserted by the GUI for a given asset type.

User notes

(You can add notes to this page using the form below.)


Only the note field is required.

Preview or