Exemple #1
0
/**
* 2015-12-14
* Добавил возможность передачи в качестве первого параметра @see O
* причём как в виде объекта, так и строки-класса.
*
* Такая возможность позволяет нам эффективно рендерить шаблоны без иерархии своих классов-блоков.
* В Российской сборке для Magento 1.x
* нам приходилось дублировать один и тот же код в классе базовой модели (аналоге класса O),
* и в 2-х базовых классах блоков (абстрактном и блоке с шаблоном), т.е. в 3 местах.
* Теперь же нам этого делать не нужно.
*
* @used-by df_phtml()
* @param string|O|null $type
* @param string|array(string => mixed) $data [optional]
* @param string|null $template [optional]
*
* 2016-11-22
* @param array $vars [optional]
* Параметры $vars будут доступны в шаблоне в качестве переменных:
* @see \Magento\Framework\View\TemplateEngine\Php::render()
		extract($dictionary, EXTR_SKIP);
* https://github.com/magento/magento2/blob/2.1.2/lib/internal/Magento/Framework/View/TemplateEngine/Php.php#L58
*
* @return AbstractBlock|BlockInterface|Template
*/
function df_block($type, $data = [], $template = null, array $vars = [])
{
    /**
     * 2015-12-14
     * $type может быть как объектом, так и строкой:
     * https://3v4l.org/udMMH
     */
    /** @var O $context */
    if (!is_a($type, O::class, true)) {
        $context = null;
    } else {
        $context = is_object($type) ? $type : new $type();
        $type = null;
    }
    if (is_null($type)) {
        $type = df_is_backend() ? BackendTemplate::class : Template::class;
    }
    /** @var string|null $template */
    if (is_string($data)) {
        $template = $data;
        $data = [];
    }
    /** @var AbstractBlock|BlockInterface|Template $result */
    /**
     * 2016-11-22
     * В отличие от Magento 1.x, в Magento 2 нам нужен синтаксис ['data' => $data]:
     * @see \Magento\Framework\View\Layout\Generator\Block::createBlock():
     * $block->addData(isset($arguments['data']) ? $arguments['data'] : []);
     * https://github.com/magento/magento2/blob/2.1.2/lib/internal/Magento/Framework/View/Layout/Generator/Block.php#L240
     * В Magento 1.x было не так:
     * https://github.com/OpenMage/magento-mirror/blob/1.9.3.1/app/code/core/Mage/Core/Model/Layout.php#L482-L491
     */
    $result = df_layout()->createBlock($type, dfa($data, 'name'), ['data' => $data]);
    // 2016-11-22
    $result->assign($vars);
    if ($template && $result instanceof Template) {
        $result->setTemplate(df_append($template, '.phtml'));
    }
    if ($context) {
        // 2016-11-22
        // «Sets the object that should represent $block in template.»
        $result->setTemplateContext($context);
    }
    return $result;
}
Exemple #2
0
/**
 * 2016-05-31
 * @param string[] ...$args
 * @return string
 */
function df_cc_path_t(...$args)
{
    return df_append(df_cc_path(dfa_flatten($args)), '/');
}