Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

How to to include "global" {functions} only ONCE?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
wxop
Smarty n00b


Joined: 10 Oct 2010
Posts: 4

PostPosted: Sat Oct 19, 2013 5:01 pm    Post subject: How to to include "global" {functions} only ONCE? Reply with quote

Hi smarty fans!

I would like to be able to include {function} that could be accessed from any templates, without creating them as plugins.

here is an example for how i've organized my templates:

site_template.tpl
Code:
<html>
   {include file='header.tpl'}
   {$app_content}
   {include file='footer.tpl'}
</html>


app_template.tpl
Code:
{capture assign=app_content}
   <div id='myapp'>
      {$page_content}
      {call name=makePrettryDiv2 input=$data}
   </div>
{/capture}
{include file='site_template.tpl'}


page_x_template
Code:
{capture assign=page_content}
   <div id='mycontent'>
      hello world
      {call name=makePrettryDiv1 input=$data}
   </div>
{/capture}
{include file='app_template.tpl'}


PHP
Code:
$smarty->assign('data','foobar');
$smarty->display('page_x_template.tpl');


Now I would like to have functions defined in a "global" template, ie:

global_functions.tpl
Code:
{function name=makePrettryDiv1 input=''}
   <div class="myPrettyDiv1">{$input}</div>
{/function}

{function name=makePrettryDiv2 input=''}
   <div class="myPrettyDiv2">{$input}</div>
{/function}

etc....


and be able to access these functions from any template, without having to include the "global_functions.tpl" each time i need one of its functions, in EACH template.

I've tried to put the include in the (global) "site_template.tpl"
{include file="global_functions.tpl"}
But obviously, it doesnt work, because it is included after the functions are called from the "children" templates.

Ideally i would have a :
Code:
$smarty->function_template_load('global_functions.tpl');

just before the smarty->display(), working exactly the way smarty->config_load() works.

I know that I could make plugins, or dynamically register plugins, but it would involve moving all theses pure "View" functions (generating html, not logic) , outside of the templates, and thus moving parts of the "V" of the MVC structure, inside the "C" part of the code. This would just ruins what Smarty is usefull for: separating the View from the Controller!

Is there any Tip that could resolve this issue? Question

How do you implement global functions in your projects ,without mixing them in your code, or whithout making multiple uneeded includes (certainly slow) in all your templates?

I Hope I've missed something, or there is may be a way to circumvent the issue..
Idea Any suggestion, ideas will be GREATLY appreciated!


Thanks for your time,


wxop
an old smarty lover! Wink
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Sun Oct 20, 2013 9:42 am    Post subject: Reply with quote

It would work if you put your {include file="global_functions.tpl"} in your template which is called at display().

But loading template function completely independent provides much more flexibility.

I will put it on the TODO list for 3.2
Back to top
View user's profile Send private message
wxop
Smarty n00b


Joined: 10 Oct 2010
Posts: 4

PostPosted: Sun Oct 20, 2013 11:00 am    Post subject: Reply with quote

Hi U.Tews, thank you for replying

U.Tews wrote:
It would work if you put your {include file="global_functions.tpl"} in your template which is called at display().

Sure, I know Very Happy

U.Tews wrote:

But loading template function completely independent provides much more flexibility.


Absolutely, ie in this case! ... I guess your answer implies that there is no better way to do what I want currently.

U.Tews wrote:
I will put it on the TODO list for 3.2


Absolutely TERRIFIC! Surprised Smile Any ETA planned for 3.2? Rolling Eyes

BTW I saw somewere that you intend to move from SVN(?) to Github, which would really ease external contributions. When you'll be on Github, be sure that I'll one of your proud "pull-requester"... Wink
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP