[HOWTO] Create powerful specific templates for posts types and categories

Typo 6.0.6 introduced custom post types, allowing you to chose which template you want to use for your posts. Since Typo 6.0.8, you can also define per category templates.

Let’s say you blog about tech and wine, and want to display wine posts differently. You upload an attached picture to every wine related post and want it to be displayed in every article. You also have a custom CSS for wine blocks, and don’t want people to comment your wine reviews (this to make the sample template more readable). You also want to apply this to the category page.

I. Prepare the path

First, login to your blog admin and go to Article -> Post Types. Create a new post type called Wine.

Custom field 2

See the template name? Remember it, because we’re going to use it somewhere soon.

Custom field 2

Now, create a new category in Articles -> Categories. We’ll call it Wine too.

Custom template creation

Note the permalink id somewhere because we’re going to reuse it very soon.

Custom template creation

II. Create the templates you need

You’re going to create 2 custom templates: one to display wine related articles, and one to display the Wine category.

First, create a templates called wine.html.erb in your theme/views/articles/ directory. If the views/articles directory doesn’t exist, create it. The template will probably look like something like this:

<div class="wine">
  <h2><%= link_to_permalink @article, @article.title %></h2>
  <p class="auth"><%= _("Posted by")%> <%= author_link(@article) %>
  <%= display_date_and_time @article.published_at %></p>
  <%= @article.html(:body) %>
  <!-- This display the attached images -->
  <% @article.resources.each do |upload| %>
    <img src='<%= "#{this_blog.base_url}/files/#{upload.filename}" %>' class="centered" />
  <% end %>
  <div class="extended">
    <%= @article.html(:extended) %>

<p class="meta">
  <%= article_links @article %>

You’re now going to do the same with the wine category. In your theme /views/categories, create a wine.html.erb template. This is adapted from the category/show.html.erb of one of the stock themes.

<h1><%= link_to "Wine", "#{this_blog.base_url}/category/#{params[:id]}" %></h1>

<div class='category-excerpt <%= "border" if @article_counter >= 0 %>' id="article-<%= article.id %>">
<% @articles.each do |article| %>
  <h2><%= link_to_permalink(article, article.title) %></h2>
  <p class="auth"><%= _("Posted by")%> <%= author_link(earticle) %>
  <%= display_date_and_time article.published_at %></p>
  <%= article.html(:body) %>
  <!-- This display the attached images -->
  <% article.resources.each do |upload| %>
    <img src='<%= "#{this_blog.base_url}/files/#{upload.filename}" %>' class="centered" />
  <% end %>
  <div class="extended">
    <%= article.html(:extended) %>
<% end %>

<div id='paginate'>
  <%= will_paginate @articles, { :previous_label => _('Previous'), :next_label => _('Next')  } %>

Here you are, you can now write about wine. Just don’t forget to chose the wine post type within the editor.

Custom template creation

Published on 20/08/2011 at 13h43 by Matijs van Zuijlen, tags

Creating advanced Typo plugins

2 days ago, there was a guy called Fish who joined us while I was idling on #typo. He was asking for a gallery plugin for his Typo blog. As I’m a photographer, having such a plugin had been on my TODO list for like years now, so I told him I would do a quick proof of concept during my daily train commute. A few hours later, when I went back home, the plugin was working. It was very simple, displaying files uploaded on a Typo blog with thumbnail, pagination, medium size and large size displayed in a lightbox. Indeed, it wasn’t enough, and Fish came back asking for multiple albums and picture selection, caption, so I came back to work. As a developper, Fish could probably have done it himself, but I’m more familiar with Typo internals, and even I didn’t know how far I could go with Typo plugin integration.

Tonight, during my daily commute, I extended my gallery plugin with full admin and frontend integration. The gallery is not working yet, but the biggest part is working: integrating a complexe plugin in a Typo blog. I wanted to share this with you as a HOWTO.

Before trying this, please be sure you’re using Typo HEAD as Friday April the 8th, or a Typo version above 6.0.4 (even though it’s not released yet) as you’ll need the route.rb modification I had to do.

Creating the plugin

As Typo plugins are just plain Rails plugins, creation is kinda trivial.

<typo:code> ./script/rails generate plugin typosampleplugin </typo:code>

You’re now going to create some folders to reflect a Rails application tree:

<typo:code> cd vendor/plugins/typosampleplugin mkdir -p app/controllers/admin app/models app/helpers/admin config app/views/admin </typo:code>

Good, you’re now ready to code the main stuff. Edit your new plugin, and let’s go!

Making your code accessible

Now, you’re going to make Typo access your code. Edit your init.rb file and add the following:

<typo:code lang=’ruby’> PLUGINNAME = ‘typosampleplugin’ PLUGINPATH = “#{::Rails.root.tos}/vendor/plugins/#{PLUGINNAME}” PLUGINCONTROLLERPATH = “#{PLUGINPATH}/app/controllers” PLUGINVIEWSPATH = “#{PLUGINPATH}/app/views” PLUGINHELPERPATH = “#{PLUGINPATH}/app/helpers” PLUGINMODELSPATH = “#{PLUGINPATH}/app/models”

config.autoloadpaths += %W(#{TypoSamplePlugin::PLUGINCONTROLLER_PATH} \

                        #{TypoSamplePlugin::PLUGIN_HELPER_PATH}      \
                        #{TypoSamplePlugin::PLUGIN_VIEWS_PATH}       \

ActionView::Base.send(:include, TypoSamplePlugin::Helper) </typo:code>

The routing

Unless Rails generators, your plugin won’t be allowed to change routes.rb. We want our end users to remove them if they don’t want them anymore. So we’re going to create our own config/routes.rb. It’s going to look like this:

<typo:code lang=’ruby’> ActionController::Routing::Routes.draw do |map| map.connect ‘sampleplugin/:action’, :controller => ‘typosample’, :action => ‘index’

%w{ sample_plugin }.each do |i|

map.connect "/admin/#{i}", :controller => "admin/#{i}", :action => 'index'
map.connect "/admin/#{i}/:action/:id", :controller => "admin/#{i}", :action => nil, :id => nil

end end </typo:code>

I know, we’re still using Rails 2 routing DSL. It’s bad but Rails 3 can’t handle everything we do.

The first block is for your frontend controllers, the second one for the admin. Easy as pie isn’t it?

The models

She’s a model and she’s looking good. I’d like to take her home that’s understood.

Add your models files in app/models exactly like on any Rails application. You can use any Active Record relations you want and access or extend existing Typo models.

To create your database schema, edit your init.rb file, and add the following code:

<typo:code lang=’ruby’> unless ::TypoSamplePlugin.tableexists? ActiveRecord::Schema.createtable(TypoSamplePlugin.table_name) do |t|

t.column :name,  :string
t.column :description,  :text

end end </typo:code>

Couldn’t be easy heh? Now let’s switch to the front and back office thing.

The front thing


To be able to display your content within your Typo blog layout, every front end controller will need the following code:

<typo:code lang=’ruby’> class TypoSamplePluginController < ActionController::Base unloadable layout :themelayout beforefilter :template_root

def template_root

self.class.view_paths = ::ActionController::Base.view_paths.dup.unshift(TypoSamplePlugin::PLUGIN_VIEWS_PATH)


def theme_layout

File.join("#{::Rails.root.to_s}/themes/#{Blog.default.theme}/views", Blog.default.current_theme.layout(self.action_name))


end </typo:code>

There’s certainly a cleaner way to do it by not repeating the code, but I’ll dig it later.


Nothing special here. Really.

The backend thing

OK, now you want to give your plugin a fancy back office? Let’s go.

Adding your module to the admin

Edit your init.rb. In your model creation block, add the following:

<typo:code lang=’ruby’> admin = Profile.findbylabel(‘admin’) admin.modules << :typosampleplugin admin.save

publisher = Profile.findbylabel(‘publisher’) publisher.modules << :typosampleplugin publisher.save

This will update both admin and publisher profiles giving them the rights to access your plugin admin.

Now, let’s add them to the access control list (and the menu as well). This is still in init.rb

<typo:code lang=’ruby’> AccessControl.map :require => [ :admin, :publisher, :contributor ] do |map| map.project_module :typosampleplugin, nil do |project|

project.menu    "My plugin meny",  { :controller => "admin/typo_sample_plugin" }
project.submenu "My submenu", {:controller => "admin/typo_sample_plugin_other" }

end end </typo:code>

Edit your lib/typosampleplugin.rb and add the following:

<typo:code lang=’ruby’> module Helper def classtyposampleplugin return classselectedtab if controller.controllername =~ /typosampleplugin/ class_tab
end end </typo:code>

This will allow you to manage the tabs highligh in the admin.


Your controllers will go to app/controllers/admin and will all look like this, pretty like normal admin controller:

<typo:code lang=’ruby’> module Admin; end

class Admin::TypoSamplePluginController < Admin::BaseController layout ‘administration’ unloadable


end </typo:code>


Typo plugins admin views look like normal admin views. Minimum code is:

<typo:code lang=’ruby’> <% @pageheading = (‘Sample plugin’) %> <% subtabs_for(:typosampleplugin) %> </typo:code>

You can access any admin helper like saveorcancel or linktonew.

Here we are (born to be kings). I’ll release Typo Gallery when I finish it, which could happen very soon as I’m thinking about quiting Flickr to host my photos somewhere like I used to do with Wordpress + Gallery2 in the past, many, many cycles ago.

Published on 08/04/2011 at 17h08 by Frédéric de Villamil, tags , , , ,

Moving to a new server... and to Ruby 1.9.2

If you can read this… I love starting a blog post with that sentence, it always teases something great, exciting, or just new. So, if you can read this, then, we’ve successfully moved to our new ass kicking server: Quad Core Xeon, 16GB RAM, 2*2TB hard drive on a Gigabit port with as much trafic as the machine can handle.

But that’s not the point.

This blog is running on a yet experimental but apparently working Ruby 1.9.2 compliant version. If you’ve been running 6.0.4 lately, then you can also try running Typo on a Ruby 1.9.2 environment.

This does not work out of the box though. You will need an UTF-8 aware Ruby 1.9 compliant database driver, just like sam-mysql-ruby. To do so, just edit Typo Gemfile and replace the gem 'mysql' line by gem 'sam-mysql-ruby'. If you’re using a custom theme, you’ll probably have to fix your theme encoding too.

As I’ve said, this is very experimental. However, if you’re willing to test this, we’ll be glad to hear your feedback. Just one thing though: don’t complain because you’re using a database driver that’s not UTF-8 compliant: we can’t do anything about it.

Published on 06/03/2011 at 17h43 by Frédéric de Villamil, tags , ,

Setting up a new plugins repository

Typo and Ruby on Rails both provide a powerful plugin interface, making extending your Typo blog easy. We wanted to make finding such an extension easy as well. Most plugins were hosted on Frédéric’s Github account, amongst many other projects, and the only way to know what is Typo related and what isn’t is to browse Typo Plugins Catalogue.

With its “fork and update” process, Github provides a fantastic opportunity to bring together a plugin repository from original code, tracking plugins evolution and updating as they come. That’s the reason why we have built the new Typo Plugin Repository, dedicated to hosting every Typo plugins available on Github. Typo plugins Catalogue will remain as a… catalogue, but browsing code will be made easier.

Just as a reminder, Typo plugins come in 3 flavour:

Typo sidebars plugins

You will recognise them easily because their name finish with _sidebar. You can enable from the admin interface and display using:

<typo:code lang=’ruby’> <%= render_sidebars %> </typo:code>


<typo:code lang=’ruby’> <%= rendersidebar(‘somesidebar’) %> </typo:code>

Typo text filters plugins

Their name start with typo_textfilter. Like the typo:code text filter or typo:flickr, they allow you to insert almost anything in your blog posts.

Other plugins

They are just basic Rails plugins being able to interact with your Typo blog. You will usually need to edit your template to make them work.

If you have created a Typo plugin and want it to be listed in our repository, just drop us a line at plugins@typosphere.org, we’ll be glad to review it and eventually add it.

Published on 03/10/2010 at 00h47 by Frédéric de Villamil, tags

What will be Typo 5.5 next major feature?

We’ve started to work on Typo 5.5 which will be our next release and wanted to add some missing feature. So we wanted to have your opinion on the next major feature you’d like to see on your Typo blogging sotware.

<a href="http://polldaddy.com/poll/3168363/">What will be Typo next major feature?</a>

Published on 07/05/2010 at 19h05 by Frédéric de Villamil, tags

Having Typo sending emails using Gmail (or any other TLS enabled SMTP)

By default, neither Typo nor Ruby On Rails can send emails using a TLS enabled server. To have Typo sending you new posts and comments notification, or even your admin password, you’ll have to tweak it a little bit. Here’s a step by step tutorial.

First, you’ll have to install the ActionMail Optional TLS plugin. From your Typo directory, just run:

<typo:code> ./script/plugin install http://github.com/collectiveidea/actionmaileroptional_tls </typo:code>

Easy as pie isn’t it?

Then, in your config/environment.rb, replace the following lines:

<typo:code lang=’ruby’> begin mailsettings = YAML.load(File.read(“#{RAILSROOT}/config/mail.yml”))

ActionMailer::Base.deliverymethod = mailsettings[‘method’] ActionMailer::Base.serversettings = mailsettings[‘settings’] rescue # Fall back to using sendmail by default ActionMailer::Base.delivery_method = :sendmail end </typo:code>


<typo:code lang=’ruby’> ActionMailer::Base.smtpsettings = { :tls => true, :address => “smtp.gmail.com”, :port => “587”, :domain => “YOURDOMAIN”, :authentication => :plain, :username => “GOOGLEUSERNAME”, :password => “GOOGLEPASSWORD” } </typo:code>

Restart your application. Enjoy having Typo sending you emails from Gmail.

Published on 16/04/2010 at 21h07 by Frédéric de Villamil, tags

News from a summerish Typo

This has been 4 months since Typo 5.3 release, and this calm month of July may be the perfect occasion to look at what happeded on the Typo planet.

Cyril and I have been quite busy lately. He quited his job and started a new adventure in a new company. Cyril also moved to Paris, which is great as we’re now able to gather and work together, when we have time. He’s also been dedicating more time to Oupsnow, a bug tracker he’s writing in Merb. I’ve also been quite busy lately, starting lots of Rails projects around Twitter, having less and less free time left by my daily job, and starting a number 3, delivery due next December, so I’ll have to move soon. And that takes a lot of time too.

Hopefully, we had many contributors who came to help us while we were too busy to care about Typo. Erik Ostrom updated Wordpress converter, Michael Reinsch fixed some bugs and did some nice refactoring, and Wei Jen Lu did a fantastic job on translating Typo to Chinese. Wel also had a couple of people submitting patches fixing bugs on Lighthouse. May they be all thanked for the great job they did.

Our main concern lately has been reducing Typo memory footprint. We’ve already dropped lots of useless code and fixed a few bugs, even though there’s still lots of work to be done. By useless code, we mean 3 things:

  1. Removing dead code that is no longer called anywhere. And there was plenty of it, trust me. Typo is an old house, inhabited by lots of different people, and every house needs a bit of cleaning sometimes.

  2. Code that’s trying to reinvent the wheel when the same functionnality has been integrated into Rails for a while now. I know we still carry a lot of this one, and we still have a lot of refactoring to do.

  3. Code that should simply not be in a blogging engine core and can be moved elsewhere as a plugin. Deciding what to keep and what to drop is not always easy, but that’s the usual step in the life of a software.

I’m not really sure yet, but I don’t think Typo next release will carry any major feature. Instead, we’re focusing on performance improvement, bug fixes and usability improvement here and there. I guess it’s a needed step to take before restarting on a fresh, clean ground.

Published on 12/07/2009 at 15h05 by Frédéric de Villamil, tags , , , ,

Moving from trac to redmine and other upcoming plans

Once again, we’re late on our planned schedule since Typo 5.0.4 should have been released 5 days ago. Piers and I have been pretty busy lately, and I’m now waiting for some external material in order to release a 5.0.4b1.

Since I was moving Typosphere to a new server, a nice 2.33ghz dual core with 4gb RAM running an Apache and mod_rails on FreeBSD, I also wanted to stop using trac as a bug tracker. In spite of being used by many open source projects around, trac is just a bloatware. I was really fed up with cleaning the session table twice a month just to avoid having it consuming 99% of my server resources. There was another reason why I wanted to ditch Trac: it’s Python, and Redmine is Ruby. As far as I know, every URL but the RSS has been redirected properly.

Typogarden is going to change too and become a theme repository based on a Typo install. I will certainly call for gardeners someday to maintain the thing once I’ve installed the new Typogarden in its new home.

More to come soon, stay tuned.

Published on 05/06/2008 at 13h50 by Frédéric de Villamil, tags , , , ,

Typo now comes baked in 6 languages

Not many news since Christmas rush and Typo 5.0 release, mostly because we’ve been rather busy since then. I’ve quited my job, and released a second kids about 10 days ago, which took me most of my spare time. Typo is not dead however, so expect a 5.0.3 bugfix release soon, coming with 2 new translations thank to DAddYE and Togawamanabu. Next time you’ll upgrade your Typo, you’ll have it natively running in :

  • English.
  • French.
  • German.
  • Italian.
  • Japanese.
  • Romanian.

Many thanks to all the contributors who have translated the application so far.

Published on 11/02/2008 at 18h57 by Frédéric de Villamil, tags , , , ,

Typo 5.0.x temporarily retired for critical issue

There’s a critical bug in Typo 5.0 that has forced us to retire Typo 5.0.x from Rubyforge until we find out what really happens.

It seems that, under certain still undefined cicumstances, when runnin SQLite, which is at the same time our default installer and Ruby on Rails 2.0 database, Typo just erases itself with its parent directory.

We’re actively working on this issue, even on new year’s eve, trying first to find out if it’s a Typo or Rails VS SQLite matter. Be sure we’ll release a 5.0.2 version fixing everything as soon as possible. We apologise for the inconvenience.

Published on 01/01/2008 at 08h57 by Frédéric de Villamil, tags ,

Powered by Publify | Photo Startup stock photos