View previous topic :: View next topic |
Author |
Message |
paulgolovin Smarty Rookie
Joined: 27 Jan 2016 Posts: 5
|
Posted: Tue Feb 02, 2016 2:03 pm Post subject: The best way to create function |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
|
Back to top |
|
paulgolovin Smarty Rookie
Joined: 27 Jan 2016 Posts: 5
|
Posted: Tue Feb 02, 2016 4:00 pm Post subject: |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue Feb 02, 2016 4:08 pm Post subject: |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue Feb 02, 2016 4:10 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Feb 02, 2016 9:44 pm Post subject: |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Feb 03, 2016 1:42 am Post subject: |
|
|
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 |
|
paulgolovin Smarty Rookie
Joined: 27 Jan 2016 Posts: 5
|
Posted: Wed Feb 03, 2016 9:38 am Post subject: |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Feb 03, 2016 2:57 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Feb 03, 2016 10:29 pm Post subject: |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Feb 03, 2016 10:39 pm Post subject: |
|
|
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 |
|
|