0

I developed a new extension. I have some CSS which I need to include on some templates.

CSS:

app\code\Fekete\Newsletter2Go\view\frontend\web\css\main.css

Block:

\app\code\Fekete\Newsletter2Go\Block\Form.php

Template:

app\code\Fekete\Newsletter2Go\view\frontend\templates\subscribe.phtml

The template is getting called in a CMS page:

{{block class="Fekete\Newsletter2Go\Block\Form" template="Fekete_Newsletter2Go::subscribe.phtml"}}

How can I include the CSS main.css in my template?

I solved it like this, it works... but in the developer documentation they say that we should not include css in the template.

<link rel="stylesheet" href="<?= $this->getViewFileUrl('Fekete_Newsletter2Go::css/main.css'); ?>"/>
  • Did you override template file or it will call in your controller? – Rakesh Varma Nov 6 '19 at 10:38
  • No, I call the template in a cms page like this {{block class="Fekete\Newsletter2Go\Block\Form" template="Fekete_Newsletter2Go::subscribe.phtml"}} – Black Nov 6 '19 at 10:59
2

You have to add below code in your router_controller_action.xml before body tag

<head>
    <css src="Vendor_Module::css/filename.css" />
</head>

If it is a CMS page then you can add above code in CMS page layout update field

  • I have no layout.xml in my module. I created one app\code\Fekete\Newsletter2Go\view\frontend\layout\layout.xml and added your code above <body> but it does not work. Of course I modified it to match my scenario. – Black Nov 6 '19 at 10:48
  • I had to rename layout.xml to default.xml, now it works – Black Nov 6 '19 at 10:54
  • 1
    now it will be included on every page, if you want to include it on any particular page then you have to rename it to router_controller_action.xml – Piyush Nov 6 '19 at 10:56
  • Oh ok I see. I have no controller for this though, it is only a template which I call in a cms page. – Black Nov 6 '19 at 10:57
  • If it is a CMS page then you can add above code in CMS page Layout Update XML field – Piyush Nov 6 '19 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.