Template Examples - Toggle Switch"
Robertwenzel (talk | contribs) |
Robertwenzel (talk | contribs) |
||
Line 12: | Line 12: | ||
= Using the original markup and styles = | = Using the original markup and styles = | ||
− | A very basic way to integrate a 3rd party layout is to | + | A very basic way to integrate a 3rd party layout is to re-use the markup as-is. Here the original source html example. |
<source lang="html"> | <source lang="html"> | ||
− | |||
− | |||
<div class="settings"> | <div class="settings"> | ||
<div class="row"> | <div class="row"> | ||
Line 26: | Line 24: | ||
... | ... | ||
</div> | </div> | ||
− | |||
</source> | </source> | ||
Line 32: | Line 29: | ||
ZK's xhtml component set can be combined with ZUL components - using the xhtml namespace '''LINKME'''. | ZK's xhtml component set can be combined with ZUL components - using the xhtml namespace '''LINKME'''. | ||
+ | |||
+ | '''LINKME source file:''' | ||
<source lang="xml"> | <source lang="xml"> | ||
<zk xmlns:x="xhtml"> | <zk xmlns:x="xhtml"> | ||
− | <div sclass="settings"><!-- | + | <div sclass="settings"><!-- zul-div --> |
− | <x:div class=" | + | <x:div class="row"><!-- xhtml-div --> |
+ | <x:div class="question" textContent="Enable Cookies" /> | ||
+ | <x:div class="switch"> | ||
+ | <x:input id="cmn-toggle-1" class="cmn-toggle cmn-toggle-round" type="checkbox" checked="true" /> | ||
+ | <x:label for="cmn-toggle-1" /> | ||
+ | </x:div> | ||
+ | </x:div> | ||
+ | |||
+ | <x:div class="row"> | ||
+ | <x:div class="question" textContent="Share Location" /> | ||
<x:div class="switch"> | <x:div class="switch"> | ||
− | <x:input id="cmn-toggle- | + | <x:input id="cmn-toggle-2" class="cmn-toggle cmn-toggle-round-flat" type="checkbox" /> |
− | <x:label for="cmn-toggle- | + | <x:label for="cmn-toggle-2" /> |
</x:div> | </x:div> | ||
</x:div> | </x:div> | ||
... | ... | ||
+ | </div> | ||
+ | </zk> | ||
+ | </source> | ||
+ | |||
+ | Assume there are no surprises here, it only looks a bit repetitive. So let's make our lives a little easier writing less code using a template, for the actual switch. | ||
+ | |||
+ | '''LINKME source file:''' | ||
+ | <source lang="xml"> | ||
+ | <?component name="toggle" templateURI="template/cmn-toggle-template.zul"?> | ||
+ | |||
+ | <zk xmlns:x="xhtml"> | ||
+ | <div sclass="settings"> | ||
+ | <x:div class="row"> | ||
+ | <x:div class="question" textContent="Enable Cookies"/> | ||
+ | <toggle toggleClass="round" checked="true"/> | ||
+ | </x:div> | ||
+ | |||
+ | <x:div class="row"> | ||
+ | <x:div class="question" textContent="Share Location"/> | ||
+ | <toggle toggleClass="round-flat" /> | ||
+ | </x:div> | ||
+ | ... | ||
+ | </div> | ||
+ | </source> | ||
+ | |||
+ | Or even combine with an inline template to apply the settingsRow multiple times with different parameters: | ||
+ | |||
+ | '''LINKME source file:''' | ||
+ | <source lang="xml"> | ||
+ | <?component name="toggle" templateURI="template/cmn-toggle-template.zul"?> | ||
+ | |||
+ | <zk xmlns:x="xhtml" xmlns:n="native"> | ||
+ | <div sclass="settings"> | ||
+ | <apply template="settingsRow" question="Enable Cookies" toggleClass="round" checked="true"/> | ||
+ | <apply template="settingsRow" question="Share Location" toggleClass="round-flat"/> | ||
+ | ... | ||
+ | |||
+ | <template name="settingsRow"> | ||
+ | <n:div class="row"> | ||
+ | <n:div class="question">${question}</n:div> | ||
+ | <toggle/><!-- inherits the parameters passed into the settingsRow template --> | ||
+ | </n:div> | ||
+ | </template> | ||
</div> | </div> | ||
</zk> | </zk> |
Revision as of 09:01, 18 May 2017
Robert Wenzel, Engineer, Potix Corporation
May 2017
ZK 8.0
Introduction
The web is full of html5/css based layout examples ... e.g. this: http://callmenick.com/post/css-toggle-switch-examples
Using the original markup and styles
A very basic way to integrate a 3rd party layout is to re-use the markup as-is. Here the original source html example.
<div class="settings">
<div class="row">
<div class="switch">
<input id="cmn-toggle-1" class="cmn-toggle cmn-toggle-round" type="checkbox">
<label for="cmn-toggle-1"></label>
</div>
</div>
...
</div>
ZK's xhtml component set can be combined with ZUL components - using the xhtml namespace LINKME.
LINKME source file:
<zk xmlns:x="xhtml">
<div sclass="settings"><!-- zul-div -->
<x:div class="row"><!-- xhtml-div -->
<x:div class="question" textContent="Enable Cookies" />
<x:div class="switch">
<x:input id="cmn-toggle-1" class="cmn-toggle cmn-toggle-round" type="checkbox" checked="true" />
<x:label for="cmn-toggle-1" />
</x:div>
</x:div>
<x:div class="row">
<x:div class="question" textContent="Share Location" />
<x:div class="switch">
<x:input id="cmn-toggle-2" class="cmn-toggle cmn-toggle-round-flat" type="checkbox" />
<x:label for="cmn-toggle-2" />
</x:div>
</x:div>
...
</div>
</zk>
Assume there are no surprises here, it only looks a bit repetitive. So let's make our lives a little easier writing less code using a template, for the actual switch.
LINKME source file:
<?component name="toggle" templateURI="template/cmn-toggle-template.zul"?>
<zk xmlns:x="xhtml">
<div sclass="settings">
<x:div class="row">
<x:div class="question" textContent="Enable Cookies"/>
<toggle toggleClass="round" checked="true"/>
</x:div>
<x:div class="row">
<x:div class="question" textContent="Share Location"/>
<toggle toggleClass="round-flat" />
</x:div>
...
</div>
Or even combine with an inline template to apply the settingsRow multiple times with different parameters:
LINKME source file:
<?component name="toggle" templateURI="template/cmn-toggle-template.zul"?>
<zk xmlns:x="xhtml" xmlns:n="native">
<div sclass="settings">
<apply template="settingsRow" question="Enable Cookies" toggleClass="round" checked="true"/>
<apply template="settingsRow" question="Share Location" toggleClass="round-flat"/>
...
<template name="settingsRow">
<n:div class="row">
<n:div class="question">${question}</n:div>
<toggle/><!-- inherits the parameters passed into the settingsRow template -->
</n:div>
</template>
</div>
</zk>
Streamlining with <template>
Customize ZK's <checkbox> component
Summary
Example Sources
The code examples are available on github in the zk-template-examples repository
- zul files: https://github.com/zkoss-demo/zk-template-examples/tree/master/src/main/webapp/css-toggle-switch
- java classes: https://github.com/zkoss-demo/zk-template-examples/tree/master/src/main/java/zk/example/template/stepbar
Running the Example
Clone the repo
git clone [email protected]:zkoss-demo/zk-template-examples.git
The example war file can be built using the gradle-wrapper (on windows simply omit the prefix './'):
./gradlew war
Execute using jetty:
./gradlew appRun
Then access the example http://localhost:8080/zk-template-examples/css-toggle-switch/
Comments
Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License. |