Automated ways to build a sub-theme (6.x)

Last modified: September 21, 2009 - 16:00

Zenophile Module

Zenophile is a tiny module which allows themers to very easily create Zen subthemes without all the tedious file copying and find-and-replacing required when creating subthemes by hand. With this module, subthemes can be created in a fraction of the time just by entering information into a single-page form and clicking “Submit.” http://drupal.org/project/zenophile

UNIX Shell script

This script prompts for a human-readable name for a new subtheme name. The human readable name is placed in the theme's .info file. A machine-readable name is created by converting the human-readable name to all lowercase letters (dashes and spaces become underscores); for example, Joe Cool's "Better" Theme becomes joe_cools_better_theme. The script then copies the STARTERKIT to the new subtheme directory and renames internal references as necessary.

Script for zen-6.x-1.x

#!/bin/bash
# This script creates a new subtheme for zen following the instructions on drupal.org/node/226507
# Place this file in your themes directory, alongside the zen folder, as a file named "subtheme"
# Usage: open a shell, navigate to the themes directory and run ./subtheme
# (Run chmod 700 on this file to make it executable)
# Based on script submitted here: drupal.org/node/276120

echo "Enter a human-readable subtheme name:"
read -e INPUTNAME

NAME=`echo $INPUTNAME | tr '[:upper:]' '[:lower:]' | sed -e 's/[ -]/_/g' -e 's/[^a-z0-9_]//g'`
SEDNAME=`echo $INPUTNAME | sed -e 's/"/\\"/g'`

echo -n "Choose a page layout type [f]ixed or [l]iquid for subtheme $NAME: "
read -e LAYOUT

if [ $LAYOUT = "f" ]; then
  CSS="layout-fixed.css"
elif [ $LAYOUT = "l" ]; then
  CSS="layout-liquid.css"
else
  echo Invalid layout type.
  exit
fi

cp -r zen/STARTERKIT $NAME

#
# Change the theme name (the line 'name = ...') to the new subtheme name
#
sed 's/STARTERKIT/'$NAME'/g' $NAME/STARTERKIT.info | sed 's/^\(name *= *\).*/\1'"$SEDNAME"'/g' > $NAME/$NAME.info
  rm $NAME/STARTERKIT.info

cp zen/zen/$CSS $NAME/layout.css
cp zen/zen/print.css $NAME/print.css
cp zen/zen/html-elements.css $NAME/html-elements.css
cp zen/zen/zen.css $NAME/$NAME.css
sed 's/STARTERKIT/'$NAME'/g' zen/STARTERKIT/template.php > $NAME/template.php
sed 's/STARTERKIT/'$NAME'/g' zen/STARTERKIT/theme-settings.php > $NAME/theme-settings.php

exit 0

Script for zen-6.x-2.x

#!/bin/bash
# This script creates a new subtheme for zen-6.x-2.x-dev
# Place this file in your theme directory, alongside the zen folder, as a file name "subtheme-creator"
# Usage: open a shell, navigate to the zen directory and run ./subtheme-creator
# (Run chmod 700 on this file to make it executable)
# Based on script submitted here: drupal.org/node/276120

#~ Chain of events:
#~ * copy STARTERKIT/ from zen/ and rename to NEWNAME (lowercase/underscores)
#~ * rename STARTERKIT.info.txt to NEWNAME.info
#~ * edit NEWNAME.info name and description field
#~ * remove unneeded layout-{!chosen layout}.css file and reference in NEWNAME.info
#~ * replace all occurances of STARTERKIT in template.php & theme-settings.php with NEWNAME


echo -n "Enter a name for your theme: "
read -e NAME

HUMAN=`echo $NAME | sed -e 's/[^a-zA-Z0-9\ ]//g'`
NAME=`echo $NAME | tr [:upper:] [:lower:] | sed -e 's/[\-\ ]/_/g' -e 's/[^a-z0-9_]//g'`

if [ -d $NAME ]; then
    echo "Theme folder already exists with that name."
    echo "Do you want to remove the existing theme?"
    select OVERWRITE in Yes No
    do
        if [ $OVERWRITE = "Yes" ]; then
            rm -r $NAME /tmp/$NAME;
            break
        else
            echo "Exiting..."
            exit
        fi
    done
fi

echo "Choose a page layout"
select LAYOUT in Fixed Liquid
do
    if [ $LAYOUT = "Liquid" ]; then
        DELCSS="layout-fixed"
        CSS="layout-liquid"
        break
    else
        DELCSS="layout-liquid"
        CSS="layout-fixed"
        break
    fi
done

# Copy the STARTERKIT folder
cp -r zen/STARTERKIT $NAME

# Create .info file
sed -e 's/STARTERKIT/'$NAME'/g' -e 's/Zen Sub-theme Starter Kit/'"$HUMAN"'/g' -e 's/^\(description = \).*$/\1A Zen sub-theme/g' $NAME/STARTERKIT.info.txt > $NAME/$NAME.info
rm $NAME/STARTERKIT.info*

# Remove layout css file and references not required
rm $NAME/css/$DELCSS*
sed -e 's/'$DELCSS'/'$CSS'/g' $NAME/$NAME.info > /tmp/zen.info
mv /tmp/zen.info $NAME/$NAME.info

# Replace all occurances of STARTERKIT with theme name in template and theme-settings files
sed 's/STARTERKIT/'$NAME'/g' $NAME/template.php > /tmp/zen-template.php
mv /tmp/zen-template.php $NAME/template.php
sed 's/STARTERKIT/'$NAME'/g' $NAME/theme-settings.php > /tmp/zen-theme-settings.php
mv /tmp/zen-theme-settings.php $NAME/theme-settings.php

echo New theme folder $NAME created in `pwd`

exit 0

Update for Bash script for zen-6.x-1.x

artifice - November 10, 2009 - 14:57

The Zen distribution for 6.x-1.1 has a file named STARTERKIT.info.txt rather than STARTERKIT.info. The Bash script needs to be updated to reflect this:

31,32c31,32
< sed 's/STARTERKIT/'$NAME'/g' $NAME/STARTERKIT.info | sed 's/^\(name *= *\).*/\1'"$SEDNAME"'/g' > $NAME/$NAME.info
<   rm $NAME/STARTERKIT.info
---
> sed 's/STARTERKIT/'$NAME'/g' $NAME/STARTERKIT.info.txt | sed 's/^\(name *= *\).*/\1'"$SEDNAME"'/g' > $NAME/$NAME.info
>   rm $NAME/STARTERKIT.info.txt
41a42
>

 
 

Drupal is a registered trademark of Dries Buytaert.