X-Cart:Dynamic .tpl Patcher

From X-Cart 4 Classic
Revision as of 21:18, 25 October 2016 by Nastya (talk | contribs)
Jump to: navigation, search

Using the dinamic .tpl patcher to create a module

This article is intended for X-Cart module developers; it explains how to use dinamic .tpl patcher when creating a module.

During the distribution pack generating you have to generate a diff file for every graphic template. (Please see: X-Cart:Generating_module_distribution_packs_for_X-Cart_4) 3rd-party templates and customizations are not considered, which may lead to difficulties installing the module.

To solve this problem, you can use smarty prefilters/output filters to insert the module entry points in the current skin during compilation - which is preferable - or when the template is displayed.

You may find the files here:
File:Dynamic tpl patcher.4.7.x.php
or here:
File:Dynamic tpl patcher.4.6.x.php


Examples of using prefilters:

1. adding the following piece of code in init function of func_MODULE_init
   if (AREA_TYPE == 'C') {
        require $xcart_dir . XC_DS . 'modules' . XC_DS . 'Pilibaba' . XC_DS . 'lib' . XC_DS . 'dynamic_tpl_patcher.php';
        modules\Pilibaba\lib\x_tpl_add_callback_patch('customer/main/cart.tpl', 'func_pilibaba_tpl_insertButton', X_TPL_PREFILTER);
        modules\Pilibaba\lib\x_tpl_add_callback_patch('customer/minicart.tpl', 'func_pilibaba_tpl_insertButton', X_TPL_PREFILTER);
        modules\Pilibaba\lib\x_tpl_add_callback_patch('modules/Add_to_cart_popup/product_added.tpl', 'func_pilibaba_tpl_insertButton', X_TPL_PREFILTER);
    } 


2. adding the following in func_pilibaba_tpl_insertButton
function func_pilibaba_tpl_insertButton($tpl_name, $tpl_source) {//{{{
    if (strpos($tpl_source, 'pilibaba_enabled') !== false) {
        return $tpl_source;
    }

    // add button {include file="modules/Pilibaba/checkout_btn.tpl" btn_place=...
    $search = '%{if \$amazon_pa_enabled}[^{]*{include file="modules/Amazon_Payments_Advanced/checkout_btn.tpl"[^{]*{/if}%Ss';
    $tpl_source = preg_replace_callback($search,
        function ($matches) {
            return $matches[0] . "\n" . str_replace(
                array('amazon_pa_enabled', 'Amazon_Payments_Advanced'),
                array('pilibaba_enabled','Pilibaba'), $matches[0]);
        },
        $tpl_source
    );

    $tpl_source = str_replace('{if $paypal_express_active || $amazon_pa_enabled', '{if $paypal_express_active || $amazon_pa_enabled || $pilibaba_enabled', $tpl_source);

    if (defined('XC_PILIBABA_DEBUG')) {
       x_log_add('pilibaba_patched_fiels', 'patched_file:' . $tpl_name . "\n" . $tpl_source);
    }

    return $tpl_source;
}//}}} 


Example of using output filter run on certain pages:

if (
    'C' != x_get_area_type()
    && x_check_controller_condition(NULL, array('register', 'user_modify'))
) {
    x_tpl_add_regexp_patch(
        'modules/XAuth/linked_accounts_admin.tpl',
        '/(<form [^>]*name="registerform"[^>]*>.+)(<tr>.+<\/tr>)/USs',

        '\1%%\2'
    );
}
x_tpl_add_listener('modules/XAuth/register.tpl', 'before', 'func_xauth_prepare_register');
x_tpl_add_listener('modules/One_Page_Checkout/profile/account_info.tpl', 'before', 'func_xauth_prepare_register');


Using prefilters helps to simplify inserting modules entry points, including 3rd-party and custom-built modules.