php - Shopware 5.2.11 在我的插件中包含 JavaScript

标签 php smarty shopware

我想在我的插件中添加一个 JavaScript 文件。我在关注这个tutorial并且能够在我的装置上成功获得今日标语(流浪汉)。

此外,我想加载一个 JavaScript 文件。我更改了 index.tpl 并添加了以下行:

   {* Include own Javascript Code *}
    {block name="frontend_index_header_javascript_jquery_lib"}
      {debug}
      {$smarty.block.parent}
      {if $myVariable}<script type="text/javascript" src="{link   file='frontend/_public/src/js/myFile.js'}"></script>{/if}
    {/block}

查看生成的 html 的源代码,脚本标记已成功加载。但是,我的 JavaScript 文件根本不会被加载。我必须将文件放在我的插件文件夹中的什么位置?

目前我有这个结构

NameOfZipfile.zip
└──Frontend
   ├─MyPlugin
   │ └─Views
   │   ├─_public
   │   │ └─src
   │   │   └─js
   │   │     └─myFile.js
   │   └─frontend
   │     └─index
   │       └─index.tpl
   └─Bootstrap.php

我使用的是 shopware 5.2.11。我不想添加内联脚本。我做错了什么?

最佳答案

(代表 OP 发布)

解决方案,我在 Bootstrap.php 安装方法中添加了以下内容:

$this->subscribeEvent('Theme_Compiler_Collect_Plugin_Javascript', 'addJsFiles');

还有这个函数:

 public function addJsFiles(Enlight_Event_EventArgs $args){
    $jsFiles = array(__DIR__ . '/Views/_public/src/js/myFile.js');

    return new Doctrine\Common\Collections\ArrayCollection($jsFiles);
}

关于php - Shopware 5.2.11 在我的插件中包含 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40868970/

相关文章:

php - 如何从表格内容中获取记录作为弹出表单的数据并显示

php - 来自 html 表单的 pgsql 日期范围查询

php - fatal error : Call to a member function count() on boolean

php - 如何在Smarty中生成年份?

shopware - 如何在shopware 6中搜索翻译的实体?

performance - 为什么使用 Google Chrome Lighthouse 时会出现未使用的图像资源?

php - 通过PHP解析字典对象并创建MySQL表

javascript - 在 prestashop 中使用 jquery 进行翻译

centos - 在 Centos 7 上安装 Shopware 时出错

php - 在 block 输出上应用 Smarty 修改器