// I need to

ModernizeUpgrade without breaking it RescueFix what someone else started Build NewTurn an idea into a product Reach More PeopleGet found, get used, include everyone

// Company

Case StudiesExpertiseAbout + CareersResourcesContact Book a call →
≡articleBuild New

Yii Framework: Customized Templates Using Themes

Clients want the same page to look different? Use Yii Framework theming to customize templates per client instead of conditionals and duplicate code.

Art+Logic Apr 8, 20132 min read

From the Art+Logic archive · written in 2013. The tools may have moved on; the thinking still holds.

Photo by Luca Bravo on Unsplash

I have been working on licensed PHP/Yii webapp that is highly customizable.  Clients can specify all sort of custom views, behaviors, and workflows.  We were running into situations where one client wanted a page to look one way, and another wanted it to look another way.  After a feeble attempt to keep customizations restricted to css changes only, we began weaving a tangled web of conditional statements, duplicate code, and duplicate templates.  We figured there must be a better way.  It turns out there was, and Yii theming was the answer.

The Problem

The app is login protected, and each user is tied to a particular client.  So once the user logs in, the particular client instance is loaded.  Each client is allowed a wide range of customizability to support their specific workflow.  The customizations really have no bounds, much to the chagrin of us developers.  Ranging from branding changes to customized help screens, each customer is given the ability to tweak and change the way the app looks and behaves at will.  So we needed to come up with a maintainable solution for managing all the view customizations, without having a bunch of duplicate code lying around.

The Solution…

In essense, we created a custom themes for each client requiring tweaks.  This might sound like a lot of cruft.  But the way Yii theming works, we only have to add view files that differ from the default views.  Yii will look in the client theme directory for a view file first.  If it’s found, it will use it.  If it’s not, it will fall back on the default view file in protected/views.  The trick is dynamically setting the Yii theme variable once the user/client has been loaded. Here’s how it worked:

Software screen capture
  1. Update the themeManager basePath to use the ‘clients’ folder.  This step isn’t really necessary, but putting the client specific view files in a folder named ‘clients’ instead of ‘themes’ seemed a bit more logical.
  2. In initialization code, lookup the client template path parameter.  Our application used a custom class that extended CWebApplication, which allowed us to put the initialization code in init().  The details of how the client is loaded isn’t important, it’s mostly about setting the Yii::app()-> theme variable.
  3. Now, create your overriden client view files at ../clients/<customer_name>/views.  If the view files exists in this directory, Yii will use it.  Otherwise, it will fall back on the ones in /protected/views/.
..
'components' = array(
    ...
    'themeManager'=> array(
        'basePath' => dirname(__FILE__).DIRECTORY_SEPARATOR.'../clients',
    )
)
MyApplication extends CWebApplication
{
    function init()
    {
        $client = getClient();
        if (isset($client))
        {
            $templatePath = $client->getVar('templatePath');
            if (isset($templatePath) && $templatePath != 'default')
            {
                $this->theme = $templatePath;
            }
        }
    }
}
keep going

More to explore in Build New.

All Build New resources →
let's talk

Got a problem like this one?

Tell us what's stuck. A real person replies within one business day.

Talk to our team →

Talk to people who know software, not a sales script.

Your first call is with Aja, our VP of Sales. He'll help make sure you're solving the right problem. Here's what you'll cover together:

  1. What you're trying to accomplish

    The business goal first, then the technology.

  2. Possible approaches

    And the tradeoffs of each, in plain language.

  3. Risks you may not have considered

    Technical and business, before they cost you.

  4. Whether AI changes the math

    What's practical, and what's just hype for your project.

  5. Sensible next steps

    Even if the right next step is not us.

book-a-call.ics

Loading the scheduler…

Open the booking page →

Some problems are easier to explain in writing. Send us a few lines about what you're working on, and we'll reply within one business day. Helpful to include:

  1. What the software does, or should do
  2. What's stuck, or what's worrying you
  3. Any deadline or budget you have in mind
note.md

Loading the form…