Yii Framework Forum: THooking component - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

THooking component Rate Topic: -----

#1 User is offline   redxiii 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 12-December 08

Posted 06 January 2009 - 05:53 AM

The Hooking allows for creating actions
and filters and hooking functions, and methods. The functions or methods will
then be run when the action or filter is called.

I borrow this idea from wordpress.


function for get or create component instance
function getComponent($id,$class=false) {
//debug($id,'getComponent #id');
$comp = Yii::app()->getComponent($id);
if (!$comp) {
if ($class) {
$comp = Yii::createComponent(array('class'=>$class) );
Yii::app()->setComponent($id,$comp);
}
}
//debug($comp,'getComponent #comp');

return $comp;
}

Attached File(s)


0

#2 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,905
  • Joined: 04-October 08
  • Location:DC, USA

Posted 06 January 2009 - 07:09 AM

Looks cool. Could you explain how to use this component?
0

#3 User is offline   redxiii 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 12-December 08

Posted 06 January 2009 - 12:57 PM

How to use it
I make a layout file like this test.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/2000/REC-xhtml1-200000126/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="language" content="en" />
<title><?php echo $this->pageTitle; ?></title>

<?php do_action('page_head'); ?>
</head>
<body>
<?php echo $content; ?>
</body>
</html>


index.php

<?php
function load_styles() {
$styles = array(
array('url' => 'themes/default/dasgboard.css','media'=>'all'),
);

$output = '';
foreach ($styles as $style) {
$output .= CHtml::cssFile($style['url'],$style['media'])."n";
}
echo $output;
debug($output,'load_styles #output');
}

function load_scripts() {
$scriptFiles = array(
'/js/dashboard.js',
);

$output = '';
foreach ($scriptFiles as $scriptFile) {
$output .= CHtml::scriptFile($scriptFile)."n";
}
echo $output;
debug($output,'load_scripts #output');
}
add_action('page_head','load_styles',9);
add_action('page_head','load_scripts');
?>
<div>
<h1>test index</h1>
</div>


functions-test.php

function _print_styles($handles=false){
do_action( '_print_styles' );
$styles = array(
array('url' => 'themes/default/style.css','media'=>'all'),
array('url' => 'themes/default/color-fresh.css','media'=>'all'),
);

$output = '';
foreach ($styles as $style) {
$output .= CHtml::cssFile($style['url'],$style['media'])."n";
}
echo $output;
debug($output,'_print_styles #output');
}

function _print_scripts($handles=false){
do_action( '_print_scripts' );
$scriptFiles = array(
'/js/common.js',
);

$output = '';
foreach ($scriptFiles as $scriptFile) {
$output .= CHtml::scriptFile($scriptFile)."n";
}
echo $output;
debug($output,'_print_scripts #output');
}


default-action.php

add_action('page_head', '_print_styles', 9);
add_action('page_head', '_print_scripts');



and i change main program a index.php to like that

....
// create a Web application instance
$application = Yii::createWebApplication($config_file);
require_once 'default-actions.php';

//- to run application.
$application->run();


Now i can add style sheet file or script into head tag section anywhere.
0

#4 User is offline   redxiii 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 12-December 08

Posted 06 January 2009 - 01:25 PM

I not sure, you're understand my idea. I make a example to using the THooking component. may be you have a better idea.

Attached File(s)


0

#5 User is offline   Vince. 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 432
  • Joined: 29-December 08
  • Location:Los Angeles,CA

Posted 06 January 2009 - 02:09 PM

Will appreciate and explanation for this.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users