Magic Data Enabling for Themes

Theme develoeprs can take the concept of Magic Data templates one step further and enable Magic Data across an entire theme. 

The concept is very similar to enabling a block template. As theme areas are output, the output is passed through the same Magic Data Symbols Helper. To simplify the process, none of that actually needs to appear in the theme page types. All the theme page types need to do is load and use Magic Data alternate models for rendering areas and global areas and the models do the rest.

For example, here is the code for the default page type of Magic Yogurt, a Magic Data enabled adaptation of the core Greek Yogurt theme.

Loader::model('magic_area', 'jl_magic_yogurt');
Loader::model('magic_global_area', 'jl_magic_yogurt');

$this->inc('elements/header.php'); ?>

	<div class="clear"></div>

	<div id="main-content-container" class="grid_16">
		<div id="main-content-inner">

			<?php
			$a = new MagicArea('Main');
			$a->display($c);
			?>

		</div>

	</div>

	<div id="right-sidebar-container" class="grid_8">

		<div id="right-sidebar-inner">

			<?php
			$a = new MagicArea('Sidebar');
			$a->display($c);
			?>

		</div>

	</div>

	<!-- end sidebar -->

<?php  $this->inc('elements/footer.php'); ?>

Where a theme would have used the core Area class, now it uses the MagicArea class. Where it would have used the GlobalArea class, now it uses the MagicGlobalArea class. 

Internally, these two classes are wrappers about the corresponding core classes. To complete your theme package, the Magic Data Symbols Helper and the two models classes need to be added to your theme package.

Loader::model('area');
class MagicArea extends Area {

	public function display(&$c, $alternateBlockArray = null){
		
		// Don't do magic in edit mode or if magic data is not available
		$mdsh = Loader::helper('magic_data_symbols', 'jl_magic_yogurt');
		$c = Page::getCurrentPage();
		if( 	!$mdsh->confirm_installed() 
					|| 
				$c->isEditMode() 		
			){
			parent::display($c, $alternateBlockArray);
		
		// Apply magic by buffering the entire area.
		} else {
			ob_start();
			parent::display($c, $alternateBlockArray);
			$html = ob_get_contents();
			ob_end_clean();
			echo $mdsh -> fill($html);
		}
	}
}
Loader::model('global_area');
class MagicGlobalArea extends GlobalArea {

	public function display(){

		// Don't do magic in edit mode or if magic data is not available
		$mdsh = Loader::helper('magic_data_symbols', 'jl_magic_yogurt');
		$c = Page::getCurrentPage();
		if(	!$mdsh->confirm_installed() 
					|| 
				$c->isEditMode()
			){
			parent::display();

		// Apply magic by buffering the entire area.
		} else {
			ob_start();
			parent::display();
			$html = ob_get_contents();
			ob_end_clean();
			echo $mdsh -> fill($html);
		}
	}
}

If you are developing a packaged theme, these file should be located in:

  • /packages/your_theme_package_name/models/magic_area.php
  • /packages/your_theme_package_name/models/magic_global area.php

Your theme is now Magic Data enabled. On systems where Magic Data is not installed, the theme will behave exactly as before. Where Magic Data is installed, any tokens found within the enabled areas will be processed by Magic Data for display.

The code on this page is free to use for any developer wanting to integrate with Magic Data. To reduce the amount of typing and editing involved, developers are welcome to download the Magic Yogurt theme and copy the models and helper from that theme.

Last updated: over a year ago