因此,我想实现一些javasript,例如
<script>console.log("Hello World")</script>在我的商店软件网站上,这不是索引(家庭)网站。
我已经试过了
1)在网站正文上添加js
2)在主题的header.tpl中添加了折叠块
{block name="frontend_index_header_javascript_jquery" append}
<script>console.log("Hello World")</script>
{/block}3)在主题的header.tpl中添加了折叠块,并创建了另一个.js文件并将其加载到tpl中。
{block name="frontend_index_header_javascript_jquery" append}
<script src="{link file='frontend/_resources/JavaScript/test.js'}"></script>
{/block}什么可能是错误的,或者我需要改变什么?我正在使用商店软件5.3。
发布于 2017-11-16 13:28:41
tpl文件只是标记。添加自定义Javascript或更少/css的正确方法是将其添加到编译Javascript和Less/css文件的事件中。在我的示例中,我创建了一个订阅服务器(ThemeCompiler)来保持引导程序的干净。
我假设订阅服务器位于文件夹中。
订阅者
在插件根目录和Javascript文件中
视图/公共/前端/_public/src/js/
请记住,一旦你的文件是添加到它的所有方面在商店内的礼物。
<?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注册为订阅服务器,请将以下行添加到您的引导带中:
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重新编译它。
https://stackoverflow.com/questions/46656488
复制相似问题