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'); ?>"/>
2
  • Did you override template file or it will call in your controller? Commented Nov 6, 2019 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"}} Commented Nov 6, 2019 at 10:59

1 Answer 1

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

5
  • 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. Commented Nov 6, 2019 at 10:48
  • I had to rename layout.xml to default.xml, now it works Commented Nov 6, 2019 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 Commented Nov 6, 2019 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. Commented Nov 6, 2019 at 10:57
  • If it is a CMS page then you can add above code in CMS page Layout Update XML field Commented Nov 6, 2019 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.