lrzins Posted February 5, 2013 Share Posted February 5, 2013 Hi, I'm using Yii version 1.1.13. I've generated a variable in a controller, that I need to display in the view file "_view.php". I pass the variable to "view.php" with render, and I can access it there. But I can't figure out how to access the variable from "_view.php", which uses the CDetailView widget. How is this done? Am I missing something? You would think this would be very easy, since it is such a common task. Thanks for any tips, Larry Z. Link to comment Share on other sites More sharing options...
lrzins Posted February 5, 2013 Author Share Posted February 5, 2013 For Pete's sake, I just figured it out, thanks to this post: http://www.yiiframework.com/forum/index.php/topic/28254-using-cdetailview-to-show-another-models-view/ and then this made more sense: http://www.yiiframework.com/doc/api/1.1/CDetailView#attributes-detail At least this is one way to do it I suppose. You can access data in the database directly using a relation defined in your model. In my Course model I have this relation: 'department' => array(self::BELONGS_TO, 'Department', 'department_id'), In the view.php file I add an attribute to the CDetailView widget that accesses the data: <?php $this->widget('zii.widgets.CDetailView', array( 'data'=>$model, 'attributes'=>array( array('label'=>'Department', 'name'=>'department_name', 'value'=>$model->department->department_name), 'course_title', 'department_core_course', 'elective_graduate_or_upper_division', 'units', ), )); ?> And in the _view.php file: <b><?php echo CHtml::encode($data->getAttributeLabel('department_id')); ?>:</b> <?php echo CHtml::encode(department_name); ?> <br /> But what if you make a calculation in a controller, which is not stored in the database, and want to display it in the _view.php file? Would it be available in $this->some_variable? OK I'll test... Whew! Larry Z. Link to comment Share on other sites More sharing options...
lrzins Posted February 5, 2013 Author Share Posted February 5, 2013 Oh, it turns out that the _view.php file is only used by the index.php file. Sorry if that mislead/confused anybody. Larry Z. Link to comment Share on other sites More sharing options...
Larry Posted February 6, 2013 Share Posted February 6, 2013 Glad you figured all this out and thanks for sharing what you learned. Link to comment Share on other sites More sharing options...
Recommended Posts