SlideShare a Scribd company logo
HTML Templating using ClearSilver Paul Wayper CLUG Programming SIG
Why HTML Templates?
Why templates? Separate content from code Good for big design projects: Web designers do web design Coders write code
Why templates? Separate content from code Works for small projects too: Easier to make sure the HTML validates
Why templates? Separate content from code Works for small projects too: Easier to make sure the HTML validates Nicer layout of HTML How you want it, not how the module writer wanted it
Why templates? Separate content from code Works for small projects too: Easier to make sure the HTML validates Nicer layout of HTML Makes 'skins' really easy Simply load a different template for the same data
Why templates? Separate content from code Works for small projects too: Easier to make sure the HTML validates Nicer layout of HTML Makes 'skins' really easy Easy to understand how the page comes together Where's the function that causes DIVs to come out doubled?
Why templates? Separate content from code Works for small projects too: Easier to make sure the HTML validates Nicer layout of HTML Makes 'skins' really easy Easy to understand how the page comes together Can be used to generate static HTML pages Mix CSV -> Mix Template -> HTML -> Edit -> put on website
Why  ClearSilver ?
Why ClearSilver? Fast Code originally written for OneList / Yahoo Groups Used on Google Groups, Discogs, Orkut, trac...
Why ClearSilver? Fast Language Neutral Parser written in C Bindings for C++, Python, Perl, Ruby, Java
Why ClearSilver? Fast Language Neutral Simple syntax <?cs var:doc.title ?>
Why ClearSilver? Fast Language Neutral Simple syntax Editable in any HTML editor <?cs var:doc.title ?>
Why ClearSilver? Fast Language Neutral Simple syntax Data loaded from file or by code Template: index.cs Data: index.hdf
Why ClearSilver? Fast Language Neutral Simple syntax Data loaded from file or by code Template: index.cs Data: index.hdf Rendered by Apache/other module directly
Show us the template!
ClearSilver Templates Variables <?cs var:doc.title ?>
ClearSilver Templates Variables Variables can be used inside HTML tags <a href=”<?cs var:doc.url ?>”>
ClearSilver Templates Variables Include other files <?cs include:”footer.cs” ?>
ClearSilver Templates Variables Include other files If/Then <div class=”userlogin”><p> <?cs if:user.logged_in ?> Hello, <?cs var:user.name ?>! </cs /if ?> </div>
ClearSilver Templates Variables Include other files If/Then/Else <div class=”userlogin”><p> <?cs if:user.logged_in ?> Hello, <?cs var:user.name ?>! </cs else ?> <!-- display log in form --> </cs /if ?> </div>
ClearSilver Templates Variables Include other files If/Then/Else – if set, display, otherwise... <?cs alt:page.name ?>No page name set.<?cs /alt ?>
ClearSilver Templates Variables Include other files If/Then/Else Looping <?cs loop:i = 1, 10, 2 ?><?cs var:i ?><?cs /loop ?>
ClearSilver Templates Variables Include other files If/Then/Else Looping <?cs loop:i = 1, 10, “-2” ?><?cs var:i ?><?cs /loop ?> Loops are guaranteed to be finite
ClearSilver Templates Variables Include other files If/Then/Else Looping through data structures <?cs each:cd = cds ?><?cs var:cd.name ?><?cs /each ?>
ClearSilver Templates Variables Include other files If/Then/Else Looping (through data structures) Instances of data structures <?cs with:cd = cds.0 ?><?cs var:cd.name ?><?cs /with ?>
ClearSilver Templates Variables Include other files If/Then/Else Looping (through data structures) Instances of data structures Macros <?cs def:show_cd(cd) ?><?cs var:cd.name ?><?cs /def ?> <?cs each:cd in cds ?><?cs call:show_cd(cd) ?><?cs /each ?>
Show us the data!
ClearSilver Data Objects HDF – Hierarchical Data Format
ClearSilver Data Objects HDF – Hierarchical Data Format Key = Value pairs page.name = Welcome to linux.conf.au!
ClearSilver Data Objects HDF – Hierarchical Data Format Key = Value pairs Where's the 'hierarchical' bit?
ClearSilver Data Objects HDF – Hierarchical Data Format Key = Value pairs Where's the 'hierarchical' bit? page.name = Welcome to linux.conf.au! page.name.fgcol = darkblue page.name.bgcol = white
ClearSilver Data Objects HDF – Hierarchical Data Format Key = Value pairs Where's the 'hierarchical' bit? Page { name = Welcome to linux.conf.au! name.fgcol = darkblue name.bgcol = white }
ClearSilver Data Objects HDF – Hierarchical Data Format Key = Value pairs Dots in names make it hierarchical Data sets: Menu { 0 { Name = About } 1 { Name = Products } }
ClearSilver Data Objects HDF – Hierarchical Data Format Key = Value pairs Dots in names make it hierarchical Data sets: Menu { 0 { Name = About } 1 { Name = Products } } <table><tr> <?cs each:item = Menu ?> <td> <?cs var:item.Name ?> </td> <?cs /each ?> </tr></table>
ClearSilver Data Objects HDF – Hierarchical Data Format Data in text files: Menu { 0 { Name = About } 1 { Name = Products } }
ClearSilver Data Objects HDF – Hierarchical Data Format Data from script: my $hdf  = ClearSilver::HDF->new; $hdf ->setValue(“Menu.0.Name”,”About”); $hdf ->setValue(“Menu.1.Name”,”Products);
ClearSilver Data Objects HDF – Hierarchical Data Format Data from script from file: my $hdf = ClearSilver::HDF->new; $hdf ->readFile(“defaults.hdf”);
Put it all together!
Complete example my $hdf = ClearSilver::HDF->new; $hdf->readFile(“defaults.hdf”); my $cs = ClearSilver::CS->new($hdf); $cs->parseFile(“default.cs”); print $cs->Render();
It can't be that simple!?
Things I've learnt - HDF Absence of a data value = false
Things I've learnt - HDF Absence of a data value = false <?cs if:cds.0.title ?> <h1>CD listing:</h1> <?cs each:cd = cds ?> ... <?cs /each ?> <?cs else ?> <p class=”error”>There are no CDs.</p> <?cs /if ?>
Things I've learnt - HDF Absence of a data value = false You can test on name
Things I've learnt - HDF Absence of a data value = false You can test on name <?cs each:cd = cds ?> <p class=”<?cs if:name(cd) % 2 ?>oddrow<?cs else ?>evenrow<?cs /if ?>”> <?cs var:cd.name ?> </p> <?cs /each ?>
Things I've learnt - CS Row fetch -> put into HDF key/value pairs -> key/value pairs
Things I've learnt - CS Row fetch -> put into HDF get_data($sql) Executes query Iterates through rows Row fetch -> put into HDF
Things I've learnt - CS get_data($sql, @params) Prepares query Executes query with params Iterates through rows Row fetch -> put into HDF
Things I've learnt - CS get_data($sql, $xfrm_href, @params) Prepares query Executes query with params Iterates through rows Row fetch -> put into HDF Iterates through keys of xfrm_href Sets row key as return of xfrm function
Things I've learnt - CS get_data($sql, $xfrm_href, @params) Prepares query Executes query with params Set blank $state_href; Iterates through rows Row fetch -> put into HDF Iterates through keys of xfrm_href Sets row key as return of xfrm function gets $row_ref, $rowcount, $state_href
Things I've learnt - CS get_data($sql, $xfrm_href, @params) set_navigation($table, $sort_field, $curr_id) Retrieves first, previous, next and last rows from table based on current position
Things I've learnt - CS get_data($sql, $xfrm_href, @params) set_navigation($table, $sort_field, $curr_id) Retrieves first, previous, next and last rows from table based on current position Independent of table primary key
Things I've learnt - CS get_data($sql, $xfrm_href, @params) set_navigation($table, $sort_field, $curr_id) Retrieves first, previous, next and last rows from table based on current position Independent of table primary key select min($sort_field), 'frst' from $table union select max($sort_field), 'prev' from $table where $sort_field < ? union select min($sort_field), 'next' from $table where $sort_field > ? union select max($sort_field), 'last' from $table
Things I've learnt - CS get_data($sql, $xfrm_href, @params) set_navigation($table, $sort_field, $curr_id) Retrieves first, previous, next and last rows from table based on current position Independent of table primary key select min($sort_field), 'frst' from $table union select max($sort_field), 'prev' from $table where $sort_field < ? union select min($sort_field), 'next' from $table where $sort_field > ? union select max($sort_field), 'last' from $table  Must handle edge cases in CS and HDF
Where do we get it?
http://www.clearsilver.net

More Related Content

PDF
DBIx::Class beginners
PPT
Xslate sv perl-2013-7-11
KEY
Apache Velocity 1.6
PDF
DBIx::Class introduction - 2010
ODP
PPTX
Quick beginner to Lower-Advanced guide/tutorial in PHP
PPT
LPW: Beginners Perl
PPT
Intro to php
DBIx::Class beginners
Xslate sv perl-2013-7-11
Apache Velocity 1.6
DBIx::Class introduction - 2010
Quick beginner to Lower-Advanced guide/tutorial in PHP
LPW: Beginners Perl
Intro to php

What's hot (20)

PDF
Current state-of-php
PDF
PHP Arrays - indexed and associative array.
PPT
Introducation to php for beginners
PDF
Data Types In PHP
PPTX
Phphacku iitd
PPT
Intro to PHP
PPTX
PHP for hacks
KEY
Introduction to Perl Best Practices
PPT
PHP variables
PDF
WordPress London 16 May 2012 - You don’t know query
PPT
Php mysql
PPT
ODP
Extending Zend Framework
PPTX
You don’t know query - WordCamp UK Edinburgh 2012
PDF
Php Tutorials for Beginners
PDF
PHP 5.3 Overview
PPTX
Php server variables
ODP
What's new, what's hot in PHP 5.3
Current state-of-php
PHP Arrays - indexed and associative array.
Introducation to php for beginners
Data Types In PHP
Phphacku iitd
Intro to PHP
PHP for hacks
Introduction to Perl Best Practices
PHP variables
WordPress London 16 May 2012 - You don’t know query
Php mysql
Extending Zend Framework
You don’t know query - WordCamp UK Edinburgh 2012
Php Tutorials for Beginners
PHP 5.3 Overview
Php server variables
What's new, what's hot in PHP 5.3
Ad

Similar to HTML Templates Using Clear Silver (20)

PPT
Synapseindia reviews sharing intro on php
PPT
Synapseindia reviews sharing intro on php
PPT
Synapseindia reviews on array php
PPT
Php course-in-navimumbai
PPTX
PDF
Open source programming
PPTX
WEB DEVELOPMENT
DOC
php&mysql with Ethical Hacking
PPT
Create a web-app with Cgi Appplication
DOC
PHP Classroom Training
PDF
Programming PHP 4th Edition Peter Macintyre
PPT
Php introduction
PPT
PPT
6 3 tier architecture php
PPT
PHP Workshop Notes
PDF
Getting started with php
PPT
Synapse india reviews on php website development
PPT
Php classes in mumbai
PDF
Yapc10 Cdt World Domination
PPT
Open Source Package PHP & MySQL
Synapseindia reviews sharing intro on php
Synapseindia reviews sharing intro on php
Synapseindia reviews on array php
Php course-in-navimumbai
Open source programming
WEB DEVELOPMENT
php&mysql with Ethical Hacking
Create a web-app with Cgi Appplication
PHP Classroom Training
Programming PHP 4th Edition Peter Macintyre
Php introduction
6 3 tier architecture php
PHP Workshop Notes
Getting started with php
Synapse india reviews on php website development
Php classes in mumbai
Yapc10 Cdt World Domination
Open Source Package PHP & MySQL
Ad

Recently uploaded (20)

PDF
Empathic Computing: Creating Shared Understanding
PDF
Encapsulation theory and applications.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Big Data Technologies - Introduction.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
KodekX | Application Modernization Development
PDF
Review of recent advances in non-invasive hemoglobin estimation
Empathic Computing: Creating Shared Understanding
Encapsulation theory and applications.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
MIND Revenue Release Quarter 2 2025 Press Release
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
NewMind AI Weekly Chronicles - August'25 Week I
Unlocking AI with Model Context Protocol (MCP)
Building Integrated photovoltaic BIPV_UPV.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Programs and apps: productivity, graphics, security and other tools
Mobile App Security Testing_ A Comprehensive Guide.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Big Data Technologies - Introduction.pptx
20250228 LYD VKU AI Blended-Learning.pptx
cuic standard and advanced reporting.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
KodekX | Application Modernization Development
Review of recent advances in non-invasive hemoglobin estimation

HTML Templates Using Clear Silver

  • 1. HTML Templating using ClearSilver Paul Wayper CLUG Programming SIG
  • 3. Why templates? Separate content from code Good for big design projects: Web designers do web design Coders write code
  • 4. Why templates? Separate content from code Works for small projects too: Easier to make sure the HTML validates
  • 5. Why templates? Separate content from code Works for small projects too: Easier to make sure the HTML validates Nicer layout of HTML How you want it, not how the module writer wanted it
  • 6. Why templates? Separate content from code Works for small projects too: Easier to make sure the HTML validates Nicer layout of HTML Makes 'skins' really easy Simply load a different template for the same data
  • 7. Why templates? Separate content from code Works for small projects too: Easier to make sure the HTML validates Nicer layout of HTML Makes 'skins' really easy Easy to understand how the page comes together Where's the function that causes DIVs to come out doubled?
  • 8. Why templates? Separate content from code Works for small projects too: Easier to make sure the HTML validates Nicer layout of HTML Makes 'skins' really easy Easy to understand how the page comes together Can be used to generate static HTML pages Mix CSV -> Mix Template -> HTML -> Edit -> put on website
  • 10. Why ClearSilver? Fast Code originally written for OneList / Yahoo Groups Used on Google Groups, Discogs, Orkut, trac...
  • 11. Why ClearSilver? Fast Language Neutral Parser written in C Bindings for C++, Python, Perl, Ruby, Java
  • 12. Why ClearSilver? Fast Language Neutral Simple syntax <?cs var:doc.title ?>
  • 13. Why ClearSilver? Fast Language Neutral Simple syntax Editable in any HTML editor <?cs var:doc.title ?>
  • 14. Why ClearSilver? Fast Language Neutral Simple syntax Data loaded from file or by code Template: index.cs Data: index.hdf
  • 15. Why ClearSilver? Fast Language Neutral Simple syntax Data loaded from file or by code Template: index.cs Data: index.hdf Rendered by Apache/other module directly
  • 16. Show us the template!
  • 17. ClearSilver Templates Variables <?cs var:doc.title ?>
  • 18. ClearSilver Templates Variables Variables can be used inside HTML tags <a href=”<?cs var:doc.url ?>”>
  • 19. ClearSilver Templates Variables Include other files <?cs include:”footer.cs” ?>
  • 20. ClearSilver Templates Variables Include other files If/Then <div class=”userlogin”><p> <?cs if:user.logged_in ?> Hello, <?cs var:user.name ?>! </cs /if ?> </div>
  • 21. ClearSilver Templates Variables Include other files If/Then/Else <div class=”userlogin”><p> <?cs if:user.logged_in ?> Hello, <?cs var:user.name ?>! </cs else ?> <!-- display log in form --> </cs /if ?> </div>
  • 22. ClearSilver Templates Variables Include other files If/Then/Else – if set, display, otherwise... <?cs alt:page.name ?>No page name set.<?cs /alt ?>
  • 23. ClearSilver Templates Variables Include other files If/Then/Else Looping <?cs loop:i = 1, 10, 2 ?><?cs var:i ?><?cs /loop ?>
  • 24. ClearSilver Templates Variables Include other files If/Then/Else Looping <?cs loop:i = 1, 10, “-2” ?><?cs var:i ?><?cs /loop ?> Loops are guaranteed to be finite
  • 25. ClearSilver Templates Variables Include other files If/Then/Else Looping through data structures <?cs each:cd = cds ?><?cs var:cd.name ?><?cs /each ?>
  • 26. ClearSilver Templates Variables Include other files If/Then/Else Looping (through data structures) Instances of data structures <?cs with:cd = cds.0 ?><?cs var:cd.name ?><?cs /with ?>
  • 27. ClearSilver Templates Variables Include other files If/Then/Else Looping (through data structures) Instances of data structures Macros <?cs def:show_cd(cd) ?><?cs var:cd.name ?><?cs /def ?> <?cs each:cd in cds ?><?cs call:show_cd(cd) ?><?cs /each ?>
  • 28. Show us the data!
  • 29. ClearSilver Data Objects HDF – Hierarchical Data Format
  • 30. ClearSilver Data Objects HDF – Hierarchical Data Format Key = Value pairs page.name = Welcome to linux.conf.au!
  • 31. ClearSilver Data Objects HDF – Hierarchical Data Format Key = Value pairs Where's the 'hierarchical' bit?
  • 32. ClearSilver Data Objects HDF – Hierarchical Data Format Key = Value pairs Where's the 'hierarchical' bit? page.name = Welcome to linux.conf.au! page.name.fgcol = darkblue page.name.bgcol = white
  • 33. ClearSilver Data Objects HDF – Hierarchical Data Format Key = Value pairs Where's the 'hierarchical' bit? Page { name = Welcome to linux.conf.au! name.fgcol = darkblue name.bgcol = white }
  • 34. ClearSilver Data Objects HDF – Hierarchical Data Format Key = Value pairs Dots in names make it hierarchical Data sets: Menu { 0 { Name = About } 1 { Name = Products } }
  • 35. ClearSilver Data Objects HDF – Hierarchical Data Format Key = Value pairs Dots in names make it hierarchical Data sets: Menu { 0 { Name = About } 1 { Name = Products } } <table><tr> <?cs each:item = Menu ?> <td> <?cs var:item.Name ?> </td> <?cs /each ?> </tr></table>
  • 36. ClearSilver Data Objects HDF – Hierarchical Data Format Data in text files: Menu { 0 { Name = About } 1 { Name = Products } }
  • 37. ClearSilver Data Objects HDF – Hierarchical Data Format Data from script: my $hdf = ClearSilver::HDF->new; $hdf ->setValue(“Menu.0.Name”,”About”); $hdf ->setValue(“Menu.1.Name”,”Products);
  • 38. ClearSilver Data Objects HDF – Hierarchical Data Format Data from script from file: my $hdf = ClearSilver::HDF->new; $hdf ->readFile(“defaults.hdf”);
  • 39. Put it all together!
  • 40. Complete example my $hdf = ClearSilver::HDF->new; $hdf->readFile(“defaults.hdf”); my $cs = ClearSilver::CS->new($hdf); $cs->parseFile(“default.cs”); print $cs->Render();
  • 41. It can't be that simple!?
  • 42. Things I've learnt - HDF Absence of a data value = false
  • 43. Things I've learnt - HDF Absence of a data value = false <?cs if:cds.0.title ?> <h1>CD listing:</h1> <?cs each:cd = cds ?> ... <?cs /each ?> <?cs else ?> <p class=”error”>There are no CDs.</p> <?cs /if ?>
  • 44. Things I've learnt - HDF Absence of a data value = false You can test on name
  • 45. Things I've learnt - HDF Absence of a data value = false You can test on name <?cs each:cd = cds ?> <p class=”<?cs if:name(cd) % 2 ?>oddrow<?cs else ?>evenrow<?cs /if ?>”> <?cs var:cd.name ?> </p> <?cs /each ?>
  • 46. Things I've learnt - CS Row fetch -> put into HDF key/value pairs -> key/value pairs
  • 47. Things I've learnt - CS Row fetch -> put into HDF get_data($sql) Executes query Iterates through rows Row fetch -> put into HDF
  • 48. Things I've learnt - CS get_data($sql, @params) Prepares query Executes query with params Iterates through rows Row fetch -> put into HDF
  • 49. Things I've learnt - CS get_data($sql, $xfrm_href, @params) Prepares query Executes query with params Iterates through rows Row fetch -> put into HDF Iterates through keys of xfrm_href Sets row key as return of xfrm function
  • 50. Things I've learnt - CS get_data($sql, $xfrm_href, @params) Prepares query Executes query with params Set blank $state_href; Iterates through rows Row fetch -> put into HDF Iterates through keys of xfrm_href Sets row key as return of xfrm function gets $row_ref, $rowcount, $state_href
  • 51. Things I've learnt - CS get_data($sql, $xfrm_href, @params) set_navigation($table, $sort_field, $curr_id) Retrieves first, previous, next and last rows from table based on current position
  • 52. Things I've learnt - CS get_data($sql, $xfrm_href, @params) set_navigation($table, $sort_field, $curr_id) Retrieves first, previous, next and last rows from table based on current position Independent of table primary key
  • 53. Things I've learnt - CS get_data($sql, $xfrm_href, @params) set_navigation($table, $sort_field, $curr_id) Retrieves first, previous, next and last rows from table based on current position Independent of table primary key select min($sort_field), 'frst' from $table union select max($sort_field), 'prev' from $table where $sort_field < ? union select min($sort_field), 'next' from $table where $sort_field > ? union select max($sort_field), 'last' from $table
  • 54. Things I've learnt - CS get_data($sql, $xfrm_href, @params) set_navigation($table, $sort_field, $curr_id) Retrieves first, previous, next and last rows from table based on current position Independent of table primary key select min($sort_field), 'frst' from $table union select max($sort_field), 'prev' from $table where $sort_field < ? union select min($sort_field), 'next' from $table where $sort_field > ? union select max($sort_field), 'last' from $table Must handle edge cases in CS and HDF
  • 55. Where do we get it?