Creating a function within a tpl file

Author Message

Kedar Deshpande

Monday 28 December 2009 9:17:47 am

Hi, I currently have a template file which I am using to do multiple fetchs and display the elements of each fetch for the user. What I need to do is add some line of code that would be used in each of the loops I have but wanted to ask if there was a way to include the repeated code in a function (preferably inside the same tpl file) and then return the results.

thanks.

Gaetano Giunta

Monday 28 December 2009 1:36:44 pm

The correct way to do it: create a template operator. It is about the only php code there is a RAD-generator for in the admin interface, and half a bazillion template operator extensions to take as examples.

The hackish way: separate your "function" code in a separate template, and use the {include uri='design:myfunction.tpl' vari1=$v1 var2=$v2} operator as if it was a function call. To get the value back in a variable, you will have to define said variable before the inclusion of the function-template (or use appropriately variable scopes, but it might be hard)

Principal Consultant International Business
Member of the Community Project Board

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 29 2025 23:52:10
Script start
Timing: Jan 29 2025 23:52:10
Module start 'layout'
Timing: Jan 29 2025 23:52:10
Module start 'content'
Timing: Jan 29 2025 23:52:10
Module end 'content'
Timing: Jan 29 2025 23:52:10
Script end

Main resources:

Total runtime0.0114 sec
Peak memory usage2,048.0000 KB
Database Queries3

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0042 588.2344151.2109
Module start 'layout' 0.00420.0021 739.445336.6484
Module start 'content' 0.00640.0037 776.093890.0625
Module end 'content' 0.01010.0012 866.156329.9922
Script end 0.0113  896.1484 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.002017.8542140.0001
Check MTime0.00108.3992140.0001
Mysql Total
Database connection0.00054.613410.0005
Mysqli_queries0.001916.593030.0006
Looping result0.00000.104810.0000
Template Total0.00098.010.0009
Template load0.00075.790810.0007
Template processing0.00022.137010.0002
Override
Cache load0.00043.689410.0004
General
dbfile0.00021.466680.0000
String conversion0.00000.060840.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs