如何在 TPL 文件中的 prestashop 1.7 中检测 smarty 中的设备?

问题描述 投票:0回答:3

我想要以下内容:

& 我知道我必须从 PrestaShop 的 context.php 中获取代码,但我似乎犯了一个错误。 getcontext的链接如下:(检测移动设备的代码在这里) https://github.com/PrestaShop/PrestaShop/blob/develop/classes/Context.php

{if isset($products) AND $products}
             {$tabname=rand()+count($products)}
            {if isset($display_mode) && $display_mode == 'carousel'}
                {include file="{$items_owl_carousel_tpl}" items=$products image_size=$image_size}
            {else}
                {if device is MOBILE} /* Correct Code Needed */
                    {include file="{$items_normal_tpl}" items=$products image_size="homepage_default"}
                {else device is NOT MOBILE} /* Correct Code Needed */
                    {include file="{$items_normal_tpl}" items=$products image_size="home_default"}
                {/if}
            {/if}
        {/if}

我应该在 IF 条件中输入什么代码以确保它检测到移动设备而不是移动设备。

还有 IF 条件写得是否正确,我应该在这段代码中更改什么?

这是 .TPL 文件。

php if-statement smarty responsive prestashop-1.7
3个回答
6
投票

尝试:

{if Context::getContext()->isMobile() == 1}
    {if Context::getContext()->getDevice() != 2}
        // TABLETTE
    {else}
        // MOBILE
    {/if}
{else}
    // PC
{/if}

问候


0
投票

在 Prestashop 1.7.8.2 中它的工作原理是 -->

{assign var="dispositivo" value="desktop"}

{if Context::getContext()->isMobile()}

    {assign var="dispositivo" value="mobile"}

{else if  Context::getContext()->isTablet()}

    {assign var="dispositivo" value="tablet"}

{else}

    {assign var="dispositivo" value="desktop"}

{/if}

0
投票

我强烈建议直接在 tpl 中调用 php 类。我刚刚介绍了有关设备的全局变量,因此从版本 9.x 开始您可以使用:

{if $device.is_mobile}
   My mobile logic here
{else if $device.is_tablet}
   My tablet logic here
{/if}

您还可以使用:

{if $device.type == 1}
   My mobile logic here
{/if}

但出于可读性和可维护性的问题我不会推荐它。

如果您的 prestashop 版本中尚未提供此功能,您可以通过覆盖添加它:

<?php

class FrontController extends FrontControllerCore
{
    protected function assignGeneralPurposeVariables()
    {
        parent::assignGeneralPurposeVariables();

        $this->context->smarty->assign([
            'device' => $this->getTemplateVarDevice(),
        ]);
    }

    protected function getTemplateVarDevice(): array
    {
        return [
            'is_mobile' => $this->context->isMobile(),
            'is_tablet' => $this->context->isTablet(),
            'type' => $this->context->getDevice(),
        ];
    }

}

或通过 prestashop 原生挂钩,例如:

actionFrontControllerSetVariables

参见:https://github.com/PrestaShop/PrestaShop/pull/34024

© www.soinside.com 2019 - 2024. All rights reserved.