magento 2 theming - knowledge sharing session by suman kc

41
This is Magento 2 Theming KSS (Knowledge sharing session) by Suman KC

Upload: suman-kc

Post on 14-Jan-2017

405 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Magento 2 theming - knowledge sharing session by suman kc

This is Magento 2 Theming

KSS (Knowledge sharing session) by Suman KC

Page 2: Magento 2 theming - knowledge sharing session by suman kc

I am Suman KCMagento developer www.linkedin.com/in/sumankc

www.sumankc.com

Hello!

Page 3: Magento 2 theming - knowledge sharing session by suman kc

Topics

◉Introduction◉Themes◉Layout◉Templates◉CSS

Page 4: Magento 2 theming - knowledge sharing session by suman kc

IntroductionMagento & Magento 2 intro leading to theming aspects

1

Page 5: Magento 2 theming - knowledge sharing session by suman kc

◉eCommerce platform◉Huge◉Unique◉Poor documentation◉Edition(Community & enterprise)

Magento in general

Page 6: Magento 2 theming - knowledge sharing session by suman kc

14K+ Files & 5K+ FoldersThat’s a lot of files & directory

109MBand that's before you add themes, modules and all your image

libraries

300+ tablesProduct & category alone has 50+ tables relations

Mag

ento

1.9

.2

Page 7: Magento 2 theming - knowledge sharing session by suman kc

Magento views are separated into

Magento views

LayoutsBlocks Templates

Page 8: Magento 2 theming - knowledge sharing session by suman kc

ThemesCreation, Structure, Configuration & Inheritance

2

Page 9: Magento 2 theming - knowledge sharing session by suman kc

1. Prerequisites◉ Not modify out of box Magento themes

[Compatibility,upgradability & easy maintenance]

◉ Set magento application to developer mode[mode influences the way static files are cached]

Creating a theme

Page 10: Magento 2 theming - knowledge sharing session by suman kc

app/design/frontend/<Vendor>/├── <theme>/│ ├── etc/│ │ ├── view.xml│ ├── web/│ │ ├── images│ │ │ ├── logo.svg│ ├── registration.php│ ├── theme.xml│ ├── composer.json

Theme directory structure

Page 11: Magento 2 theming - knowledge sharing session by suman kc

<theme_dir>/├── <Vendor>_<Module>/ │ ├── web/│ │ ├── css/│ │ │ ├── source/│ ├── layout/│ │ ├── override/│ ├── templates/├── etc/├── i18n/ ├── media/├── web/│ ├── css/│ │ ├── source/ │ ├── fonts/│ ├── images/│ ├── js/├── composer.json ├── registration.php ├── theme.xml

Typi

cal M

agen

to th

eme

dire

ctor

y

Page 12: Magento 2 theming - knowledge sharing session by suman kc

Apply a theme1. In Admin, go to Stores > Configuration > Design.2. In the Store View drop-down field, select the store view

where you want to apply the theme.3. On the Design Theme tab, select your newly created

theme in the Design Theme drop-down.4. Click Save Config.5. If caching is enabled, clear the cache.6. To see your changes applied, reload the store front pages.

Configure a theme (admin section)

Page 13: Magento 2 theming - knowledge sharing session by suman kc

The properties of product images used on the storefront are stored in the view.xml configuration file.

<theme_dir>/etc/view.xml

Image properties are configured for each image type defined by the id and type attributes of the <image> element:

<images module="Magento_Catalog"><image id="unique_image_id"

type="image_type">...</image><images/>

Configure image properties

Page 14: Magento 2 theming - knowledge sharing session by suman kc

◉ Set a parent theme

◉ Override view.xml file

◉ Override static assets

Theme inheritance

◉ Override templates

◉ Extend Layouts

◉ Override Layouts

Page 15: Magento 2 theming - knowledge sharing session by suman kc

◉ Current theme static files: <theme_dir>/web/◉ Ancestor’s static files, recursively, until a theme with no

parent is reached: <parent_theme_dir>/web/

Module context◉ Current theme module static files

<theme_dir>/<Namespace>_<Module>/web/

◉ Ancestor themes module static files, recursively, until a theme with no ancestor is reached: <parent_theme_dir>/<Namespace>_<Module>/web/

Override static assets

Page 16: Magento 2 theming - knowledge sharing session by suman kc

Fallback schema◉ Current theme templates:

<theme_dir>/<Namespace>_<Module>/templates

◉ Ancestors themes templates, recursively, until a theme with no ancestor is reached: <parent_theme_dir>/<Namespace>_<Module>/templates

◉ Module templates: <module_dir>/view/frontend/templates

Override templates

Page 17: Magento 2 theming - knowledge sharing session by suman kc

The system collects layout files in the following order◉ Current theme layouts:

<theme_dir>/<Vendor>_<Module>/layout/

◉ Ancestors themes <parent_theme_dir>/<Vendor>_<Module>/layout/

◉ Module templates: <module_dir>/view/frontend/templates

◉ Module layouts for the frontend area: <module_dir>/view/frontend/layout/

◉ Module layouts for the base area: <module_dir>/view/base/layout/

Extend Layouts

Page 18: Magento 2 theming - knowledge sharing session by suman kc

To override the instructions from an ancestor theme layout file:

● Create a layout file with the same name in the <theme_dir>/<Vendor>_<Module>/layout/override/theme/<Vendor>/<ancestor_theme> directory.

*Though overriding layouts is not recommended, it is still possible, and might be a solution for certain customization tasks.

Overriding Layouts

Page 19: Magento 2 theming - knowledge sharing session by suman kc

LayoutInstruction, extending, overriding and Customizations

3

Page 20: Magento 2 theming - knowledge sharing session by suman kc

◉NO local.xml file - to modify, make a new xml file with same name

◉All attributes & definitions from origianl module will be inherited

◉move action - great feature of new XML◉Structural & content blocks to

containers & blocks

Layouts

Page 21: Magento 2 theming - knowledge sharing session by suman kc

◉ Common layout instructions to customize layoutLayout instructions

◉<block>◉<container>◉<before> &

<after>◉<action>◉<referenceBlock>

and <referenceContainer>

◉<move>◉<remove>◉<update>◉<argument>

Page 22: Magento 2 theming - knowledge sharing session by suman kc

◉ Container is a structure without content which holds other blocks and containers

◉ You can specify HTML attributes...

◉ When extending, we have referenceBlock & referenceContainer

Containers and blocks

Page 23: Magento 2 theming - knowledge sharing session by suman kc

◉ Updates in <referenceBlock> & <referenceContainer> are applied to the corresponding <block> or <container>.

◉ Eg. if you make a reference by <referenceBlock name=”right”>, you are targeting the block <block name=”right”>

◉ Attributes = remove & display, values = true/false

◉ <referenceBlock name="block.name" remove="true" />

<referenceBlock> & <referenceContainer>

Page 24: Magento 2 theming - knowledge sharing session by suman kc

◉ <move> helps moving elements to other location without the need to unset or removing from one place

<move element="name.of.an.element" destination="name.of.destination.element" as="new_alias" after="name.of.element.after" before="name.of.element.before"/>

◉ <remove> to remove static resource linked in a page <head and to remove blocks & containers

<head> <!-- Remove local resources --> <remove src="css/styles-m.css" />

<move> & <remove>

Page 25: Magento 2 theming - knowledge sharing session by suman kc

Extend a layout

Page 26: Magento 2 theming - knowledge sharing session by suman kc

Overriding layouts

Page 27: Magento 2 theming - knowledge sharing session by suman kc

Overriding layouts (from parent theme)

Page 28: Magento 2 theming - knowledge sharing session by suman kc

1. Set page layout<page layout="2columns-left" xmlns:xsi="" xsi:noNamespaceSchemaLocation="">...</page>

2. Include/remove static resources<page xmlns:xsi="" xsi:noNamespaceSchemaLocation=""> <head> <!-- Add local resources --> <css src="css/my-styles.css"/>

<css src="css/ie-9.css" ie_condition="IE 9" />

<remove src="my-js.js"

Layout Customization examples

Page 29: Magento 2 theming - knowledge sharing session by suman kc

3. Create a container - Reference a container<container name="some.container" as="someContainer" label="Some Container" htmlTag="div" htmlClass="some-container" />

<referenceContainer name="header.panel"> <block class="Magento\Framework\View…”>....</block>

4. Create a block - Reference block<block class="Magento\Catalog\Block\Product\View\Description" name="product.info.sku" template="product/view/attribute.phtml" after="product.info.type"> <arguments>...</arguments></block>

<referenceBlock name="logo">...</referenceBlock>

Layout Customization examples

Page 30: Magento 2 theming - knowledge sharing session by suman kc

TemplatesBasic concepts, customizations & overriding

4

Page 31: Magento 2 theming - knowledge sharing session by suman kc

◉ Template hint - to locate template(store> config > advanced > developer > debug > enable template)

◉ Copy template to your theme following convention◉ Make required changes

For eg. form inside <Magento_Review_module_dir>/view/frontend/templates/form.phtml

app/design/frontend/OrangeCo/orange/Magento_Review/templates

Template customization walkthrough

Page 32: Magento 2 theming - knowledge sharing session by suman kc

◉ Templates are usually initiated in layout files. ◉ Each layout block has an associated template.

◉ The template is specified in the template attribute of the layout instruction. For example, from <Magento_Catalog_module_dir>/view/frontend/layout/catalog_category_view.xml:

<block class="Magento\Catalog\Block\Category\View" name="category.image" template="Magento_Catalog::category/image.phtml"/>

Template initiation

Page 33: Magento 2 theming - knowledge sharing session by suman kc

Templates are stored in the following locations:● Module templates:

<module_dir>/view/frontend/templates/<path_to_templates>

● Theme templates: <theme_dir>/<Namespace>_<Module>/templates/<path_to_templates>

Conventional template location

Page 34: Magento 2 theming - knowledge sharing session by suman kc

Customize email templates using theme◉ Template fallback is supported for email templates◉ For eg, to override the New Order email template, create a

template named order_new.html in the <theme_dir>/Magento_Sales/email

◉ Customize using Magento Admin◉ In the Magento Admin, navigate to MARKETING > Communications >

Email Templates◉ Click Add New Template.

Customizing email templates

Page 35: Magento 2 theming - knowledge sharing session by suman kc

CSSInclude, preprocessor & Magento UI Library

5

Page 36: Magento 2 theming - knowledge sharing session by suman kc

In the Magento application, CSS files are included in layout files.◉ Include css in

<Magento_Blank_theme_dir>/Magento_Theme/layout/default_head_blocks.xml

◉ <page xmlns:xsi="" xsi:noNamespaceSchemaLocation=""> <head> <css src="css/styles-m.css" />

◉ Module-specific styles - /<Namespace>_<Module>/web/css

◉ web/css - styles-m.less, styles-l.less, _styles.less

Include CSS

Page 37: Magento 2 theming - knowledge sharing session by suman kc

how stylesheets are preprocessed and compiled to CSS◉ Server-side LESS compilation

default compilation mode, only option for production mode, Compilation performed server, using the LESS PHP library.

◉ Client-side LESS compilationWhen your application is not in the production mode, you can set Magento to compile .less files in a browseBackend : Stores > Configuration > ADVANCED > Developer [Store View drop-down field,

select Default Config.] Front-end development workflow, in the Workflow type

Preprocessor (LESS)

Page 38: Magento 2 theming - knowledge sharing session by suman kc

The Magento UI library is a flexible LESS-based frontend library designed to assist Magento theme developers● actions-toolbar● breadcrumbs● buttons● drop-downs● Forms.. And so on

Magento UI Library

Page 39: Magento 2 theming - knowledge sharing session by suman kc

You can find the Magento UI library under lib/web/css.Magento UI Library location

Page 40: Magento 2 theming - knowledge sharing session by suman kc

Any questions ?Appreciate your participation !!

Thanks!

Page 41: Magento 2 theming - knowledge sharing session by suman kc

◉ Magento 2 frontend developers guide◉ Sessiondigital & Inchoo articles

Credit◉Presentation template by SlidesCarnival◉Prepared slides using Google Slides

Frontend Demo: https://iwdagency.com/magento2/Backend Demo: https://iwdagency.com/magento2/admin/User: demoPass: demo123

Reference