Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
kartik-v opened this issue Jul 25, 2023 · 2 comments

Comments

@kartik-v
Copy link
Owner

kartik-v commented Jul 25, 2023

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:

  • $key: string, defines the flag.

  • $callback: string|array, the callback function name that will return the value to be replaced. This can be a
    global function name or a callback setting in an array format as understood by PHP's call_user_func_array method. For example:

    For example:

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
Copy link
Owner Author

@urielgolab
Copy link

urielgolab commented Oct 7, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants