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

The best way to create function

 
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
paulgolovin
Smarty Rookie


Joined: 27 Jan 2016
Posts: 5

PostPosted: Tue Feb 02, 2016 2:03 pm    Post subject: The best way to create function Reply with quote

Hello everyone.

I'd like to know what is the best way to define function in Smarty 3.1.29.
For example, i have some html code:
Code:
<div class="tumbler">
    <label>
        <input type="checkbox">
        <span class="container">
            <span class="swipe"></span>
        </span>
    </label>
</div>


And I have to print this tumbler on lots of pages.
Where should I define function 'print_tumbler'?
I think it's not a good idea write html code in smarty plugins. Or not?

Help, please.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Feb 02, 2016 3:54 pm    Post subject: Reply with quote

Why not? This is exactly what Smarty plugins are intended for.
http://www.smarty.net/docs/en/plugins.functions.tpl


Last edited by AnrDaemon on Tue Feb 02, 2016 4:08 pm; edited 2 times in total
Back to top
View user's profile Send private message
paulgolovin
Smarty Rookie


Joined: 27 Jan 2016
Posts: 5

PostPosted: Tue Feb 02, 2016 4:00 pm    Post subject: Reply with quote

Like this?

Quote:
function smarty_function_print_tumbler($params) {
return <<<"EOD"
<div class="tumbler">
<label>
<input type="checkbox" value=">
<span class="container">
<span class="swipe"></span>
</span>
</label>
</div>
EOD;

}
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Feb 02, 2016 4:08 pm    Post subject: Reply with quote

Like that.
Or you can turn it into a modifier, if your intention is to completely translate a text block.

I.e.
Code:
<?php
/** Smarty base64 encode modifier plugin
*
* @version $Id: modifier.base64.php 95 2014-08-06 11:08:47Z anrdaemon $
*/

if(is_callable('base64_encode'))
{
  function smarty_modifier_base64($source, $wrap = 76)
  {
    $_result = base64_encode($source);

    return chunk_split($_result, $wrap);
  }
}
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Feb 02, 2016 4:10 pm    Post subject: Reply with quote

paulgolovin wrote:
Like this?

Code:
function smarty_function_print_tumbler($params)

Except the naming. "print_tumbler" - you will get tired of writing it very fast.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Feb 02, 2016 9:44 pm    Post subject: Reply with quote

Just put that HTML in tumbler.tpl and then {include file="tumbler.tpl"} wherever you need it. And you are correct, don't try to manage HTML inside plugins if you don't have to.
Back to top
View user's profile Send private message Visit poster's website
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Feb 03, 2016 1:42 am    Post subject: Reply with quote

mohrt wrote:
Just put that HTML in tumbler.tpl and then {include file="tumbler.tpl"} wherever you need it. And you are correct, don't try to manage HTML inside plugins if you don't have to.

I'm assuming, he want to insert some data into that HTML block.
However, if it is just a block with no variable data in it, then, indeed,
Code:
{include 'tumbler.tpl' inline}
is the most optimal solution.
Back to top
View user's profile Send private message
paulgolovin
Smarty Rookie


Joined: 27 Jan 2016
Posts: 5

PostPosted: Wed Feb 03, 2016 9:38 am    Post subject: Reply with quote

Is not a problem to include file 100 or maybe 500 times on one page?
And i use variables, but i just simplified code.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Feb 03, 2016 2:57 pm    Post subject: Reply with quote

paulgolovin wrote:
Is not a problem to include file 100 or maybe 500 times on one page?

No. Especially not, if you are using {include ... inline} for small static blocks. It will reduce the number of cached pages, and overall speed up your server.
Quote:
And i use variables, but i just simplified code.

Then, as I said, you would be best off creating template function with parameters.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Feb 03, 2016 10:29 pm    Post subject: Reply with quote

It is no more of a problem than using <?php include("..."); ?> hundreds of times in PHP. If you really do have hundreds of calls don't use the inline param, let it include the php file in the compiled template.

If it is completely static content then another option is to feed the template into a variable:

Code:
{include file="foo.tpl" assign="my_tpl"}


Or do that in PHP:

Code:
$smarty->assign('my_tpl', $smarty->fetch('foo.tpl'));


Then use {$my_tpl} everywhere you want it displayed.
Back to top
View user's profile Send private message Visit poster's website
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Feb 03, 2016 10:39 pm    Post subject: Reply with quote

mohrt wrote:
Or do that in PHP:

Code:
$smarty->assign('my_tpl', $smarty->fetch('foo.tpl'));


Then use {$my_tpl} everywhere you want it displayed.

This approach could have issues with caching.
I would not recommend manually calling Smarty::fetch() unless absolutely necessary.
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