首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在商店软件站点中实现JS

在商店软件站点中实现JS
EN

Stack Overflow用户
提问于 2017-10-09 23:30:59
回答 1查看 1.2K关注 0票数 1

因此,我想实现一些javasript,例如

代码语言:javascript
复制
<script>console.log("Hello World")</script>

在我的商店软件网站上,这不是索引(家庭)网站。

我已经试过了

1)在网站正文上添加js

2)在主题的header.tpl中添加了折叠块

代码语言:javascript
复制
{block name="frontend_index_header_javascript_jquery" append}
   <script>console.log("Hello World")</script>
{/block}

3)在主题的header.tpl中添加了折叠块,并创建了另一个.js文件并将其加载到tpl中。

代码语言:javascript
复制
{block name="frontend_index_header_javascript_jquery" append}
   <script src="{link file='frontend/_resources/JavaScript/test.js'}"></script>
{/block}

什么可能是错误的,或者我需要改变什么?我正在使用商店软件5.3。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-16 13:28:41

tpl文件只是标记。添加自定义Javascript或更少/css的正确方法是将其添加到编译Javascript和Less/css文件的事件中。在我的示例中,我创建了一个订阅服务器(ThemeCompiler)来保持引导程序的干净。

我假设订阅服务器位于文件夹中。

订阅者

在插件根目录和Javascript文件中

视图/公共/前端/_public/src/js/

请记住,一旦你的文件是添加到它的所有方面在商店内的礼物。

代码语言:javascript
复制
<?php

namespace ShopwarePlugins\MyPlugin\Subscriber;

use \Shopware\Components\Theme\LessDefinition;
use Doctrine\Common\Collections\ArrayCollection;

class ThemeCompiler implements \Enlight\Event\SubscriberInterface
{

  public static function getSubscribedEvents()
  {
    return array(
        'Theme_Compiler_Collect_Plugin_Less' => 'onCollectLessFiles',
        'Theme_Compiler_Collect_Plugin_Javascript' => 'onCollectJavascriptFiles'
    );
  }

  private function path(){
    return __DIR__ . '/../';
  }

  public function onCollectLessFiles()
  {
    return new LessDefinition(
        [],
        [$this->path(). 'Views/common/frontend/_public/src/less/myLessFile.less']
    );
  }

  public function onCollectJavascriptFiles()
  {
    $jsFiles = array(
        $this->path() . 'Views/common/frontend/_public/src/js/myJsFile.js'
    );
    return new ArrayCollection($jsFiles);
  }

}

要将ThemeCompiler注册为订阅服务器,请将以下行添加到您的引导带中:

代码语言:javascript
复制
use ShopwarePlugins\MyPlugin\Subscriber\ThemeCompiler;

...

private function registerSubscriber(){
    $this->subscribeEvent('Enlight_Controller_Front_StartDispatch', 'onRegisterSubscriber');
    $this->subscribeEvent('Shopware_Console_Add_Command', 'onRegisterSubscriber');
}

public function onRegisterSubscriber(Enlight_Event_EventArgs $args){
    Shopware()->Events()->addSubscriber(new ThemeCompiler());
}

添加这些更改后,清除商店缓存并使用后端-ui重新编译它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46656488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档