This is a migrated thread and some comments may be shown as answers.

PHP function in column template

4 Answers 56 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Andrej
Top achievements
Rank 1
Andrej asked on 17 Sep 2019, 07:24 AM
Hello,

I am using Telerik UI for PHP Grid component. And I want to use PHP function in column template, for ex.: 

#if (kendo.toString(kendo.parseDate(data.NearestTaskDate), "yyyy-MM-dd") === date(\'Y-m-d\')) {#
<div class="feed image">
<i class="#:NearestTaskTypeIcon# icon bg-yellow"></i>
</div>

where date is PHP function. However, browser console message says that date is not defined. How can fix it? Thank you.

4 Answers, 1 is accepted

Sort by
0
Tsvetomir
Telerik team
answered on 18 Sep 2019, 12:44 PM
Hi Andrej,

Generally, the functions within the PHP template have to be set up with the "#=#" syntax. So invoking a function would be something similar to:

#= someFunction(args) #

Give this suggestion a try and let me know how it works out for you.


Best regards,
Tsvetomir
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Andrej
Top achievements
Rank 1
answered on 18 Sep 2019, 01:43 PM

I have tried, but approach

#if (kendo.toString(kendo.parseDate(data.NearestTaskDate), "yyyy-MM-dd") === #=date(\'Y-m-d\')#) {#

doesn't work.

0
Tsvetomir
Telerik team
answered on 19 Sep 2019, 11:58 AM

Hi Andrej,

Generally, the logic executed in PHP is on server-side. While the templates of the Kendo UI are evaluated on the client-side. Therefore, the date() function has to be in a script block, placed after the <?php . . . ?> block. Here are the relevant code snippets:

$contactName->field('ContactName')
            ->template("#=foo()#")
            ->title('Contact Name')
            ->width(240);

And the template:

<script>
     function foo()
     {
         return "Lorem Ipsum";
     }
</script>

I am attaching the whole file to my response, for your reference.

Best regards,
Tsvetomir
Progress Telerik

Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Vishal
Top achievements
Rank 1
Iron
answered on 27 Jul 2021, 08:49 AM

Hello Friends, This is the right coding id you're create PHP function in column template. you can try this. 

 

<?php /** * Simple Templating function * * @param $file - Path to the PHP file that acts as a template. * @param $args - Associative array of variables to pass to the template file. * @return string - Output of the template file. Likely HTML. */ function template( $file, $args ){ // ensure the file exists if ( !file_exists( $file ) ) { return ''; } // Make values in the associative array easier to access by extracting them if ( is_array( $args ) ){ extract( $args ); } // buffer the output (including the file is "output") ob_start(); include $file; return ob_get_clean(); }

Georgi Denchev
Telerik team
commented on 29 Jul 2021, 07:08 AM

Hello, Vishal,

Thank you for sharing your solution with the rest of the community!

Tags
Grid
Asked by
Andrej
Top achievements
Rank 1
Answers by
Tsvetomir
Telerik team
Andrej
Top achievements
Rank 1
Vishal
Top achievements
Rank 1
Iron
Share this question
or