Forums / Developer / Develop a template function ? (not fetch function)

Develop a template function ? (not fetch function)

Author Message

Jérôme Vieilledent

Saturday 29 March 2008 7:47:16 am

Hello,

I'd like to know if it was possible to develop a template function, such as attribute_view_gui.
Indeed, I'm thinking about sharing an extension to include css/js files depending on which node / content class is viewed, and I would like to propose it as a simple template function, so we have the minimum template code to write.

Example of what I would like to do in a the pagelayout :

<html>
<head>
    <title>{$site.title}</title>
    {* My css/js files go here *}
    {my_template_display_function}

...
</head>
</html>

Do you know if it is possible to do that ?
Thanks :-)

Sébastien Antoniotti

Saturday 29 March 2008 11:29:19 am

Hi Jérome !

Why not simply develop a template operator ?

You can call it like this :

 

     {my_template_operator()}
 

or if any parameter is needed

 

     {my_template_operator($param1, $param2, ...)}
 

Would it be ok like that ?

eZ Publish Freelance
web : http://www.webaxis.fr

Hans Melis

Saturday 29 March 2008 2:23:14 pm

> Do you know if it is possible to do that ?

Short answer: Yes, it's possible :-)

Hans
http://blog.hansmelis.be

Heath

Saturday 29 March 2008 10:49:04 pm

Hello Jérôme Vieilledent,

You say template function ...

They say template operator ...

Essentially they are the same thing by any name:)

http://ezpedia.org/wiki/en/ez/template_operators

Cheers,
Heath

Hint: In the eZ Components Template component template operators will be called template functions (or so I've heard and read before). So in the future your name for this construct will be accurate. Until then create yourself a template operator ... it's fun!

Brookins Consulting | http://brookinsconsulting.com/
Certified | http://auth.ez.no/certification/verify/380350
Solutions | http://projects.ez.no/users/community/brookins_consulting
eZpedia community documentation project | http://ezpedia.org

Jérôme Vieilledent

Monday 31 March 2008 2:50:44 am

Thanks for your answers... :-)

Sébastien : An operator can only return values to put in a variable, can't it ? What I would like to do is to directly display the result (without an dirty echo in the operator class of course)

Hans : Thanks :-) But can you tell me how please ? ^^

Heath : Thanks for the info ;-)

Sébastien Antoniotti

Monday 31 March 2008 4:02:08 am

If your operator returns a piece of HTML and if you call it like this...

 

    {operator()}
 

... the piece of HTML will be included where the operator is called.

I think it's ok for you ;)

eZ Publish Freelance
web : http://www.webaxis.fr

Jérôme Vieilledent

Monday 31 March 2008 5:00:57 am

OK Thanks, it's clearer now :-)

Tobias S.

Sunday 02 August 2009 3:37:44 am

I'm about to develop my first extension and was already able to implement a template fetch function prototype.

My search about template operators, template functions and template fetch functions just lead me to this article. As it's the only resource describing differences of the above (even the docs don't tell the difference), I just add my questions here.

What is the difference between template fetch functions and template operators? How does one know what's the best choice?

Heath

Sunday 02 August 2009 11:06:06 am

Hello,

I hope this helps. http://ezpedia.org/en/ez/custom_fetch

These days I prefer to create template functions instead of template operators as more flexible and simpler to create and use.

Cheers,
Heat

Brookins Consulting | http://brookinsconsulting.com/
Certified | http://auth.ez.no/certification/verify/380350
Solutions | http://projects.ez.no/users/community/brookins_consulting
eZpedia community documentation project | http://ezpedia.org

Gaetano Giunta

Sunday 02 August 2009 1:44:22 pm

There is a logical difference first of all:
- template operator: a 'decorator' of the input it receives, or a simple operation
- fetch function: accesses info from db or external resource (eg. via webservice). It needs to be tied to a module, too
- plain template function: more complex and powerful operations relating usually to the templating system rather than the content system

As for the differences in coding:
- there is a wizard that allows to create easily template operators, there is none for functions/fetch functions
- for fetch functions otoh the API is simpler, as you can register any php function you like. You will need to create a function_definition.php file in your module dir; take existing function_definition.php files as an example
- template functions are defined via the $eZTemplateFunctionArray variable in the same eztemplateautoload.php file where template operators are defined

Principal Consultant International Business
Member of the Community Project Board

/dev/null

Monday 03 August 2009 2:01:57 am

Pfft. Those wizards you mention are rubbish bit rott not to be relied upon.

They do -not- produce valid template operator php source code -nor- do they output even flexible or reusable template operator php source code (re: design pattern differences decipherable)

Fetch functions are the most practical to use overall for most of my kind of needs (based on your descriptions).

<i>/dev/null</i>

eZpedia community documentation project: http://ezpedia.org