Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Advertisement

Bab 7. Built-in Functions

Smarty comes with several built-in functions. These built-in functions are the integral part of the smarty template engine. You cannot create your own custom functions with the same name; and you should not need to modify the built-in functions.

A few of these functions have an assign attribute which collects the result the function to a named template variable instead of being output; much like the {assign} function.

{capture}

{capture} dipakai untuk mengumpulkan output template antara tag ke dalam variable daripada menampilkannya. Setiap konten antara {capture name='foo'} dan {/capture} yang dikumpulkan ke dalam variabel ditetapkan dalam atribut name.

Konten yang ditangkap dapat digunakan dalam template dari variabel $smarty.capture.foo di mana "foo" adalah nilai yang dikirimkan dalam atribut name. Jika anda tidak menyertakan atribut name, maka "default" akan dipakai sebagai nama misalnya $smarty.capture.default.

{capture}'s dapat diulang-ulang.

Attribute NameTypeRequiredDefaultDescription
namestringnodefaultThe name of the captured block
assignstringNon/aThe variable name where to assign the captured output to

Perhatian: Harap berhati-hati ketika menangkap output {insert}. Jika anda menghidupkan $caching dan anda mempunyai perintah {insert} yang anda harapkan untuk dijalankan di dalam konten yang di-cache, jangan menangkap konten ini.

Teladan 7-1. {capture} dengan atribut nama

{* kita tidak ingin mencetak tag div kecuali konten yang ditampilkan *}
{capture name=banner}
  {include file='get_banner.tpl'}
{/capture}

{if $smarty.capture.banner ne ''}
<div id="banner">{$smarty.capture.banner}</div>
{/if}

Teladan 7-2. {capture} ke dalam variabel template

Contoh ini juga memperlihatkan fungsi {popup}

{capture name=some_content assign=popText}
Server adalah {$smarty.server.SERVER_NAME|upper} pada {$smarty.server.SERVER_ADDR}<br>
IP anda adalah {$smarty.server.REMOTE_ADDR}.
{/capture}
<a href="#" {popup caption='Info Server' text=$popText}>bantuan</a>

Lihat juga $smarty.capture, {eval}, {fetch}, fetch() dan {assign}.