kartik-v / yii2-grid

Enhanced GridView with various utilities for Yii Framework 2.0
http://demos.krajee.com/grid
Other
558 stars 302 forks source link

Gridview `replaceTags` optimized to render content only if necessary using callbacks #1072

Closed kartik-v closed 1 year ago

kartik-v commented 1 year ago

This enhancement will update the GridView::replaceTags property behavior

replaceTags array, tags to replace in the rendered layout. Enter this as an associative array of the format $key => $callback, where:

function renderTag1() { // global function
    $string = ''; // do your stuff to render;
    return $string;
};
echo GridView::widget([
    'replaceTags' => [
        '{tag1}' => 'renderTag1'
    ]
    // other gridview settings
]);

Alternatively you can return a function name from your class or object as an array format. For example:


class YourClass {
  public function renderToken2() {       // object function
     $string = ''; // do your stuff to render;
     return $string;
  }

  public static function renderToken3() { // static function
     $string = ''; // do your stuff to render;
     return $string;
  }

  public function render() {
     return GridView::widget([
         'replaceTags' => [
             '{token2}' => [$this, 'renderToken2'],
             '{token3}' => [YourClass::class, 'renderToken3']
         ]
         // other gridview settings
    ]);
  }
}
kartik-v commented 1 year ago

API Documentation is updated to reflect this

urielgolab commented 10 months ago

I'm not confident enough to make changes to file \yii2-grid\src**GridViewTrait.php at line 1644, function kartik\grid\GridView::replaceLayoutPart**, but I think it should treat callback as an array, and send it the parameters to that function.