How to include a file from a different theme than current?

How to include a file from a different theme than current?

If the view isn’t in themes/your_theme/views, then Yii will look in protected/views.

I need to include a /css/ from the /admin/ theme, but the current theme is ‘city’.

I’m not sure why you wouldn’t just create a new theme with the copied css file included, but you could try something like:


<link rel="stylesheet" href="<?php echo Yii::app()->theme->baseUrl; ?>/../admin/c

ss/style.css" type="text/css" media="screen" />

Note the “…” to move back in the directory tree. I haven’t tested this, but it should work. You could also use Yii::app()->basePath to build a path to your file.

Well that’s a workaround, but I would like to be Yiish. Maybe my other theme is served by CDN and has a total different path.

I am looking at something like

CTheme::instance(‘admin’)->baseUrl;

but could locate the right syntax.

CThemeManager::getTheme($name)->baseUrl might be what you’re looking for. I haven’t tried it, so buyer beware. Good luck.

That really sounds wrong on all levels…

Put the common css in app_root/css - problem solved. ;)

What are you trying to accomplish?

Why do you have a theme called ‘admin’ in the first place?

I have an existing module that uses ‘admin’ theme. And I am creating another set of administration panel, that will embed inside the existing admin panel.

So when I do the layout for panel I create, I must include some CSS files from the other existing panel.

This is workaround until all the admin functionalities are redone in the new panel.

That’s not a good approach IMO.

The ‘Rights’ extension module uses this approach:

It has an ‘assets’ directory and a ‘views’ directory (with a layout).

It then publishes the assets directory which contains css and images and other assets.

And then uses the path to the published assets in the view.

Everything is neatly self-contained.

May I suggest that you rewrite that module to be self-contained?

Rights has this code in the module script:




	/**

	* Registers the necessary scripts.

	*/

	public function registerScripts()

	{

		// Get the url to the module assets

		$assetsUrl = $this->getAssetsUrl();


		// Register the necessary scripts

		$cs = Yii::app()->getClientScript();

		$cs->registerCoreScript('jquery');

		$cs->registerCoreScript('jquery.ui');

		$cs->registerScriptFile($assetsUrl.'/js/rights.js');

		$cs->registerCssFile($assetsUrl.'/css/core.css');


		// Make sure we want to register a style sheet.

		if( $this->cssFile!==false )

		{

			// Default style sheet is used unless one is provided.

			if( $this->cssFile===null )

				$this->cssFile = $assetsUrl.'/css/default.css';

			else

				$this->cssFile = Yii::app()->request->baseUrl.$this->cssFile;


			// Register the style sheet

			$cs->registerCssFile($this->cssFile);

		}

	}


	/**

	* Publishes the module assets path.

	* @return string the base URL that contains all published asset files of Rights.

	*/

	public function getAssetsUrl()

	{

		if( $this->_assetsUrl===null )

		{

			$assetsPath = Yii::getPathOfAlias('rights.assets');


			// We need to republish the assets if debug mode is enabled.

			if( $this->debug===true )

				$this->_assetsUrl = Yii::app()->getAssetManager()->publish($assetsPath, false, -1, true);

			else

				$this->_assetsUrl = Yii::app()->getAssetManager()->publish($assetsPath);

		}


		return $this->_assetsUrl;

	}