Yii Framework Forum: Yii - On what circumstances should we use clips? - Yii Framework Forum

Jump to content

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

Yii - On what circumstances should we use clips? Trying to understand where should we use clips. Rate Topic: -----

#1 User is offline   mem 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 16-December 11

Posted 17 May 2012 - 10:35 AM

Hello all,

Partials will be a view with no layout attached, for small non complex logic involved.

Layouts will be for common elements, header footer, column1, 3columns...

Widgets will be for elements that have a complex logic behind like calendars, galleries, and navigation menus (like cmenu widget);

Clips

Quote

A clip is a piece of rendering result that can be inserted on different places.


This was all that I can read on Yii guide about it. :(

Calendar and Galleries sure are

Quote

rendering results that can be inserted on different places


. Hence, the trouble:

On what circumstances should we use clips ?



Thanks in advance.
0

#2 User is offline   yiwi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 20-January 12

Posted 17 May 2012 - 12:10 PM

Haha, up to now I haven't heard of clips at all, but why don't you try the forum search?
I found an application example here: http://www.yiiframew...on-using-clips/
0

#3 User is offline   mem 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 16-December 11

Posted 20 May 2012 - 01:52 PM

View Postyiwi, on 17 May 2012 - 12:10 PM, said:

Haha, up to now I haven't heard of clips at all, but why don't you try the forum search?
I found an application example here: http://www.yiiframew...on-using-clips/


I see nothing there. No example of using clips at all. And, above all, any explanation about on what circumstances should we use or not clips.

:(
0

#4 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 20 May 2012 - 02:10 PM

Imagine you have clientside-sortable portlets. With clips you can first record all the portlets in a default order. Then you can display them based on the order defined by the user. Simple example:


// record each portlet here in any order

...

$_COOKIE['portlet-order'] = 'login|poll|something-else'; // this order was set by the client

$portlets = explode('|', $_COOKIE['portlet-order']);

foreach (portlets as $portlet)
{
   echo $this->clips[$portlet];
}


2

#5 User is offline   mem 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 16-December 11

Posted 20 May 2012 - 02:22 PM

Thanks.

Still not getting. Sorry. English is not my main language, and that could play a role here. As a plus, I don't consider myself a great programmer so, that's also an issue. All this to say: "Please have patience"

So:

View PostY!!, on 20 May 2012 - 02:10 PM, said:

Imagine you have clientside-sortable portlets. With clips you can first record all the portlets in a default order. Then you can display them based on the order defined by the user. Simple example:


// record each portlet here in any order

...

$_COOKIE['portlet-order'] = 'login|poll|something-else'; // this order was set by the client

$portlets = explode('|', $_COOKIE['portlet-order']);

foreach (portlets as $portlet)
{
   echo $this->clips[$portlet];
}





So, portlet can be clips? So, for example, the "Poll" would be a clip ?
But the pull has a lot of logic involved. Shouldn't that be a Widget ?
Or a clip could be a widget ?

:mellow:
0

#6 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 20 May 2012 - 02:30 PM

$this->beginClip('login');
   $this->widget('LoginWidget');
$this->endClip();

$this->beginClip('poll');
   $this->widget('PollWidget');
$this->endClip();

$portlets = explode('|', $_COOKIE['portlet-order']);

foreach (portlets as $portlet)
{
   echo $this->clips[$portlet];
}


So with clips you can record different fragments of a page and then display them dynamically.
2

#7 User is offline   fsb 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 123
  • Joined: 09-January 11

Posted 20 May 2012 - 03:57 PM

View PostY!!, on 20 May 2012 - 02:30 PM, said:

$this->beginClip('login');
   $this->widget('LoginWidget');
$this->endClip();

$this->beginClip('poll');
   $this->widget('PollWidget');
$this->endClip();

$portlets = explode('|', $_COOKIE['portlet-order']);

foreach (portlets as $portlet)
{
   echo $this->clips[$portlet];
}


So with clips you can record different fragments of a page and then display them dynamically.


I'm not sure I get it yet. The above could be accomplished more easily using the $captureOutput argument of CController::widget.

A key difference it seems is that you can record arbitrary output between the beginClip and endClip.

$this->beginClip('login');
   $this->widget('LoginWidget');
   echo 'Some string';
   $this->renderPartial('aView');
   echo 'etc...';
$this->endClip();

and you can capture any number of them.

I should have phrased this as a question. I've never used clips.
Join the happiest place on Freenode: #yii
We've got a MrFisk the bot, hilarious banter and all kinds of other groovy stuff going on.
Comparison of Internet Relay Chat clients
Web IRC client
0

#8 User is offline   JamesBarnsley 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 444
  • Joined: 18-November 10
  • Location:UK

Posted 20 May 2012 - 04:50 PM

Plus cant we just do that with partials anyway?
0

#9 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 20 May 2012 - 05:10 PM

Yeah, but you can run clips in any controller - anywhere.
I use it from time to time.
Instead of passing arrays to sidebars and other elements, it's much simpler to use a clip or two. And it's cleaner as well.
"Less noise - more signal"
1

#10 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 20 May 2012 - 05:14 PM

*
POPULAR

For example, I have this in components/controller.php:
class Controller extends CController {

	public $sidebar_content = '';
	public $users_online = null;
	public $visitorTableName = '{{visitor}}';

	protected function beforeAction($action) {
    	if (isset(Yii::app()->user->id)) {
        	$user_id = Yii::app()->user->id;

        	//TODO: Don't do this every time the app runs??

        	$sql = "SELECT user_id FROM {$this->visitorTableName} WHERE user_id=:user_id";
        	if (Yii::app()->db->createCommand($sql)->bindValue(':user_id', $user_id)->queryScalar() === false)
            	$sql = "INSERT INTO {$this->visitorTableName} (user_id, last_activity) VALUES (:user_id, :last_activity)";
        	else
            	$sql = "UPDATE {$this->visitorTableName} SET last_activity=:last_activity WHERE user_id=:user_id";
        	Yii::app()->db->createCommand($sql)->bindValue(':user_id', $user_id)->bindValue(':last_activity', date('Y-m-d H:i:s'))->execute();
    	}

    	$users = Yii::app()->db->createCommand()
            	->select('u.id, username, TIMESTAMPDIFF(MINUTE, last_activity, UTC_TIMESTAMP())')
            	->from('{{usergroups_user}} u')
            	->join("{$this->visitorTableName} v", 'u.id=v.user_id')
            	->where('TIMESTAMPDIFF(MINUTE, last_activity, UTC_TIMESTAMP()) < 5')
            	->queryAll();

    	$this->users_online = $users;

    	$this->beginWidget('system.web.widgets.CClipWidget', array('id' => 'users_online'));
    	if (isset($this->users_online)) {
        	foreach ($this->users_online as $user)
            	if (isset($user)) {
                	echo '<b>' . ucfirst($user['username']) . '</b> <small>(';
                	echo 'Idle for ' . $user['TIMESTAMPDIFF(MINUTE, last_activity, UTC_TIMESTAMP())'] . ' minutes)</small><br/>';
            	}
    	} else {
        	echo 'none';
    	}
    	$this->endWidget();
    	$this->beginWidget('system.web.widgets.CClipWidget', array('id' => 'site_statistics'));
        	echo CHtml::openTag('p');
            	echo 'online: ' . Yii::app()->usercounter->getOnline() . '<br />';
            	echo 'today: ' . Yii::app()->usercounter->getToday() . '<br />';
            	echo 'yesterday: ' . Yii::app()->usercounter->getYesterday() . '<br />';
            	echo 'total: ' . Yii::app()->usercounter->getTotal() . '<br />';
            	echo 'maximum: ' . Yii::app()->usercounter->getMaximal() . '<br />';
            	echo 'date for maximum: ' . date("Y-m-d H:i:s", Yii::app()->usercounter->getMaximalTime()) . '<br />';
        	echo CHtml::closeTag('p');
    	$this->endWidget();

    	return true;
	}

Maybe not a prime example of good coding practices, but at least it makes it possible to use those two clips anywhere. ;)
Without cluttering up controllers/views.
"Less noise - more signal"
5

#11 User is offline   yiqing95 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 602
  • Joined: 27-December 10
  • Location:china

Posted 22 May 2012 - 05:45 AM

*
POPULAR

you can also use clips to pass content to layouts from view file ;

clulumn2 :

      if(isset($this->clips['someSpecMenu'])){

        echo $this->clips['someSpecMenu'];
      }



in your action or view file:


   $this->beginClip('someSpecMenu');
   // echo  any content here ;
$this->endClip();



thus different controller can use a same layout file (column2) but in some area it can output different contents (menus , widgets , ad .. and so no ) :D
7

#12 User is offline   Haensel 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 444
  • Joined: 14-January 11
  • Location:Vienna (Austria)

Posted 22 May 2012 - 08:29 AM

@jacmoe, yiqing95, Y!!: Thanks for the info. I have to admit I never got the use of clips but they actually seem more flexible than partials or public controller attributes.

EDIT: Just tested it, so much better than the lame use of public properties :)
0

#13 User is offline   mem 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 16-December 11

Posted 22 May 2012 - 09:57 AM

Sure the examples will help me out to do similar things when I found myself on the same or near circumstances. I still believe it would be helpful if we have, on a manual, some theoretical references about this, well organized that we can read and truly comprehend.

Thank you all for all your comments and examples provided that have helped.


Cheers,
mem
0

#14 User is offline   Don Khan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 30-August 13

Posted 16 April 2014 - 10:59 AM

I've just gotten past a bit of the learning curve with clips, so I thought I'd share my current solution.

Basically, I wanted a re-useable CMenu sidebar widget for various departments of my site.

My controller looks like this (I'm using actions too):

class ExampleController extends Controller
{
    ...
    public function actions()
    {
        return array(...
	    'stories' => array(
	        'class' => 'application.controllers.example.StoriesAction',
	    ),
	);
    }
}


Then, my StoriesAction (controllers/example/StoriesAction.php) looks like this.
class StoriesAction extends CAction
{
    public function run()
    {
        ...
	$this->controller->renderPartial("_sidebar");
	$this->controller->layout = 'sidebarleft';
	$this->controller->render("stories");
    }
}


Then, in views/example/_sidebar.php:

$this->beginClip('sidebar');

$this->widget('zii.widgets.CMenu', array(
	'activeCssClass'=>'active',
	'itemCssClass' => 'item',
        ...
));

$this->endClip();


The sidebarleft layout contains, among other things:

<?php echo $this->clips['sidebar']; ?>
<?php echo $content; ?>



This way, each controller can have its own sidebar, written as a single script, which is rendered into a clip and used in a global layout. Niiice.
0

#15 User is offline   jkofsky 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 536
  • Joined: 17-May 10
  • Location:Pensacola, Florida

Posted 16 April 2014 - 01:02 PM

View Postmem, on 17 May 2012 - 10:35 AM, said:

Hello all,

Partials will be a view with no layout attached, for small non complex logic involved.

Layouts will be for common elements, header footer, column1, 3columns...

Widgets will be for elements that have a complex logic behind like calendars, galleries, and navigation menus (like cmenu widget);

Clips



This was all that I can read on Yii guide about it. :(

Calendar and Galleries sure are

. Hence, the trouble:

On what circumstances should we use clips ?



Thanks in advance.

One of the primary uses of clips, if my research is right, is server optimization. If you have a db query that takes a while to complete, you could save the result in a clip. Then when you run the query the first time it takes how eve long to complete, but for the next request the server just sends the clip. I believe there is a way, either programmer if() test or clip expire thing, to get fresh clip if underlying data changes.

Just my 2 cents 8)
Do, or do not. There is no 'try.' Jedi Master Yoda
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