How about this:

tag 'date' do |tag|
  page = tag.locals.page
  format = (tag.attr['format'] || '%A, %B %d, %Y')
  which = tag.attr['which']
  if which
    if (which == 'now')
      date = Date::Time.now
    elsif ['published_at', 'created_at', 'updated_at'].include? which
      date = page[which]
    else
      raise TagError, "Invalid value for 'which' attribute."
    end
  else
    date = page.published_at || page.created_at
  end

  date.strftime(format)
end



Notice that I made sure it's a valid attribute, otherwise raising an error.

If none of the core team has problems with this, I'll write some tests 
and patch it into mental.

Sean


Gabriel Lamounier wrote:
> Hi All,
>
> I'm trying to code the new date tag we talked about earlier, but as
> I'm new to ruby, I'm posting the code for you to have a look.
>
> desc %{
>   Renders the actual date or the date the page was published, created or 
> updated
>   (in the event that it has not been published yet, the date that it
> was created).
>   The format attribute uses the same formating codes used by the Ruby
> @strftime@
>   function. By default it's set to @%A, %B %d, [EMAIL PROTECTED]
>
>   *Usage:*
>   <pre><code><r:date [which="date_type"] [format="format_string"]
> /></code></pre>
> }
> tag 'date' do |tag|
>   page = tag.locals.page
>   format = (tag.attr['format'] || '%A, %B %d, %Y')
>
>   if tag.attr['which']
>     if (tag.attr['which'] == 'now')
>       date = Date::Time.now
>     else
>       which = tag.attr['which']   # are this two lines correct?
>       date = page.which          # i don't know if it's the way to do it
>     end
>   else
>     date = page.published_at || page.created_at
>   end
>
>   date.strftime(format)
> end
>
> date_type would be [created_at | published_at | updated_at | now]
>
> I need some help
>
> Thanks,
> Gabriel
> _______________________________________________
> Radiant mailing list
> Post:   [email protected]
> Search: http://radiantcms.org/mailing-list/search/
> Site:   http://lists.radiantcms.org/mailman/listinfo/radiant
>
>   
_______________________________________________
Radiant mailing list
Post:   [email protected]
Search: http://radiantcms.org/mailing-list/search/
Site:   http://lists.radiantcms.org/mailman/listinfo/radiant

Reply via email to