To content | To menu | To search

Wednesday 15 October 2008

A specific stylesheet for each module

So this is the first post of the newly opened symfony category of this blog, and I want to make things clear right now: you (most likely) won't find (yet) any protip or high level symfony tutorials here, as I'm still in the process of learning symfony. The good news though is that I'm currently assigned an 1.2-DEV project, so you may get some insight at what's up in the dev branch of the framework (especially regarding sfForm) :-)

If your are looking for more complete material on the subject, please redirect yourself to the official website (where you can find the documentation and a very interesting blog) or Fabien's blog.

That said, I think it could be interesting to post all the little things I learn everyday that make development with symfony easier for the everyday php developper that you might be if you made it this far into this post ;-)

Soooo, let's begin the show, with some yaml magic. Yaml I said ? Yaml I said. For those not knowing yet, yaml is the format of choice for symfony's configurations file. So what's the point between configuration files and stylesheets ? Let's say you've got a symfony application (say, frontend), and you'd like a particular module (say, news) in this application to have its own stylesheet in addition of the defaults stylesheets you defined already. Very simple, you start by creating the adequate configuration file:

cd apps/frontend/modules/news/
mkdir config
vi config/view.yml

All you have to do know is declare the stylesheet:

all:
  stylesheets: [news]

And that's all, no helper call in the layout, your news.css will automagically be appended to the <head> of your generated html. You can also declare multiples css, or control which actions get a particular css, and even specify to which media they apply:

all:
  stylesheets: [news, news_print: { media: print }]
list:
  stylesheets: [list]

Handy, heh.

But that's not all ! If you're not that much into yaml, you can use a view helper, directly into your template:

<?php use_stylesheet('news'); ?>

Or even add it from the controller:

<?php $this->getResponse()->addStylesheet('news'); ?>

Sunday 2 December 2007

yaml, activerecord and acts_as_nested_set

I used to use this yaml_to_ar lib from christophe to load categories tree into my database, using acts_as_tree in the model that was perfect. Arrived the time when I felt the need to use acts_as_nested_set instead, for which I had to fill the lft and rgt columns. So I just rewrote the yaml_to_ar piece of code (put this in lib/yaml_to_ar.rb):

require 'yaml'

class YAML_to_AR

  def initialize(file, model)
    @data = File.open(file) { |yf| YAML::load( yf ) }
    @model = model
  end

  def process(data = @data, parent = nil)
    if data.is_a? Array
      data.each do |val|
        process(val, parent)
      end
    elsif data.is_a? Hash
      data.each do |key,val|
        parent = @model.create(:title => key)
        process(val, parent)
      end
    elsif data.is_a? String
      parent.add_child(@model.create(:title => data))
    end
  end

 end

This should handle both acts_as_tree and acts_as_nested. To ease things a bit further, I also wrote a rake task (to drop in lib/tasks/db_load_categories.rake for example):

namespace :db do
  desc "Loads categories defaults data"
  task :load_categories => :environment do
    require 'lib/yaml_to_ar'
    Category.delete_all
    categories = YAML_to_AR.new('db/categories.yml', Category)
    categories.process
  end
end

Now I just rake db:load_categories, and voila !