4

I tried so may things but unable to add the CSS & JS files for each Static CMS pages. I know if CSS can be added in cms_page_view.xml but this will reflect in all pages.

is there anyway to add different CSS files for different static pages.

seb
3,5723 gold badges26 silver badges57 bronze badges
asked Jul 20, 2016 at 14:34

5 Answers 5

5

You can add Layout Update XML update

GOTO ADMIN -> Content -> Pages -> Edit page -> Design -> Layout Update XML

With the content :

<head>
 <css src="Namespace_YourModule::css/styles.css"/>
</head>

Then there is a bug in Magento 2, you need to override the default Magento 2 vendor/magento/framework/View/Layout/etc/page_layout.xsd validation file.

A workaround, not a clean solution, but waiting Magento 2 to allow override xsd files

app/code/Vendor/Module/etc/di.xml

<preference for="Magento\Framework\View\Model\Layout\Update\Validator" type="Vendor\Module\Model\Layout\Update\Validator" />

app/code/Vendor/Module/etc/page_layout.xsd

<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:include schemaLocation="urn:magento:framework:View/Layout/etc/elements.xsd"/>
 <xs:include schemaLocation="urn:magento:framework:View/Layout/etc/head.xsd"/>
 <xs:include schemaLocation="urn:magento:framework:View/Layout/etc/body.xsd"/>
 <xs:complexType name="pageLayoutType">
 <xs:sequence minOccurs="0" maxOccurs="unbounded">
 <xs:element ref="referenceContainer" minOccurs="0" maxOccurs="unbounded"/>
 <xs:element ref="container" minOccurs="0" maxOccurs="unbounded"/>
 <xs:element ref="update" minOccurs="0" maxOccurs="unbounded"/>
 <xs:element ref="move" minOccurs="0" maxOccurs="unbounded"/>
 <xs:element name="head" type="headType" minOccurs="0" maxOccurs="unbounded"/>
 <xs:element name="body" type="bodyType" minOccurs="0" maxOccurs="unbounded"/>
 </xs:sequence>
 </xs:complexType>
 <xs:element name="layout" type="pageLayoutType">
 <xs:unique name="containerKey">
 <xs:selector xpath=".//container"/>
 <xs:field xpath="@name"/>
 </xs:unique>
 </xs:element>
</xs:schema>

app/code/Vendor/Module/Model/Layout/Update/Validator.php

<?php
namespace Hublot\Setup\Model\Layout\Update;
use Magento\Framework\Config\Dom\UrnResolver;
class Validator extends \Magento\Framework\View\Model\Layout\Update\Validator
{
 const LAYOUT_SCHEMA_PAGE_HANDLE = 'page_layout';
 const LAYOUT_SCHEMA_MERGED = 'layout_merged';
 /**
 * @param \Magento\Framework\Config\DomFactory $domConfigFactory
 * @param \Magento\Framework\Config\Dom\UrnResolver $urnResolver
 */
 public function __construct(
 \Magento\Framework\Config\DomFactory $domConfigFactory,
 UrnResolver $urnResolver
 ) {
 $this->_domConfigFactory = $domConfigFactory;
 $this->_initMessageTemplates();
 $this->_xsdSchemas = [
 self::LAYOUT_SCHEMA_PAGE_HANDLE => $urnResolver->getRealPath(
 'urn:hublot:module:Hublot_Setup:etc/page_layout.xsd'
 ),
 self::LAYOUT_SCHEMA_MERGED => $urnResolver->getRealPath(
 'urn:magento:framework:View/Layout/etc/layout_merged.xsd'
 ),
 ];
 }
}
answered Mar 27, 2017 at 10:23
3
3

Please follow below steps to add css and js to specific cms page using custom module.

1) Create custom module using below step

app/code/Vendor/Module/registration.php

<?php
 \Magento\Framework\Component\ComponentRegistrar::register(
 \Magento\Framework\Component\ComponentRegistrar::MODULE,
 'Vendor_Module',
 __DIR__
);

app/code/Vendor/Module/etc/module.xml

<?xml version="1.0" encoding="UTF-8"?>
 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
 <module name="Vendor_Module" setup_version="1.0.0"/>
 </config>

app/code/Vendor/Module/view/frontend/web/css/custom.css Then add your css code into custom.css file

2) GOTO ADMIN -> Content -> Pages -> Edit page -> Design -> Layout Update XML and add below code for particulate page.

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

For the other cms page create specific css file and add app/code/Vendor/Module/view/frontend/web/css/ and add that css file into other cms page same as step2 via just changing css file name.

enter image description here

answered Jan 12, 2020 at 9:14
0

You can add custom css like below:
make an entry in layout file

 <page> 
 <head>
 <css src="css/ie-9.css" ie_condition="IE 9" />
 <css src="Magento_Catalog::css/custom.css" />
 <css src="Custom_SampleModule::css/custom.css"/>
 </head>
 </page>
answered Nov 11, 2017 at 16:20
1
  • What is the full path to 'Magento_Catalog::css/custom.css' or 'Custom_SampleModule::css/custom.css' Commented Jan 7, 2020 at 23:28
0

I found that Franck Garnier's does not quite work on 2.1.16.

I had to change the last file, Validator.php, a bit to get it to work as follows.

namespace Vendor\CmsLayoutFix\Model\Layout\Update;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Config\Dom\UrnResolver;
use Magento\Framework\Config\DomFactory;
use Magento\Framework\Config\ValidationStateInterface;
class Validator extends \Magento\Framework\View\Model\Layout\Update\Validator
{
 const LAYOUT_SCHEMA_PAGE_HANDLE = 'page_layout';
 const LAYOUT_SCHEMA_MERGED = 'layout_merged';
 private $validationState;
 public function __construct(
 DomFactory $domConfigFactory,
 UrnResolver $urnResolver,
 ValidationStateInterface $validationState = null
 ) {
 parent::__construct($domConfigFactory, $urnResolver);
 $this->_xsdSchemas = [
 self::LAYOUT_SCHEMA_PAGE_HANDLE => $urnResolver->getRealPath(
 'urn:vendor:module:Vendor_CmsLayoutFix:etc/page_layout.xsd'
 ),
 self::LAYOUT_SCHEMA_MERGED => $urnResolver->getRealPath(
 'urn:magento:framework:View/Layout/etc/layout_merged.xsd'
 ),
 ];
 }
}

WHERE:

vendor = Vendor

module = CmsLayoutFix

answered Feb 5, 2019 at 7:38
-1

In magento all CMS pages have an option of Layout Update XML update .Just Call your Custom code there also add CSS or JS .

Admin CMS Pages

GOTO ADMIN -> Content -> Pages -> Edit page -> Design -> Layout Update XML

add below call

<head>
 <css src="Namespace_YourModule::css/styles.css"/>
</head>
answered Jul 20, 2016 at 14:46
5
  • this works in page layout configuration not in Layout Update XML , hope you checked... Commented Jul 21, 2016 at 4:44
  • It is working fine I have tested this code . just remove "Namespace_YourModule::" and set in to "Layout Update XML" and refresh you Cache so you can show in that page only. Commented Jul 21, 2016 at 18:59
  • 1
    Please correct the XML data and try again. Element 'head': This element is not expected. Expected is one of ( referenceContainer, container, update, move ). Line: 1 this we will get if we put anything else apart from above four Element. I am using magento 2.0.7 Commented Jul 22, 2016 at 4:40
  • 1
    Yeah, this worked for me. Just remember to flush the cache after you change the layout for the cms page to pickup the change. Commented Dec 13, 2017 at 3:23
  • What is "Namespace_YourModule" is it my custom theme? I already extended Luma theme but I do not have any specific module. Commented Jan 7, 2020 at 23:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.