Ana içeriğe geç

serB2B Özel Fonksiyonlar

Bu dokümantasyon, serB2B template sistemine özel olarak eklenmiş Twig fonksiyonları, filtreleri ve değişkenleri detaylı olarak açıklar.

Global Değişkenler

Template'lerin tamamında kullanılabilir değişkenler:

Kullanıcı ve Oturum

DeğişkenTipAçıklamaÖrnek Kullanım
userobjectGiriş yapmış kullanıcı objesi{{ user.name }}, {{ user.email }}
is_loggedbooleanKullanıcı giriş yapmış mı?{% if is_logged %}
is_adminbooleanKullanıcı admin mi?{% if is_admin %}
is_mobilebooleanMobil cihaz mı?{% if is_mobile %}
localestringMevcut dil kodu{{ locale }} (tr, en, vb.)

Site ve Konfigürasyon

DeğişkenTipAçıklamaÖrnek Kullanım
base_urlstringSite temel URL'si<base href="{{ base_url }}">
app_configobjectUygulama konfigürasyonu{{ app_config.site_name }}
requestobjectHTTP request verileri{{ request.uri_string }}
get_currencyobjectMevcut para birimi{{ get_currency.symbol }}
get_alternate_currencyobjectAlternatif para birimi{{ get_alternate_currency.code }}

E-ticaret Verileri

DeğişkenTipAçıklamaÖrnek Kullanım
categoriesarraySite kategorileri{% for cat in categories %}Kategori Alanları
groupsarraySite grupları{% for group in groups %}Grup Alanları
brandsarrayMarkalar{% for brand in brands %}Marka Alanları
productobjectÜrün detay sayfasında mevcut ürün{{ product.title }}Ürün Alanları
productsarrayÜrün listesi sayfasında ürünler{% for product in products %}Ürün Alanları

Fonksiyonlar

Konfigürasyon Fonksiyonları

config(key, default = null)

Uygulama konfigürasyon değerini getirir.

{{ config('SITE_NAME') }}
{{ config('SHOW_PRICE_TO_NONUSERS', false) }}
{{ config('DEBUG_MODE') }}

theme_config(key, default = null)

Tema konfigürasyon değerini getirir.

{{ theme_config('header_style') }}
{{ theme_config('is_sticky_header', false) }}
{{ theme_config('brands_slider_active') }}

Varlık Yönetimi Fonksiyonları

webpack_bundle(entry)

Webpack varlık paketini yükler.

{{ webpack_bundle('layout') }}      {# Ana layout varlıkları #}
{{ webpack_bundle('index') }} {# Ana sayfa varlıkları #}
{{ webpack_bundle('product') }} {# Ürün sayfası varlıkları #}
{{ webpack_bundle('list') }} {# Liste sayfası varlıkları #}

webpack_bundle_header()

Header'a özel webpack varlıklarını yükler.

{{ webpack_bundle_header() }}       {# Header stili için özel CSS/JS #}

Render Fonksiyonları

render_header()

Tema ayarlarına göre uygun header'ı render eder.

{{ render_header() }}                {# Otomatik header seçimi #}

render_template(template, data = [])

Belirtilen template'i parametrelerle render eder.

{{ render_template('_product_card', {'product': item}) }}
{{ render_template('widgets/banner', {'slides': banner_data}) }}

render_page(url)

CMS sayfasını URL'den render eder.

{{ render_page('footer-content') }}
{{ render_page('privacy-policy') }}

___(widget_key)

CMS widget'ını render eder.

{{ ___('page__signin_box') }}
{{ ___('home__top_banner') }}
{{ ___('category__sidebar_menu') }}

E-ticaret Fonksiyonları

get_last_products(limit = 12)

Son eklenen ürünleri getirir.

{% set last_products = get_last_products() %}
{% set featured = get_last_products(8) %}

get_brands(active_only = true)

Markaları getirir.

{% set brands = get_brands() %}
{% set all_brands = get_brands(false) %}

get_banner(key)

Banner verilerini getirir.

{% set main_slider = get_banner('main_slider') %}
{% set brands_banner = get_banner('brands_slider') %}

is_product_in_cart(product_id)

Ürünün sepette olup olmadığını kontrol eder.

{% if is_product_in_cart(product.id) %}
<button class="in-cart">Sepette</button>
{% endif %}

get_product_cart_count(product_id)

Sepetteki ürün adetini getirir.

Sepette: {{ get_product_cart_count(product.id) }} adet

SEO ve İçerik Fonksiyonları

page_title()

Sayfa başlığını getirir.

<title>{{ page_title() ?: config('SAYFA_BASLIK') }}</title>

page_description()

Sayfa açıklamasını getirir.

<meta name="description" content="{{ page_description() }}">

page_keywords()

Sayfa anahtar kelimelerini getirir.

<meta name="keywords" content="{{ page_keywords() }}">

URL Fonksiyonları

site_url(path = '')

Site URL'si oluşturur.

<a href="{{ site_url('products') }}">Ürünler</a>
<a href="{{ site_url('category/electronics') }}">Elektronik</a>

get_locales()

Mevcut dilleri ve URL'lerini getirir.

{% for locale_data in get_locales() %}
<a href="{{ locale_data.url }}">
<img src="{{ ('flags/' ~ locale_data.code ~ '.png') | image }}">
{{ locale_data.name }}
</a>
{% endfor %}

Sistem Fonksiyonları

ui_head_tags()

Sistem head etiketlerini ekler (analytics, meta vb.).

<head>
{{ ui_head_tags() }}
</head>

Sistem footer script'lerini ekler.

    {{ ui_footer_tags() }}
</body>

ui_after_body_tags()

Body'den hemen sonra gelen sistem etiketlerini ekler.

<body>
{{ ui_after_body_tags() }}

dump(variable)

Debug için değişken içeriğini gösterir.

{% if config('DEBUG_MODE') %}
{{ dump(product) }}
{% endif %}

Filtreler

Çeviri Filtreleri

translate veya t

Metni mevcut dile çevirir.

{{ 'merhaba_dunya' | t }}
{{ 'hosgeldin_mesaji' | translate }}
{{ 'urun_bulunamadi' | t }}

Konfigürasyon Filtreleri

config

Konfigürasyon değerini filtre olarak getirir.

{{ 'SITE_NAME' | config }}
{{ 'SHOW_PRICES' | config }}

theme_config

Tema konfigürasyon değerini filtre olarak getirir.

{{ 'header_style' | theme_config }}
{{ 'primary_color' | theme_config }}

URL ve Resim Filtreleri

url

Site URL'si oluşturur.

{{ 'products' | url }}
{{ 'category/electronics' | url }}
<a href="{{ 'login' | url }}">Giriş Yap</a>

route

Sistem route'u getirir.

{{ 'ADD_TO_CART' | route }}
{{ 'LOGIN' | route }}
{{ 'CHECKOUT' | route }}

image

Tema resim URL'si oluşturur.

{{ 'logo.png' | image }}
{{ 'icons/cart.svg' | image }}
<img src="{{ 'banner.jpg' | image }}" alt="Banner">

image_url

Upload edilmiş resim URL'si oluşturur.

{{ product.image | image_url }}
{{ category.banner | image_url }}

Fiyat ve Format Filtreleri

price_with_currency

Fiyatı para birimi ile formatlar.

{{ product.price | price_with_currency }}
{{ 1234.56 | price_with_currency }}

price_format

Fiyatı formatlar (para birimi olmadan).

{{ product.price | price_format }}
{{ discount_amount | price_format }}

HTML ve Veri Filtreleri

html_data_attributes

Dizi/objeden HTML data attribute'ları oluşturur.

<div {{ product.variant_data | html_data_attributes }}>
{# Çıktı: data-variant-id="123" data-variant-name="Kırmızı" #}
</div>

json_encode

Veriyi JSON formatına çevirir.

<script>
var productData = {{ product | json_encode }};
</script>

Pratik Örnekler

Responsive Grid Oluşturma

<div class="row">
{% for product in products %}
{% if loop.index0 % 4 == 0 and not loop.first %}
</div><div class="row">
{% endif %}
<div class="col-md-3">
{{ include('_product_card.twig.php', {'product': product}) }}
</div>
{% endfor %}
</div>

Koşullu Widget Yükleme

{% if theme_config('module_instagram') and theme_config('module_instagram_token') %}
{% include 'widgets/instagram.twig.php' %}
{% endif %}

{% for ii in 1..4 %}
{% if theme_config('category_grid_slider_' ~ ii ~ '_active') %}
{% include 'widgets/category_grid_' ~ ii ~ '.twig.php' %}
{% endif %}
{% endfor %}

Güvenli Değişken Kullanımı

{# Varsayılan değer ile #}
{{ product.title | default('İsimsiz Ürün') }}

{# HTML içerik için #}
{{ product.description | default('&nbsp;') | raw }}

{# Null coalescing operatörü #}
{{ product.subtitle ?: 'Alt başlık yok' }}

{# Çoklu kontrol #}
{% if product.price is defined and product.price > 0 %}
{{ product.price | price_with_currency }}
{% endif %}

Dil Seçici

<div class="language-selector">
{% for locale_data in get_locales() %}
<a href="{{ locale_data.url }}"
class="{% if locale_data.code == locale %}active{% endif %}">
<img src="{{ ('flags/' ~ locale_data.code ~ '.png') | image }}"
alt="{{ locale_data.name }}">
{{ locale_data.name }}
</a>
{% endfor %}
</div>

Sayfalama

{% if products.pagination is defined %}
<nav aria-label="Sayfa navigasyonu">
{{ products.pagination | raw }}
</nav>
{% endif %}

Debug ve Geliştirme

Debug Modu

{% if config('DEBUG_MODE') %}
<div class="debug-panel">
<h4>Debug Bilgileri</h4>
<pre>{{ dump(product) }}</pre>
<p>Sayfa yükleme süresi: {{ execution_time }} saniye</p>
<p>Bellek kullanımı: {{ memory_usage }} MB</p>
</div>
{% endif %}

Değişken Kontrolü

{# Değişken tanımlı mı? #}
{% if product is defined %}
{{ product.title }}
{% endif %}

{# Değişken boş mu? #}
{% if product.description is not empty %}
{{ product.description | raw }}
{% endif %}

{# Değişken tipi kontrolü #}
{% if products is iterable %}
{% for product in products %}
{# ... #}
{% endfor %}
{% endif %}

Performans İpuçları

  1. Include yerine Embed kullanın - Değişken scope'u izole etmek için
  2. Cache kullanın - Sık değişmeyen içerikler için
  3. Lazy loading - Resimler için data-src attribute'u
  4. Bundle splitting - Sayfa bazlı webpack bundle'ları
  5. Koşullu yükleme - Gereksiz widget'ları yüklemeyin

Güvenlik Notları

  1. Otomatik escaping - Twig otomatik olarak HTML escape yapar
  2. Raw filtresi dikkatli kullanın - Sadece güvenilir içerik için
  3. User input'u filtreleyin - Özellikle URL parametreleri
  4. CSRF token kullanın - Form gönderimlerinde
  5. XSS koruması - Twig'in otomatik escape özelliğine güvenin

📚 İlgili Dokümantasyonlar: