Lewati ke konten

Variabel template

XOOPS secara otomatis menyediakan banyak variabel ke template Smarty. Referensi ini mendokumentasikan variabel yang tersedia untuk pengembangan template theme dan module.

  • Smarty-Dasar - Dasar-dasar Smarty di XOOPS
  • Pengembangan theme - Membuat theme XOOPS
  • Smarty-4-Migrasi - Peningkatan dari Smarty 3 ke 4

Variabel-variabel ini tersedia di template theme (theme.tpl):

VariabelDeskripsiContoh
$xoops_sitenameNama situs dari preferensi"My XOOPS Site"
$xoops_pagetitleJudul halaman saat ini"Welcome"
$xoops_sloganSlogan situs"Just Use It!"
$xoops_urlXOOPS URL Penuh"https://example.com"
$xoops_langcodeKode bahasa"en"
$xoops_charsetKumpulan karakter"UTF-8"
VariabelDeskripsi
$xoops_meta_keywordsKata kunci meta
$xoops_meta_descriptionDeskripsi meta
$xoops_meta_robotsTag meta robot
$xoops_meta_ratingPeringkat konten
$xoops_meta_authorTag meta penulis
$xoops_meta_copyrightPemberitahuan hak cipta
VariabelDeskripsi
$xoops_themeNama theme saat ini
$xoops_imageurlDirektori gambar theme URL
$xoops_themecsstheme utama CSS file URL
$xoops_icons32_urlIkon 32x32 URL
$xoops_icons16_urlIkon 16x16 URL
VariabelDeskripsi
$xoops_contentsKonten halaman utama
$xoops_module_headerKonten kepala khusus module
$xoops_footerKonten catatan kaki
$xoops_jsJavaScript untuk memasukkan
VariabelDeskripsi
$xoops_mainmenuMenu navigasi utama
$xoops_usermenuMenu pengguna
VariabelDeskripsi
$xoops_lblocksArray block kiri
$xoops_rblocksArray block kanan
$xoops_cblocksArray block tengah
$xoops_showlblockTampilkan block kiri (boolean)
$xoops_showrblockTampilkan block kanan (boolean)
$xoops_showcblockTampilkan block tengah (boolean)

Saat pengguna masuk:

VariabelDeskripsi
$xoops_isuserPengguna masuk (boolean)
$xoops_isadminPengguna adalah admin (boolean)
$xoops_useridID Pengguna
$xoops_unameNama pengguna
$xoops_isownerPengguna memiliki konten saat ini (boolean)
<{if $xoops_isuser}>
<p>Welcome, <{$xoops_uname}>!</p>
<p>Your email: <{$xoopsUser->getVar('email')}>}</p>
<p>Joined: <{$xoopsUser->getVar('user_regdate')|date_format:"%Y-%m-%d"}>}</p>
<{else}>
<p>Welcome, Guest!</p>
<{/if}>

Dalam template module:

VariabelDeskripsi
$xoops_dirnameNama direktori module
$xoops_modulenameNama tampilan module
$mod_urlmodule URL (bila ditugaskan)
// In PHP
$helper = \XoopsModules\MyModule\Helper::getInstance();
$GLOBALS['xoopsTpl']->assign('mod_url', $helper->url());
$GLOBALS['xoopsTpl']->assign('mod_name', $helper->getModule()->getVar('name'));
{* In template *}
<a href="<{$mod_url}>">Back to <{$mod_name}></a>

Setiap block di $xoops_lblocks, $xoops_rblocks, dan $xoops_cblocks memiliki:

PropertiDeskripsi
$block.idBlokir ID
$block.titleBlokir judul
$block.contentBlokir konten HTML
$block.templateBlokir nama template
$block.moduleNama module
$block.weightBlokir weight/order
<{foreach item=block from=$xoops_lblocks}>
<div class="block block-<{$block.module}>">
<{if $block.title}>
<h3 class="block-title"><{$block.title}></h3>
<{/if}>
<div class="block-content">
<{$block.content}>
</div>
</div>
<{/foreach}>

Saat menggunakan kelas XoopsForm:

// PHP
$form = new XoopsThemeForm('Edit Item', 'edit_form', 'save.php');
$form->addElement(new XoopsFormText('Title', 'title', 50, 255, $title));
$GLOBALS['xoopsTpl']->assign('form', $form->render());
{* Template *}
<div class="form-container">
<{$form}>
</div>
// PHP
include_once XOOPS_ROOT_PATH . '/class/pagenav.php';
$pagenav = new XoopsPageNav($total, $limit, $start, 'start');
$GLOBALS['xoopsTpl']->assign('page_nav', $pagenav->renderNav());
{* Template *}
<{if $page_nav}>
<div class="pagination">
<{$page_nav}>
</div>
<{/if}>
$GLOBALS['xoopsTpl']->assign('my_title', 'Custom Title');
$GLOBALS['xoopsTpl']->assign('item_count', 42);
$GLOBALS['xoopsTpl']->assign('is_featured', true);
<h1><{$my_title}></h1>
<p><{$item_count}> items found</p>
<{if $is_featured}>Featured!<{/if}>
$items = [
['id' => 1, 'name' => 'Item One', 'price' => 10.99],
['id' => 2, 'name' => 'Item Two', 'price' => 20.50],
];
$GLOBALS['xoopsTpl']->assign('items', $items);
<ul>
<{foreach $items as $item}>
<li>
<{$item.name}> - $<{$item.price|string_format:"%.2f"}>
</li>
<{/foreach}>
</ul>
$item = $itemHandler->get($itemId);
$GLOBALS['xoopsTpl']->assign('item', $item->toArray());
// Or for XoopsObject
$GLOBALS['xoopsTpl']->assign('item_obj', $item);
{* Array access *}
<h2><{$item.title}></h2>
<p><{$item.content}></p>
{* Object method access *}
<h2><{$item_obj->getVar('title')}></h2>
$category = [
'id' => 1,
'name' => 'Technology',
'items' => [
['id' => 1, 'title' => 'Article 1'],
['id' => 2, 'title' => 'Article 2'],
]
];
$GLOBALS['xoopsTpl']->assign('category', $category);
<h2><{$category.name}></h2>
<ul>
<{foreach $category.items as $item}>
<li><{$item.title}></li>
<{/foreach}>
</ul>

Stempel waktu saat ini:

<p>Current year: <{$smarty.now|date_format:"%Y"}></p>
<p>Current date: <{$smarty.now|date_format:"%Y-%m-%d"}></p>
<p>Current time: <{$smarty.now|date_format:"%H:%M:%S"}></p>

Akses konstanta PHP:

<p>XOOPS URL: <{$smarty.const.XOOPS_URL}></p>
<p>Root Path: <{$smarty.const.XOOPS_ROOT_PATH}></p>
<p>Upload Path: <{$smarty.const.XOOPS_UPLOAD_PATH}></p>

$smarty.dapatkan, $smarty.posting, $smarty.permintaan

Section titled “$smarty.dapatkan, $smarty.posting, $smarty.permintaan”

Variabel permintaan akses (gunakan dengan hati-hati):

{* Only for reading, always escape output! *}
<{if $smarty.get.page}>
Page: <{$smarty.get.page|escape}>
<{/if}>

Variabel server:

<p>Server: <{$smarty.server.SERVER_NAME}></p>
<p>Request URI: <{$smarty.server.REQUEST_URI|escape}></p>

Informasi lingkaran:

<{foreach $items as $item name=itemloop}>
<{* Index (0-based) *}>
Index: <{$smarty.foreach.itemloop.index}>
<{* Iteration (1-based) *}>
Number: <{$smarty.foreach.itemloop.iteration}>
<{* First item *}>
<{if $smarty.foreach.itemloop.first}>First Item!<{/if}>
<{* Last item *}>
<{if $smarty.foreach.itemloop.last}>Last Item!<{/if}>
<{* Total count *}>
Total: <{$smarty.foreach.itemloop.total}>
<{/foreach}>

Saat menggunakan XMF, tersedia pembantu tambahan:

// In PHP
use Xmf\Module\Helper;
$helper = Helper::getInstance();
$GLOBALS['xoopsTpl']->assign('mod_config', $helper->getConfig());
$GLOBALS['xoopsTpl']->assign('mod_url', $helper->url());
$GLOBALS['xoopsTpl']->assign('mod_path', $helper->path());
{* In template *}
<a href="<{$mod_url}>">Module Home</a>
<{if $mod_config.show_breadcrumb}>
{* Breadcrumb HTML *}
<{/if}>
{* Theme images *}
<img src="<{$xoops_imageurl}>images/logo.png" alt="Logo">
{* Module images *}
<img src="<{$xoops_url}>/modules/<{$xoops_dirname}>/assets/images/icon.png">
{* Upload directory *}
<img src="<{$xoops_url}>/uploads/mymodule/<{$item.image}>">
{* Using icons *}
<img src="<{$xoops_icons32_url}>edit.png" alt="Edit">
<img src="<{$xoops_icons16_url}>delete.png" alt="Delete">
{* Show only to logged-in users *}
<{if $xoops_isuser}>
<a href="<{$xoops_url}>/modules/profile/">My Profile</a>
<a href="<{$xoops_url}>/user.php?op=logout">Logout</a>
<{else}>
<a href="<{$xoops_url}>/user.php">Login</a>
<a href="<{$xoops_url}>/register.php">Register</a>
<{/if}>
{* Show only to admins *}
<{if $xoops_isadmin}>
<a href="<{$xoops_url}>/admin.php">Admin Panel</a>
<{/if}>
{* Show only to content owner *}
<{if $xoops_isowner || $xoops_isadmin}>
<a href="edit.php?id=<{$item.id}>">Edit</a>
<a href="delete.php?id=<{$item.id}>">Delete</a>
<{/if}>
// In PHP - load language file
xoops_loadLanguage('main', 'mymodule');
// Assign language constants
$GLOBALS['xoopsTpl']->assign('lang_title', _MD_MYMODULE_TITLE);
$GLOBALS['xoopsTpl']->assign('lang_submit', _SUBMIT);
{* In template *}
<h1><{$lang_title}></h1>
<button type="submit"><{$lang_submit}></button>

Atau gunakan konstanta secara langsung:

<h1><{$smarty.const._MD_MYMODULE_TITLE}></h1>

Untuk melihat semua variabel yang tersedia:

{* Display debug console *}
<{debug}>
{* Print specific variable *}
<pre><{$myvar|@print_r}></pre>
{* Export variable *}
<pre><{$myvar|@var_export}></pre>

#smarty #templates #variables #xoops #reference