PageRenderTime 32ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/doc/howtos/themes.rst

https://gitlab.com/thanhchatvn/cloud-odoo
ReStructuredText | 968 lines | 653 code | 315 blank | 0 comment | 0 complexity | f228609e0ac6ba20e5b6c4014c03e8cc MD5 | raw file
  1. :banner: banners/build_a_theme.jpg
  2. =====================
  3. Theme Tutorial
  4. =====================
  5. .. rst-class:: lead
  6. Odoo celebrates freedom. Freedom for the designer to go further and
  7. freedom for the user to customize everything according to their needs.
  8. Ready to create your own theme? Great. Here are some things you should know before you begin. This tutorial is a guide to creating an Odoo theme.
  9. .. image:: theme_tutorial_assets/img/Intro.jpg
  10. An introduction for web designers
  11. =================================
  12. If you are a web designer using Odoo for the first time, you are in the right place.
  13. This introduction will outline the basics of Odoo theme creation.
  14. .. note::
  15. Odoos team has created a framework thats powerful and easy to use. Theres no need to know special syntaxes to use this set of tools.
  16. From common CMS to Odoo
  17. -----------------------
  18. .. note::
  19. If you always think and work in the same way, youll probably get the same results. If you want something completely new, then try something different.
  20. ..
  21. Where is my header.php file?
  22. This is usually the first question from a web designer used to working with Wordpress or Joomla and coming to Odoo for the first time.
  23. .. container:: col-sm-4
  24. .. image:: theme_tutorial_assets/img/cms.jpg
  25. .. container:: col-sm-7
  26. Indeed, when using common CMSs, you have to code several files (like header.php, page.php, post.php, etc.) in order to create a basic structure for your website. With those systems, this base structure acts as a design foundation that you have to update over time to ensure compatibility within your CMS. So, even after you have spent hours coding the files, you have not even started on the design yet.
  27. This **does not** apply to creating Odoo themes.
  28. .. note::
  29. :class: col-sm-12
  30. We think that theme design should be simple (and powerful). When we created our Website Builder, we decided to start from scratch instead of relying on what already existed. This approach gave us the freedom to focus on the things that are really important for designers: styles, content and the logic behind them. No more struggling with technical stuff.
  31. Odoo default theme structure
  32. ----------------------------
  33. .. container:: col-sm-8
  34. Odoo comes with a default theme structure.
  35. It is a very basic theme that provides minimal structure and layout. When you create a new theme, you are actually extending this.
  36. Indeed its always enabled in your setup and it acts exactly like the CMSs base structure we mentioned above, except that you dont have to create or maintain it.
  37. It will upgrade automatically within your Odoo installation and, since it is included in the Website Builder module, everything is smoothly integrated by default.
  38. As a result, you are totally free to focus on design while this structure does the job of providing integrations and functionality.
  39. .. container:: col-sm-4
  40. .. image:: theme_tutorial_assets/img/def_structure.jpg
  41. .. container:: col-md-6
  42. **Main features:**
  43. * Basic layouts for pages, blog and eCommerce
  44. * Website Builder integration
  45. * Basic Snippets
  46. * Automatic Less/Sass compiling
  47. * Automatic Js and CSS minification and combination
  48. .. container:: col-md-6
  49. **Main technologies:**
  50. * Twitter Bootstrap
  51. * jQuery
  52. * jQuery UI
  53. * underscore.js
  54. Thinking "modular"
  55. ==================
  56. An Odoo theme is not a folder containing HTML or PHP files, its a modular framework written in XML. Never worked with XML files before? Dont worry, after following the tutorial, youll be able to create your first theme with only basic knowledge of HTML.
  57. Using classical web design workflows, you usually code the layout of the entire page. The result of this is a static web page. You can update the content, of course, but your client will need you to work on making even basic changes.
  58. Creating themes for Odoo is a total change of perspective. Instead of defining the complete layout for a page, you can create blocks (snippets) at let the user choose where to drag&drop them, creating the page layout on their own.
  59. We call this modular design.
  60. Imagine an Odoo theme as a list of elements and options that you have to create and style.
  61. As a designer, your goal is to style these elements in order to achieve a wonderful result, regardless of where the end user chooses to place them.
  62. Lets take a tour of our list elements:
  63. .. row
  64. .. figure:: theme_tutorial_assets/img/snippet.jpg
  65. :figclass: col-sm-6
  66. Snippets (or building-blocks)
  67. A piece of HTML code. The user will drag&drop, modify and combine them using our built-in Website Builder interface. You can define sets of options and styles for each snippet. The user will choose from them according to their needs.
  68. .. figure:: theme_tutorial_assets/img/page.jpg
  69. :figclass: col-sm-6
  70. Pages
  71. These are normal web pages, except that they will be editable by the final user and that you can define an empty area that the user can fill by dragging snippets into it.
  72. .. /row
  73. .. raw:: html
  74. <div class="clearfix themes"></div>
  75. .. figure:: theme_tutorial_assets/img/styles.jpg
  76. :figclass: col-sm-6
  77. Styles
  78. Styles are defined using standard CSS files (or Less/Sass). You can define a style as **default** or **optional**. The default styles are always active in your theme, the optional styles can be enabled or disabled by the user.
  79. .. figure:: theme_tutorial_assets/img/functionalities.jpg
  80. :figclass: col-sm-6
  81. Functionalities
  82. Thanks to Odoos modularity, everything can be personalized even more. This means there are endless possibilities for your creativity. Adding functionalities is easy and its simple to provide the end user with customizable options.
  83. .. /row
  84. Odoo's XML files, an overview
  85. -----------------------------
  86. Any Odoo XML file starts with encoding specifications.
  87. After that, you have to write your code inside a ``<data>`` tag, placed into an ``</openerp>`` tag.
  88. .. code-block:: xml
  89. [XML]
  90. <?xml version="1.0" encoding="utf-8" ?>
  91. <openerp>
  92. <data>
  93.   ## YOUR CODE HERE
  94. </data>
  95. </openerp>
  96. Almost every element and option that you create has to be placed inside a ``<template>`` tag, like in this example.
  97. .. code-block:: xml
  98. [XML]
  99. <template id="my_title" name="My title">
  100. <h1>This is an HTML block</h1>
  101. <h2 class="lead">And this is a subtitle</h2>
  102. </template>
  103. .. important::
  104. don't misunderstand what ``template`` means. A template tag only
  105. defines a piece of html code or options - but it does not
  106. necessarily coincide with a visual arrangement of elements.
  107. The previous code defines a title, but it will not be displayed
  108. anywhere because that *template* is not associated with any part of
  109. the **Odoo default structure**. In order to do that you can use
  110. **xpath**, **qWeb** or a combination of both.
  111. Keep reading the tutorial to learn to how properly extend it with your own code.
  112. Update your theme
  113. -----------------
  114. .. container:: col-sm-6
  115. Since XML files are only loaded when you install the theme, you will have to force reloading every time you make changes on an xml file.
  116. To do that, click on the Upgrade button in the modules page.
  117. .. image:: theme_tutorial_assets/img/restart.png
  118. .. container:: col-sm-5
  119. .. image:: theme_tutorial_assets/img/upgrade_module.png
  120. Create a theme module
  121. ======================
  122. Odoos themes are packaged like modules. Even if you are designing a very simple website for your company or client, you need to package the theme like an Odoo module.
  123. ``main folder``
  124. Create a folder and name it like this: ``theme_`` followed by your
  125. theme's name.
  126. ``__openerp__.py``
  127. Create an empty document and save it to your folder as
  128. ``__openerp__.py``. This will contain the configuration info for
  129. your theme.
  130. ``__init__.py``
  131. Create another empty file and name it ``__init__.py``. It's a
  132. mandatory system file. Create and leave it blank.
  133. ``views`` and ``static`` folders
  134. Create them in the main folder. In ``views`` you'll place your xml
  135. files that define your snippets, your pages and your
  136. options. ``static`` folder is the right place for your style ,
  137. images and custom js code.
  138. .. important::
  139. Use two underscore characters at the beginning
  140. and two at the end of openerp and init file names.
  141. The final result should be something like this:
  142. .. image:: theme_tutorial_assets/img/folder.jpg
  143. Edit ``__openerp__.py``
  144. -----------------------
  145. Open the ``__openerp__.py`` you created and copy/paste the following:
  146. .. code-block:: python
  147. {
  148. 'name':'Tutorial theme',
  149. 'description': 'A description for your theme.',
  150. 'version':'1.0',
  151. 'author':'Your name',
  152. 'data': [
  153. ],
  154. 'category': 'Theme/Creative',
  155. 'depends': ['website'],
  156. }
  157. Replace the first four propertys values with anything you like.
  158. These values will be used to identify your new theme in Odoos backend.
  159. The ``data`` property will contain the xml files list. Right now its empty, but we will add any new files created.
  160. ``application: True`` is mandatory.
  161. ``category`` defines your module category (always Theme) and, after a slash, the subcategory. You can use one subcategory from the Odoo Apps categories list. (https://www.odoo.com/apps/themes)
  162. ``depends`` specifies the modules needed by our theme to work properly. For our tutorial theme, we only need website. If you need blogging or eCommerce features as well, you have to add those modules too.
  163. .. code-block:: python
  164. ...
  165. 'depends': ['website', 'website_blog', 'sale'],
  166. ...
  167. Installing your theme
  168. ---------------------
  169. To install your theme, you just place your theme folder inside addons in your Odoo installation.
  170. After that, navigate to the Settings page, look for your theme and click on the install button.
  171. Structure of an Odoo page
  172. =========================
  173. An Odoo page is the visual result of a combination of 2 kind of elements, **cross-pages** and **unique**.
  174. By default, Odoo provides you with a **Header** and a **Footer** (cross-pages) and a unique main element that contains the content that makes your page unique.
  175. .. note::
  176. Cross-pages elements will be the same on every page. Unique elements are related to a specific page only.
  177. .. image:: theme_tutorial_assets/img/page_structure.jpg
  178. To inspect the default layout, simply create a new page using the
  179. Website Builder. Click on :menuselection:`Content --> New Page` and
  180. add a page name. Inspect the page using your browser.
  181. .. code-block:: html
  182. <div id=wrapwrap>
  183. <header />
  184. <main />
  185. <footer />
  186. </div>
  187. Extend the default Header
  188. -------------------------
  189. By default, Odoo header contains a responsive navigation menu and the companys logo. You can easily add new elements or style the existing one.
  190. To do so, create a **layout.xml** file in your **views** folder and add the default Odoo xml markup.
  191. .. code-block:: xml
  192. <?xml version="1.0" encoding="utf-8" ?>
  193. <openerp>
  194. <data>
  195. </data>
  196. </openerp>
  197. Create a new template into the ``<data>`` tag, copy-pasting the following
  198. code.
  199. .. code-block:: xml
  200. <!-- Customize header -->
  201. <template id="custom_header" inherit_id="website.layout" name="Custom Header">
  202. <!-- Assign an id -->
  203. <xpath expr="//div[@id='wrapwrap']/header" position="attributes">
  204. <attribute name="id">my_header</attribute>
  205. </xpath>
  206. <!-- Add an element after the top menu -->
  207. <xpath expr="//div[@id='wrapwrap']/header/div" position="after">
  208. <div class="container">
  209. <div class="alert alert-info mt16" role="alert">
  210. <strong>Welcome</strong> in our website!
  211. </div>
  212. </div>
  213. </xpath>
  214. </template>
  215. The first xpath will add the id ``my_header`` to the header. Its the best option if you want to
  216. target css rules to that element and avoid these affecting other content on the page.
  217. .. warning::
  218. Be careful replacing default elements attributes. As your theme will extend the default one,
  219. your changes will take priority in any future Odoos update.
  220. The second xpath will add a welcome message just after the navigation menu.
  221. The last step is to add layout.xml to the list of xml files used by
  222. the theme. To do that, edit your ``__openerp__.py`` file like this
  223. .. code-block:: python
  224. 'data': [ 'views/layout.xml' ],
  225. Update your theme
  226. .. image:: theme_tutorial_assets/img/restart.png
  227. Great! We successfully added an id to the
  228. header and an element after the navigation menu. These changes will be
  229. applied to each page of the website.
  230. .. image:: theme_tutorial_assets/img/after-menu.png
  231. :class: shadow-0
  232. Create a specific page layout
  233. =============================
  234. Imagine that we want to create a specific layout for a Services page.
  235. For this page, we need to add a list of services to the top and give the client the possibility of setting the rest of the pages layout using snippets.
  236. Inside your *views* folder, create a **pages.xml** file and add the
  237. default Odoo markup. Inside ``<data>`` create a ``<template>`` tag, set the
  238. ``page`` attribute to ``True`` and add your code into it.
  239. .. code-block:: xml
  240. <?xml version="1.0" encoding="utf-8" ?>
  241. <openerp>
  242. <data>
  243. <!-- === Services Page === -->
  244. <template name="Services page" id="website.services" page="True">
  245. <h1>Our Services</h1>
  246. <ul class="services">
  247. <li>Cloud Hosting</li>
  248. <li>Support</li>
  249. <li>Unlimited space</li>
  250. </ul>
  251. </template>
  252. </data>
  253. </openerp>
  254. The page title will be the template ID. In our case *Services* (from ``website.services``)
  255. We successfully created a new page layout, but we haven't told the
  256. system **how to use it**. To do that, we can use **QWeb**. Wrap the
  257. html code into a ``<t>`` tag, like in this example.
  258. .. code-block:: xml
  259. <!-- === Services Page === -->
  260. <template name="Services page" id="website.services" page="True">
  261. <t t-call="website.layout">
  262. <div id="wrap">
  263. <div class="container">
  264. <h1>Our Services</h1>
  265. <ul class="services">
  266. <li>Cloud Hosting</li>
  267. <li>Support</li>
  268. <li>Unlimited space</li>
  269. </ul>
  270. </div>
  271. </div>
  272. </t>
  273. </template>
  274. Using ``<t t-call="website.layout">`` we will extend the Odoo
  275. default page layout with our code.
  276. As you can see, we wrapped our code into two ``<div>``, one with ID ``wrap`` and the other one with class ``container``. This is to provide a minimal layout.
  277. The next step is to add an empty area that the user
  278. can fill with snippets. To achieve this, just create a ``div`` with
  279. ``oe_structure`` class just before closing the ``div#wrap`` element.
  280. .. code-block:: xml
  281. <?xml version="1.0" encoding="utf-8" ?>
  282. <openerp>
  283. <data>
  284. <!-- === Services Page === -->
  285. <template name="Services page" id="website.services" page="True">
  286. <t t-call="website.layout">
  287. <div id="wrap">
  288. <div class="container">
  289. <h1>Our Services</h1>
  290. <ul class="services">
  291. <li>Cloud Hosting</li>
  292. <li>Support</li>
  293. <li>Unlimited space</li>
  294. </ul>
  295. <!-- === Snippets' area === -->
  296. <div class="oe_structure" />
  297. </div>
  298. </div>
  299. </t>
  300. </template>
  301. </data>
  302. </openerp>
  303. .. tip::
  304. You can create as many snippet areas as you like and place them anywhere in your pages.
  305. Our page is almost ready. Now all we have to do is add **pages.xml** in our **__openerp__.py** file
  306. .. code-block:: python
  307. 'data': [
  308. 'views/layout.xml',
  309. 'views/pages.xml'
  310. ],
  311. Update your theme
  312. .. image:: theme_tutorial_assets/img/restart.png
  313. Great, our Services page is ready and youll be able to access it by navigating to ``/yourwebsite/page/services``.
  314. You will notice that it's possible to drag/drop snippets underneath the
  315. *Our Services* list.
  316. .. image:: theme_tutorial_assets/img/services_page_nostyle.png
  317. :class: shadow-0
  318. Now let's go back to our *pages.xml* and, after our page template,
  319. copy/paste the following code.
  320. .. code-block:: xml
  321. <record id="services_page_link" model="website.menu">
  322. <field name="name">Services</field>
  323. <field name="url">/page/services</field>
  324. <field name="parent_id" ref="website.main_menu" />
  325. <field name="sequence" type="int">99</field>
  326. </record>
  327. This code will add a link to the main menu.
  328. .. image:: theme_tutorial_assets/img/services_page_menu.png
  329. :class: shadow-0
  330. The **sequence** attribute defines the links position in the top menu.
  331. In our example, we set the value to ``99`` in order to place it last. I you want to place it in a particular position, you have to replace the value according to your needs.
  332. As you can see inspecting the *data.xml* file in the ``website`` module, the **Home** link is set to ``10`` and the **Contact** us one is set to ``60`` by default.
  333. If, for example, you want to place your link in the **middle**, you can set your links sequence value to ``40``.
  334. Add Styles
  335. ==========
  336. Odoo includes Bootstrap by default. This means that you can take advantage of all Bootstrap styles and layout functionalities out of the box.
  337. Of course Bootstrap is not enough if you want to provide a unique design. The following steps will guide you through how to add custom styles to your theme.
  338. The final result won't be pretty, but will provide you with enough information to build upon on your own.
  339. Lets start by creating an empty file called **style.less** and place it in a folder called **less** in your static folder.
  340. The following rules will style our *Services* page. Copy and paste it, then save the file.
  341. .. code-block:: css
  342. .services {
  343. background: #EAEAEA;
  344. padding: 1em;
  345. margin: 2em 0 3em;
  346. li {
  347. display: block;
  348. position: relative;
  349. background-color: #16a085;
  350. color: #FFF;
  351. padding: 2em;
  352. text-align: center;
  353. margin-bottom: 1em;
  354. font-size: 1.5em;
  355. }
  356. }
  357. Our file is ready but it is not included in our theme yet.
  358. Lets navigate to the view folder and create an XML file called *assets.xml*. Add the default Odoo xml markup and copy/paste the following code. Remember to replace ``theme folder`` with your themes main folder name.
  359. .. code-block:: xml
  360. <template id="mystyle" name="My style" inherit_id="website.assets_frontend">
  361. <xpath expr="link[last()]" position="after">
  362. <link href="/theme folder/static/less/style.less" rel="stylesheet" type="text/less"/>
  363. </xpath>
  364. </template>
  365. We just created a template specifying our less file. As you can see,
  366. our template has a special attribute called ``inherit_id``. This
  367. attribute tells Odoo that our template is referring to another one in
  368. order to operate.
  369. In this case, we are referring to ``assets_frontend`` template,
  370. located in the ``website`` module. ``assets_frontend`` specifies the
  371. list of assets loaded by the website builder and our goal is to add
  372. our less file to this list.
  373. This can be achieved using xpath with the attributes
  374. ``expr="link[last()]"`` and ``position="after"``, which means "*take my
  375. style file and place it after the last link in the list of the
  376. assets*".
  377. Placing it after the last one, we ensure that our file will
  378. be loaded at the end and take priority.
  379. Finally add **assets.xml** in your **__openerp__.py** file.
  380. Update your theme
  381. .. image:: theme_tutorial_assets/img/restart.png
  382. Our less file is now included in our theme, it will be automatically compiled, minified and combined with all Odoos assets.
  383. .. image:: theme_tutorial_assets/img/services_page_styled.png
  384. :class: shadow-0
  385. Create Snippets
  386. ===============
  387. Since snippets are how users design and layout pages, they are the most important element of your design.
  388. Lets create a snippet for our Service page. The snippet will display three testimonials and it will be editable by the end user using the Website Builder UI.
  389. Navigate to the view folder and create an XML file called **snippets.xml**.
  390. Add the default Odoo xml markup and copy/paste the following code.
  391. The template contains the HTML markup that will be displayed by the snippet.
  392. .. code-block:: xml
  393. <template id="snippet_testimonial" name="Testimonial snippet">
  394. <section class="snippet_testimonial">
  395. <div class="container text-center">
  396. <div class="row">
  397. <div class="col-md-4">
  398. <img alt="client" class="img-circle" src="/theme_tutorial/static/src/img/client_1.jpg"/>
  399. <h3>Client Name</h3>
  400. <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
  401. </div>
  402. <div class="col-md-4">
  403. <img alt="client" class="img-circle" src="/theme_tutorial/static/src/img/client_2.jpg"/>
  404. <h3>Client Name</h3>
  405. <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
  406. </div>
  407. <div class="col-md-4">
  408. <img alt="client" class="img-circle" src="/theme_tutorial/static/src/img/client_3.jpg"/>
  409. <h3>Client Name</h3>
  410. <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
  411. </div>
  412. </div>
  413. </div>
  414. </section>
  415. </template>
  416. As you can see, we used Bootstrap default classes for our three columns. Its not just about layout, these classes **will be triggered by the Website Builder to make them resizable by the user**.
  417. The previous code will create the snippets content, but we still need to place it into the editor bar, so the user will be able to drag&drop it into the page. Copy/paste this template in your **snippets.xml** file.
  418. .. code-block:: xml
  419. <template id="place_into_bar" inherit_id="website.snippets" name="Place into bar">
  420. <xpath expr="//div[@id='snippet_content']/div[@class='o_panel_body']" position="inside">
  421. <t t-snippet="theme_tutorial.snippet_testimonial"
  422. t-thumbnail="/theme_tutorial/static/src/img/ui/snippet_thumb.jpg"/>
  423. </xpath>
  424. </template>
  425. .. rst-class:: col-sm-6
  426. Using xpath, we are targeting a particular element with id
  427. ``snippet_structure``. This means that the snippet will appear in the
  428. Structure tab. If you want to change the destination tab, you have just to replace the ``id`` value in the xpath expression.
  429. .. image:: theme_tutorial_assets/img/snippet_bar.png
  430. :class: col-sm-6 shadow-0
  431. ============ ==================================
  432. Tab Name Xpath expression
  433. ============ ==================================
  434. Structure ``//div[@id='snippet_structure']``
  435. Content ``//div[@id='snippet_content']``
  436. Feature ``//div[@id='snippet_feature']``
  437. Effect ``//div[@id='snippet_effect']``
  438. ============ ==================================
  439. The ``<t>`` tag will call our snippet's template and will assign a thumbnail placed in the img folder.
  440. You can now drag your snippet from the snippet bar, drop it in your page and see the result.
  441. .. image:: theme_tutorial_assets/img/snippet_default.png
  442. Snippet options
  443. ===============
  444. Options allow publishers to edit a snippets appearance using the Website Builders UI.
  445. Using Website Builder functionalities, you can create snippet options easily and automatically add them to the UI.
  446. Options group properties
  447. -------------------------
  448. Options are wrapped in groups. Groups can have properties that define how the included options will interact with the user interface.
  449. ``data-selector=" css selector(s) "``
  450. Bind all the options included into the group to a particular element.
  451. ``data-js=" custom method name "``
  452. Is used to bind custom Javascript methods.
  453. ``data-drop-in=" css selector(s) "``
  454. Defines the list of elements where the snippet can be dropped into.
  455. ``data-drop-near=" css selector(s) "``
  456. Defines the list of elements that the snippet can be dropped beside.
  457. Default option methods
  458. -----------------------
  459. Options apply standard CSS classes to the snippet. Depending on the method that you choose, the UI will behave differently.
  460. ``data-select_class=" class name "``
  461. More data-select_class in the same group defines a list of classes that the user can choose to apply. Only one option can be enabled at a time.
  462. ``data-toggle_class=" class name "``
  463. The data-toggle_class is used to apply one or more CSS classes from the list to a snippet. Multiple selections can be applied at once.
  464. Let's demonstrate how default options work with a basic example.
  465. We start by adding a new file in our views folder - name it **options.xml** and add the default Odoo XML markup. Create a new template copy/pasting the following
  466. .. code-block:: xml
  467. <template id="snippet_testimonial_opt" name="Snippet Testimonial Options" inherit_id="website.snippet_options">
  468. <xpath expr="//div[@data-js='background']" position="after">
  469. <div data-selector=".snippet_testimonial"> <!-- Options group -->
  470. <li class="dropdown-submenu">
  471. <a href="#">Your Option</a>
  472. <ul class="dropdown-menu"> <!-- Options list -->
  473. <li data-select_class="opt_shadow"><a>Shadow Images</a></li>
  474. <li data-select_class="opt_grey_bg"><a>Grey Bg</a></li>
  475. <li data-select_class=""><a>None</a></li>
  476. </ul>
  477. </li>
  478. </div>
  479. </xpath>
  480. </template>
  481. .. note::
  482. The previous template will inherit the default **snippet_options template** adding our options after the **background** options (xpath expr attribute).
  483. To place your options in a particular order, inspect the **snippet_options template** from the **website module** and add your options before/after the desired position.
  484. As you can see, we wrapped all our options inside a DIV tag that will
  485. group our options and that will target them to the right selector
  486. (``data-selector=".snippet_testimonial"``).
  487. To define our options we applied ``data-select_class`` attributes to the
  488. ``li`` elements. When the user selects an option, the class contained in
  489. the attribute will automatically be applied to the element.
  490. Since ``select_class`` method avoids multiple selections, the last "empty"
  491. option will reset the snippet to default.
  492. Add **options.xml** to ``__openerp__.py`` and update your theme.
  493. .. image:: theme_tutorial_assets/img/restart.png
  494. Dropping our snippet onto the page, you will notice that our new options are automatically added to the customize menu. Inspecting the page, you will also notice that the class will be applied to the element when selecting an option.
  495. .. image:: theme_tutorial_assets/img/snippet_options.png
  496. Lets create some css rules in order to provide a visual feedback for our options. Open our **style.less** file and add the following
  497. .. code-block:: css
  498. .snippet_testimonial {
  499. border: 1px solid #EAEAEA;
  500. padding: 20px;
  501. }
  502. // These lines will add a default style for our snippet. Now let's create our custom rules for the options.
  503. .snippet_testimonial {
  504. border: 1px solid #EAEAEA;
  505. padding: 20px;
  506. &.opt_shadow img {
  507. box-shadow: 0 2px 5px rgba(51, 51, 51, 0.4);
  508. }
  509. &.opt_grey_bg {
  510. border: none;
  511. background-color: #EAEAEA;
  512. }
  513. }
  514. .. image:: theme_tutorial_assets/img/snippet_options2.png
  515. :class: shadow-0
  516. Great! We successfully created options for our snippet.
  517. Any time the publisher clicks on an option, the system will add the class specified in the data-select_class attribute.
  518. By replacing ``data-select_class`` with ``data-toggle_class`` you will be able to select
  519. more classes at the same time.
  520. Javascript Options
  521. ------------------
  522. ``data-select_class`` and ``data-toggle_class`` are great if you need to perform
  523. simple class change operations. But what if your snippets customization needs something more?
  524. As we said before, ``data-js`` propriety can be assigned to an options group in order to define a custom method. Lets create one for our *testimonials snippet* by adding a ``data-js`` attribute to the options group div that we created earlier.
  525. .. code-block:: xml
  526. <div data-js="snippet_testimonial_options" data-selector=".snippet_testimonial">
  527. [...]
  528. </div>
  529. Done. From now on, the Website Builder will look for a
  530. ``snippet_testimonial_options`` method each time the publisher enters in edit
  531. mode.
  532. Let's go one step further by creating a javascript file, name
  533. it **tutorial_editor.js** and place it into the **static** folder. Copy/paste
  534. the following code
  535. .. code-block:: javascript
  536. (function() {
  537. 'use strict';
  538. var website = openerp.website;
  539. website.openerp_website = {};
  540. })();
  541. Great, we successfully created our javascript editor file. This file will contain all the javascript functions used by our snippets in edit mode. Lets create a new function for our testimonial snippet using the ``snippet_testimonial_options`` method that we created before.
  542. .. code-block:: javascript
  543. (function() {
  544. 'use strict';
  545. var website = openerp.website;
  546. website.openerp_website = {};
  547. website.snippet.options.snippet_testimonial_options = website.snippet.Option.extend({
  548. on_focus: function() {
  549. alert("On focus!");
  550. }
  551. })
  552. })();
  553. As you will notice, we used a method called ``on_focus`` to trigger our function. The Website Builder provides several events you can use to trigger your custom functions.
  554. =========================== ==================================
  555. Event Description
  556. =========================== ==================================
  557. ``start`` Fires when the publisher selects the snippet for the first time in an editing session or when the snippet is drag-dropped into the page
  558. ``on_focus`` Fires each time the snippet is selected by the user or when the snippet is drag-dropped into the page.
  559. ``on_blur`` This event occurs when a snippet loses focus.
  560. ``on_clone`` Fires just after a snippet is duplicated. A new js variable is created ($clone) containing the cloned element.
  561. ``on_remove`` It occurs just before that the snippet is removed.
  562. ``drop_and_build_snippet`` Fires just after that the snippet is drag and dropped into a drop zone. When this event is triggered, the content is already inserted in the page.
  563. ``clean_for_save`` It trigger before the publisher save the page.
  564. =========================== ==================================
  565. Lets add our new javascript files to the editor assets list.
  566. Go back to **assets.xml** and create a new template like the previous one.
  567. This time we have to inherit ``assets_editor`` instead of ``assets_frontend``.
  568. .. code-block:: xml
  569. <template id="my_js" inherit_id="website.assets_editor" name="My Js">
  570. <xpath expr="script[last()]" position="after">
  571. <script type="text/javascript" src="/theme_tutorial/static/src/js/tutorial_editor.js" />
  572. </xpath>
  573. </template>
  574. Update your theme
  575. .. image:: theme_tutorial_assets/img/restart.png
  576. Lets test our new javascript function. Enter in Edit mode and drop into the page.
  577. You should now see the javascript alert that we bound on the ``on_focus`` event.
  578. If you close it, then click outside of your snippet and then click in it again, the event will trigger again.
  579. .. image:: theme_tutorial_assets/img/snippet_custom_method.png
  580. :class: shadow-0
  581. Editing Reference Guide
  582. =======================
  583. Basically all the elements in a page can be edited by the publisher.
  584. Besides that, some element types and css classes will trigger special Website Builder functionalities when edited.
  585. Layout
  586. ------
  587. ``<section />``
  588. Any section element can be edited like a block of content. The publisher can move or duplicate it. Its also possible to set a background image or color. Section is the standard main container of any snippet.
  589. ``.row > .col-md-*``
  590. Any medium bootstrap columns directly descending from a .row element, will be resizable by the publisher.
  591. ``contenteditable="False"``
  592. This attribute will prevent editing to the element and all its children.
  593. ``contenteditable="True"``
  594. Apply it to an element inside a contenteditable="False" element in order to create an exception and make the element and its children editable.
  595. ``<a href=# />``
  596. In Edit Mode, any link can be edited and styled. Using the Link Modal its also possible to replace it with a button.
  597. Media
  598. -----
  599. ``<span class=fa />``
  600. Pictogram elements. Editing this element will open the Pictogram library to replace the icon. Its also possible to transform the elements using CSS.
  601. ``<img />``
  602. Once clicked, the Image Library will open and you can replace images. Transformation is also possible for this kind of element.
  603. .. code-block:: html
  604. <div class="media_iframe_video" data-src="[your url]" >
  605. <div class="css_editable_mode_display"/>
  606. <div class="media_iframe_video_size"/>
  607. <iframe src="[your url]"/>
  608. </div>
  609. This html structure will create an ``<iframe>`` element editable by the publisher.
  610. SEO best practice
  611. =================
  612. Facilitate content insertion
  613. ----------------------------
  614. Modern search engine algorithms increasingly focus on content, which means there is less focus on **keyword saturation** and more focus on whether or not the content is **actually relevant to the keywords**.
  615. As content is so important for SEO, you should concentrate on giving publishers the tools to easily insert it. It is important that your snippets are content-responsive, meaning that they should fit the publishers content regardless of size.
  616. Lets have a look to this example of a classic two column snippet, implemented in two different ways.
  617. .. container:: col-sm-7
  618. .. image:: theme_tutorial_assets/img/seo_snippet_wrong.png
  619. .. container:: col-sm-5
  620. Bad
  621. Using fixed image, the publisher will be forced to limit the text in order to follow the layout.
  622. .. container:: col-sm-7
  623. .. image:: theme_tutorial_assets/img/seo_snippet_good.png
  624. .. container:: col-sm-5
  625. Good
  626. Using background images that fit the column height, the publisher will be free to add the content regardless of the images height.
  627. Page segmentation
  628. -----------------
  629. Basically, page segmentation means that a page is divided into several separate parts and these parts are treated as separate entries by search engines.
  630. When you design pages or snippets, you should be sure to use the right tags in order to facilitate search engine indexing.
  631. ``<article>``
  632. Specifies an independent block of content. Within it should be a piece of self-contained content that should make sense on its own. You can nest ``<article>`` elements within one another. In this case, its implied that the nested elements are related to the outer ``<article>`` element.
  633. ``<header>``
  634. Indicates the header section of a self-contained block of content (an ``<article>``).
  635. ``<section>``
  636. Is the snippet default tag and it specifies a subsection of a block of content. It can be used to split ``<article>`` content into several parts. Its advisable to use a heading element (``<h1>`` ``<h6>``) to define the sections topic.
  637. ``<hgroup>``
  638. Is used to wrap a section of headings (``<h1>`` - ``<h6>``). A great example would be an article with both a headline and sub-headline at the top:
  639. .. code-block:: html
  640. <hgroup>
  641. <h1>Main Title</h1>
  642. <h2>Subheading</h2>
  643. </hgroup>
  644. Describe your page
  645. ------------------
  646. Define keywords
  647. '''''''''''''''
  648. You should use appropriate, relevant keywords and synonyms for those keywords. You can define them for each page using the built-in Promote function found in the bar at the top.
  649. Define a title and a description
  650. ''''''''''''''''''''''''''''''''
  651. Define them using the Promote function. Keep your page titles short and include the main keyword phrase for the page.
  652. Good titles evoke an emotional response, ask a question or promise something.
  653. Descriptions, while not important to search engine rankings, are extremely important in gaining user click-through. These are an opportunity to advertise content and to let people searching know exactly whether the given page contains the information they're looking for. It is important that titles and descriptions on each page are unique.