SlideShare a Scribd company logo
1 of 30
Download to read offline
Zotonic
The Tutorial
Erlang User Conference 2013,
Stockholm
Arjan Scherpenisse - arjan@miraclethings.nl
Zotonic's goals
๎€Š
Maximise the use of hardware
๎€Š Frontender friendly
๎€Š
Hit the ground running
So, what's in the box?
๎€Š
Multiple sites
๎€Š Admin interface
๎€Š
User management
๎€Š Page management
๎€Š
Menu editor
๎€Š Commenting
๎€Š Image management
๎€Š Video embedding
๎€Š
Integrated e-mail
sending + receiving (!)
๎€Š API OAuth
๎€Š i18n
๎€Š SEO modules
๎€Š Importer modules
๎€Š Mailinglist
Getting technical
๎€Š
The data model
๎€Š HTTP request flow
๎€Š
Modules & sites
๎€Š URL dispatching
๎€Š
The building blocks
Data model: everything is a thing
ArticleArticle
PersonPerson ImageImage
author depiction
HTTP request flow
Modules
๎€Š
Modules are self-contained:
๎€Š Dispatch rules, resources, templates, css, javascript,
images, translations, template filters, actions, validators,
API services, models, โ€ฆ
๎€Š Prioritized list
๎€Š File lookup mechanism
๎€Š Every module can be a gen_server
Notification system
๎€Š Is how modules extend and offer functionality
๎€Š gen_event-like
๎€Š Listen to notifications
๎€Š Send notifications:
๎€Š notify, notify1, fold, map, first
๎€Š Both sync and async
% @doc Check if the recipient is a known user, if so redirect the received
e-mail
observe_email_received(#email_received{localpart=Recipient} = Received,
Context) ->
case m_identity:lookup_by_username(Recipient, Context) of
undefined ->
undefined;
Props ->
case proplists:get_value(is_verified, Props) of
true ->
URL dispatching
๎€Š
Dispatch rules per site
๎€Š (and per module)
๎€Š
Dispatch rules have names
๎€Š e.g. this rule serves the contact.tpl on the /contact
URL:
{contact,
[โ€œcontactโ€], controller_template, [ {template, "contact.tpl"} ]},
The page controller
๎€Š
Webmachine entrypoint for pages
๎€Š 404 check
๎€Š
Authorization check + logon redirect
๎€Š Template selection
Template system
๎€Š
Extended ErlyDTL
๎€Š Expressions: {% if a + b > c %}
๎€Š Tuple and list data types
๎€Š Data access through models: {{ m.rsc[id].title }}
๎€Š
Templates can be made more specific by category
and/or unique page name
๎€Š Templates with the same name override each
other
๎€Š Uses module priority
Template ex.: page.page_projects.tpl
{% extends "page.tpl" %}
{% block content_body %}
{% cache 7200 %}
{% for r in m.search[{archive_year cat='project'}] %}
<div class="grid_9 alpha omega">
<h2 class="year">{{ r.year }}</h2>
</div>
{% for group in m.search[{query publication_year=r.year|append:""
cat='project'
sort="-publication_start"}]|chunk:3 %}
<div class="grid_9 alpha omega">
{% for id in group %}
{% catinclude "inc/listitem.tpl" id alpha=forloop.first
omega=forloop.last%}
{% endfor %}
</div>
{% endfor %}
{% endfor %}
{% endcache %}
{% endblock %}
Scomps โ€” screen components
๎€Š
For when you need more logic than a template can
handle
๎€Š Erlang module
๎€Š Cacheable
๎€Š Example: {% menu id=id %}
Javascript โ€” Event handling
๎€Š
Ajax postbacks
๎€Š Form submits
๎€Š
Calls event/2 function
๎€Š Communicates back to browser by sending
javascript
๎€Š Inspired by Nitrogen
{% button postback={click foo=โ€barโ€} text=โ€click meโ€ %}
event({postback, {click, Args}, _Trigger, _Target}, Context) ->
z_render:wire({alert, [{text,โ€helloโ€}]}, Context).
Javascript โ€” Push connections
๎€Š
Uses either Comet or WebSockets
๎€Š Attaches to the page process
๎€Š
Transports Javascript to the UA
REST services
๎€Š
API method is an Erlang module
๎€Š /api/mymodule/mymethod
๎€Š
In /services module directory
๎€Š JSON input / output
๎€Š
Process GET and POST
Hands on!
๎€Š
Get Vagrant (version >= 1.1!)
๎€Š Clone:
git clone
https://github.com/arjan/zotonic-vagrant-tutorial.git
๎€Š Get it running
vagrant up
๎€Š โ€ฆ get some coffee...
๎€Š Visit: http://localhost:8000
๎€Š ...you can also run it on your local machine,
outside vagrant
Let's start!
Tutorial use case: Portfolio site
๎€Š
Site with information about your projects
๎€Š Each project has:
๎€Š Title, text
๎€Š Slide show of large images
๎€Š List of related projects
Easy development
๎€Š
apt-get install inotify-tools
๎€Š Enable mod_development
๎€Š
Emacs' zotonic-tpl-mode for editing templates
๎€Š โ€ฆ and of course erlang-mode / flymake-mode
๎€Š
Each step of this tutorial is a git tag! e.g.
git checkout step-0
#0 โ€“ base site
๎€Š
This is what we started with, created using:
๎€Š zotonic addsite -L -s basesite tutorial
#1 โ€“ create project category
Define the our site-specific domain model:
specific categories that this site uses.
๎€Š
In the site's module (tutorial.erl), we create
manage_schema/2
๎€Š Returns a #datamodel{} record
๎€Š When done:
๎€Š z:m(), z:flush().
๎€Š Click โ€œreinstall data modelโ€ button in the admin
#2 โ€“ create project page template
Create a page specific for resources of the
category project.
๎€Š
Template name: page.project.tpl
๎€Š Loops through the list of images
๎€Š Note: look at templates/tablet/page.tpl in mod_base_site to see the
template block names that you can use
#3 โ€“ project list
A list of recent projects, ordered by their
publication date
๎€Š The easy way:
๎€Š Create resource with category โ€œsearch queryโ€
๎€Š Search query: โ€œcat=project sort=-publication_startโ€
๎€Š Give it the page path โ€œ/projectsโ€
๎€Š This can go in your #datamodel{}
๎€Š
#3b โ€“ alternative project list
๎€Š The hard(coded) way:
๎€Š Set up new dispatch rule for /all-projects
๎€Š Create all-projects.tpl, write template code...
๎€Š (git checkout step-3b)
#4 โ€“ feedback form
A form which sends an email to the site
administrator.
๎€Š
Add form to page.project.tpl
๎€Š Create event handler in tutorial.erl
๎€Š Takes form data,
๎€Š Sends email
๎€Š Email is HTML; rendered from template
#5 โ€“ notification system
We want to make sure a project always has at
least one edge of type โ€œauthorโ€
๎€Š
create observe_rsc_update_done/2 in tutorial.erl
๎€Š Tip: Look in zotonic_notifications.hrl for more hooks into
the system!
๎€Š
Add code
๎€Š Test it in the admin!
๎€Š (reload after save to see the new edge)
Thank you!
๎€Š
Please come to my talk on friday!
๎€Š โ€œMaking it fastโ€ - the performance of Zotonic
๎€Š โ€ฆ or chat with me & Andreas :-)
๎€Š Online resources:
๎€Š http://zotonic.com
๎€Š http://zotonic.com/docs
๎€Š IRC, XMPP, mailing lists
๎€Š Tutorial source code, tutorial slides

More Related Content

What's hot

An Introduction to Ajax Programming
An Introduction to Ajax ProgrammingAn Introduction to Ajax Programming
An Introduction to Ajax Programming
hchen1
ย 
Ajax
AjaxAjax
Ajax
Tech_MX
ย 
Introduction to ajax
Introduction to ajaxIntroduction to ajax
Introduction to ajax
Venkat Pinagadi
ย 
Ajax Introduction Presentation
Ajax   Introduction   PresentationAjax   Introduction   Presentation
Ajax Introduction Presentation
thinkphp
ย 

What's hot (20)

Ajax
AjaxAjax
Ajax
ย 
Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)
ย 
Ajax
AjaxAjax
Ajax
ย 
Ajax Ppt
Ajax PptAjax Ppt
Ajax Ppt
ย 
Introduction to ajax
Introduction to ajaxIntroduction to ajax
Introduction to ajax
ย 
AJAX
AJAXAJAX
AJAX
ย 
Ajax
AjaxAjax
Ajax
ย 
Ajax
AjaxAjax
Ajax
ย 
Ajax Presentation
Ajax PresentationAjax Presentation
Ajax Presentation
ย 
Introduction to ajax
Introduction to ajaxIntroduction to ajax
Introduction to ajax
ย 
Advantages and disadvantages of an ajax based client application
Advantages and disadvantages of an ajax based client applicationAdvantages and disadvantages of an ajax based client application
Advantages and disadvantages of an ajax based client application
ย 
An Introduction to Ajax Programming
An Introduction to Ajax ProgrammingAn Introduction to Ajax Programming
An Introduction to Ajax Programming
ย 
Ajax
AjaxAjax
Ajax
ย 
Ajax Ppt 1
Ajax Ppt 1Ajax Ppt 1
Ajax Ppt 1
ย 
Introduction to ajax
Introduction to ajaxIntroduction to ajax
Introduction to ajax
ย 
Ajax.ppt
Ajax.pptAjax.ppt
Ajax.ppt
ย 
Ajax presentation
Ajax presentationAjax presentation
Ajax presentation
ย 
Ajax Introduction Presentation
Ajax   Introduction   PresentationAjax   Introduction   Presentation
Ajax Introduction Presentation
ย 
What is Ajax technology?
What is Ajax technology?What is Ajax technology?
What is Ajax technology?
ย 
Introduction to ajax
Introduction  to  ajaxIntroduction  to  ajax
Introduction to ajax
ย 

Viewers also liked

Arava Sites By Guy
Arava Sites By GuyArava Sites By Guy
Arava Sites By Guy
GZ-Israel
ย 
Alice Quick Guide
Alice Quick GuideAlice Quick Guide
Alice Quick Guide
KJSROSE
ย 
Israelair Photos 1214209899278185 8
Israelair Photos 1214209899278185 8Israelair Photos 1214209899278185 8
Israelair Photos 1214209899278185 8
GZ-Israel
ย 
Ezvc
EzvcEzvc
Ezvc
ezvc
ย 
Rami Meiri
Rami MeiriRami Meiri
Rami Meiri
GZ-Israel
ย 
Israel 1214781329718633 9
Israel 1214781329718633 9Israel 1214781329718633 9
Israel 1214781329718633 9
GZ-Israel
ย 

Viewers also liked (20)

KazooCon 2014 - Introduction to Kazoo APIs!
KazooCon 2014 - Introduction to Kazoo APIs!KazooCon 2014 - Introduction to Kazoo APIs!
KazooCon 2014 - Introduction to Kazoo APIs!
ย 
Arava Sites By Guy
Arava Sites By GuyArava Sites By Guy
Arava Sites By Guy
ย 
Alice Quick Guide
Alice Quick GuideAlice Quick Guide
Alice Quick Guide
ย 
Zotonic presentation Erlang Camp Boston, august 2011
Zotonic presentation Erlang Camp Boston, august 2011Zotonic presentation Erlang Camp Boston, august 2011
Zotonic presentation Erlang Camp Boston, august 2011
ย 
Duo Disco - doing the Erlang dance
Duo Disco - doing the Erlang danceDuo Disco - doing the Erlang dance
Duo Disco - doing the Erlang dance
ย 
IPsec
IPsecIPsec
IPsec
ย 
Israelair Photos 1214209899278185 8
Israelair Photos 1214209899278185 8Israelair Photos 1214209899278185 8
Israelair Photos 1214209899278185 8
ย 
Ezvc
EzvcEzvc
Ezvc
ย 
Seica o basilisco รฉ animal de compaรฑa
Seica o basilisco รฉ animal de compaรฑaSeica o basilisco รฉ animal de compaรฑa
Seica o basilisco รฉ animal de compaรฑa
ย 
Rami Meiri
Rami MeiriRami Meiri
Rami Meiri
ย 
Tel Aviv 1920 1940
Tel Aviv 1920 1940Tel Aviv 1920 1940
Tel Aviv 1920 1940
ย 
O mar desde esa banda. De Carlos Penela
O mar desde esa banda. De Carlos PenelaO mar desde esa banda. De Carlos Penela
O mar desde esa banda. De Carlos Penela
ย 
O Meu Pequeno Pais
O Meu Pequeno PaisO Meu Pequeno Pais
O Meu Pequeno Pais
ย 
Android workshop
Android workshopAndroid workshop
Android workshop
ย 
Mark Chagall
Mark ChagallMark Chagall
Mark Chagall
ย 
Maketechwork4u
Maketechwork4uMaketechwork4u
Maketechwork4u
ย 
Mediamatic Night Lab #2
Mediamatic Night Lab #2Mediamatic Night Lab #2
Mediamatic Night Lab #2
ย 
Israel 1214781329718633 9
Israel 1214781329718633 9Israel 1214781329718633 9
Israel 1214781329718633 9
ย 
El Pueblo Gallego 1948
El Pueblo Gallego 1948El Pueblo Gallego 1948
El Pueblo Gallego 1948
ย 
Tweets polls surveys
Tweets polls surveysTweets polls surveys
Tweets polls surveys
ย 

Similar to Zotonic tutorial EUC 2013

A Basic Django Introduction
A Basic Django IntroductionA Basic Django Introduction
A Basic Django Introduction
Ganga Ram
ย 
Spring first in Magnolia CMS - Spring I/O 2015
Spring first in Magnolia CMS - Spring I/O 2015Spring first in Magnolia CMS - Spring I/O 2015
Spring first in Magnolia CMS - Spring I/O 2015
Tobias Mattsson
ย 
MVC & SQL_In_1_Hour
MVC & SQL_In_1_HourMVC & SQL_In_1_Hour
MVC & SQL_In_1_Hour
Dilip Patel
ย 
CCCDjango2010.pdf
CCCDjango2010.pdfCCCDjango2010.pdf
CCCDjango2010.pdf
jayarao21
ย 

Similar to Zotonic tutorial EUC 2013 (20)

Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
ย 
Django cheat sheet
Django cheat sheetDjango cheat sheet
Django cheat sheet
ย 
Serverless ML Workshop with Hopsworks at PyData Seattle
Serverless ML Workshop with Hopsworks at PyData SeattleServerless ML Workshop with Hopsworks at PyData Seattle
Serverless ML Workshop with Hopsworks at PyData Seattle
ย 
Django 1.10.3 Getting started
Django 1.10.3 Getting startedDjango 1.10.3 Getting started
Django 1.10.3 Getting started
ย 
Custom Forms and Configuration Forms in Drupal 8
Custom Forms and Configuration Forms in Drupal 8Custom Forms and Configuration Forms in Drupal 8
Custom Forms and Configuration Forms in Drupal 8
ย 
A Basic Django Introduction
A Basic Django IntroductionA Basic Django Introduction
A Basic Django Introduction
ย 
Angular - Chapter 4 - Data and Event Handling
 Angular - Chapter 4 - Data and Event Handling Angular - Chapter 4 - Data and Event Handling
Angular - Chapter 4 - Data and Event Handling
ย 
Creating Single Page Web App using Backbone JS
Creating Single Page Web App using Backbone JSCreating Single Page Web App using Backbone JS
Creating Single Page Web App using Backbone JS
ย 
Spring first in Magnolia CMS - Spring I/O 2015
Spring first in Magnolia CMS - Spring I/O 2015Spring first in Magnolia CMS - Spring I/O 2015
Spring first in Magnolia CMS - Spring I/O 2015
ย 
Java script
Java scriptJava script
Java script
ย 
MVC & SQL_In_1_Hour
MVC & SQL_In_1_HourMVC & SQL_In_1_Hour
MVC & SQL_In_1_Hour
ย 
Microsoft Graph community call-October 2018
Microsoft Graph community call-October 2018Microsoft Graph community call-October 2018
Microsoft Graph community call-October 2018
ย 
Django crush course
Django crush course Django crush course
Django crush course
ย 
Interoperable Component Patterns
Interoperable Component PatternsInteroperable Component Patterns
Interoperable Component Patterns
ย 
Introduction to Html5
Introduction to Html5Introduction to Html5
Introduction to Html5
ย 
Actionview
ActionviewActionview
Actionview
ย 
CCCDjango2010.pdf
CCCDjango2010.pdfCCCDjango2010.pdf
CCCDjango2010.pdf
ย 
AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014
ย 
HTML5
HTML5 HTML5
HTML5
ย 
Meteor + Ionic Introduction
Meteor + Ionic IntroductionMeteor + Ionic Introduction
Meteor + Ionic Introduction
ย 

More from Arjan (6)

De Naakte Noorderlingen
De Naakte NoorderlingenDe Naakte Noorderlingen
De Naakte Noorderlingen
ย 
Erlang: Software for a Concurrent world
Erlang: Software for a Concurrent worldErlang: Software for a Concurrent world
Erlang: Software for a Concurrent world
ย 
Physical Computing with Android and IOIO
Physical Computing with Android and IOIOPhysical Computing with Android and IOIO
Physical Computing with Android and IOIO
ย 
Hello, world
Hello, worldHello, world
Hello, world
ย 
Open-CI for beginners
Open-CI for beginnersOpen-CI for beginners
Open-CI for beginners
ย 
OBG Presentatie Mediamatic Salon
OBG Presentatie Mediamatic SalonOBG Presentatie Mediamatic Salon
OBG Presentatie Mediamatic Salon
ย 

Recently uploaded

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
ย 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(โ˜Ž๏ธ+971_581248768%)**%*]'#abortion pills for sale in dubai@
ย 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
ThousandEyes
ย 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
ย 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
ย 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
ย 

Recently uploaded (20)

ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
ย 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
ย 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
ย 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
ย 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
ย 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
ย 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
ย 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
ย 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
ย 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
ย 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
ย 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
ย 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
ย 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
ย 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
ย 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
ย 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
ย 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
ย 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
ย 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
ย 

Zotonic tutorial EUC 2013

  • 1. Zotonic The Tutorial Erlang User Conference 2013, Stockholm Arjan Scherpenisse - arjan@miraclethings.nl
  • 2. Zotonic's goals ๎€Š Maximise the use of hardware ๎€Š Frontender friendly ๎€Š Hit the ground running
  • 3. So, what's in the box? ๎€Š Multiple sites ๎€Š Admin interface ๎€Š User management ๎€Š Page management ๎€Š Menu editor ๎€Š Commenting ๎€Š Image management ๎€Š Video embedding ๎€Š Integrated e-mail sending + receiving (!) ๎€Š API OAuth ๎€Š i18n ๎€Š SEO modules ๎€Š Importer modules ๎€Š Mailinglist
  • 4. Getting technical ๎€Š The data model ๎€Š HTTP request flow ๎€Š Modules & sites ๎€Š URL dispatching ๎€Š The building blocks
  • 5. Data model: everything is a thing ArticleArticle PersonPerson ImageImage author depiction
  • 7. Modules ๎€Š Modules are self-contained: ๎€Š Dispatch rules, resources, templates, css, javascript, images, translations, template filters, actions, validators, API services, models, โ€ฆ ๎€Š Prioritized list ๎€Š File lookup mechanism ๎€Š Every module can be a gen_server
  • 8. Notification system ๎€Š Is how modules extend and offer functionality ๎€Š gen_event-like ๎€Š Listen to notifications ๎€Š Send notifications: ๎€Š notify, notify1, fold, map, first ๎€Š Both sync and async % @doc Check if the recipient is a known user, if so redirect the received e-mail observe_email_received(#email_received{localpart=Recipient} = Received, Context) -> case m_identity:lookup_by_username(Recipient, Context) of undefined -> undefined; Props -> case proplists:get_value(is_verified, Props) of true ->
  • 9. URL dispatching ๎€Š Dispatch rules per site ๎€Š (and per module) ๎€Š Dispatch rules have names ๎€Š e.g. this rule serves the contact.tpl on the /contact URL: {contact, [โ€œcontactโ€], controller_template, [ {template, "contact.tpl"} ]},
  • 10. The page controller ๎€Š Webmachine entrypoint for pages ๎€Š 404 check ๎€Š Authorization check + logon redirect ๎€Š Template selection
  • 11. Template system ๎€Š Extended ErlyDTL ๎€Š Expressions: {% if a + b > c %} ๎€Š Tuple and list data types ๎€Š Data access through models: {{ m.rsc[id].title }} ๎€Š Templates can be made more specific by category and/or unique page name ๎€Š Templates with the same name override each other ๎€Š Uses module priority
  • 12. Template ex.: page.page_projects.tpl {% extends "page.tpl" %} {% block content_body %} {% cache 7200 %} {% for r in m.search[{archive_year cat='project'}] %} <div class="grid_9 alpha omega"> <h2 class="year">{{ r.year }}</h2> </div> {% for group in m.search[{query publication_year=r.year|append:"" cat='project' sort="-publication_start"}]|chunk:3 %} <div class="grid_9 alpha omega"> {% for id in group %} {% catinclude "inc/listitem.tpl" id alpha=forloop.first omega=forloop.last%} {% endfor %} </div> {% endfor %} {% endfor %} {% endcache %} {% endblock %}
  • 13. Scomps โ€” screen components ๎€Š For when you need more logic than a template can handle ๎€Š Erlang module ๎€Š Cacheable ๎€Š Example: {% menu id=id %}
  • 14. Javascript โ€” Event handling ๎€Š Ajax postbacks ๎€Š Form submits ๎€Š Calls event/2 function ๎€Š Communicates back to browser by sending javascript ๎€Š Inspired by Nitrogen {% button postback={click foo=โ€barโ€} text=โ€click meโ€ %} event({postback, {click, Args}, _Trigger, _Target}, Context) -> z_render:wire({alert, [{text,โ€helloโ€}]}, Context).
  • 15. Javascript โ€” Push connections ๎€Š Uses either Comet or WebSockets ๎€Š Attaches to the page process ๎€Š Transports Javascript to the UA
  • 16. REST services ๎€Š API method is an Erlang module ๎€Š /api/mymodule/mymethod ๎€Š In /services module directory ๎€Š JSON input / output ๎€Š Process GET and POST
  • 17. Hands on! ๎€Š Get Vagrant (version >= 1.1!) ๎€Š Clone: git clone https://github.com/arjan/zotonic-vagrant-tutorial.git ๎€Š Get it running vagrant up ๎€Š โ€ฆ get some coffee... ๎€Š Visit: http://localhost:8000 ๎€Š ...you can also run it on your local machine, outside vagrant
  • 19. Tutorial use case: Portfolio site ๎€Š Site with information about your projects ๎€Š Each project has: ๎€Š Title, text ๎€Š Slide show of large images ๎€Š List of related projects
  • 20. Easy development ๎€Š apt-get install inotify-tools ๎€Š Enable mod_development ๎€Š Emacs' zotonic-tpl-mode for editing templates ๎€Š โ€ฆ and of course erlang-mode / flymake-mode ๎€Š Each step of this tutorial is a git tag! e.g. git checkout step-0
  • 21. #0 โ€“ base site ๎€Š This is what we started with, created using: ๎€Š zotonic addsite -L -s basesite tutorial
  • 22. #1 โ€“ create project category Define the our site-specific domain model: specific categories that this site uses. ๎€Š In the site's module (tutorial.erl), we create manage_schema/2 ๎€Š Returns a #datamodel{} record ๎€Š When done: ๎€Š z:m(), z:flush(). ๎€Š Click โ€œreinstall data modelโ€ button in the admin
  • 23.
  • 24. #2 โ€“ create project page template Create a page specific for resources of the category project. ๎€Š Template name: page.project.tpl ๎€Š Loops through the list of images ๎€Š Note: look at templates/tablet/page.tpl in mod_base_site to see the template block names that you can use
  • 25. #3 โ€“ project list A list of recent projects, ordered by their publication date ๎€Š The easy way: ๎€Š Create resource with category โ€œsearch queryโ€ ๎€Š Search query: โ€œcat=project sort=-publication_startโ€ ๎€Š Give it the page path โ€œ/projectsโ€ ๎€Š This can go in your #datamodel{} ๎€Š
  • 26. #3b โ€“ alternative project list ๎€Š The hard(coded) way: ๎€Š Set up new dispatch rule for /all-projects ๎€Š Create all-projects.tpl, write template code... ๎€Š (git checkout step-3b)
  • 27. #4 โ€“ feedback form A form which sends an email to the site administrator. ๎€Š Add form to page.project.tpl ๎€Š Create event handler in tutorial.erl ๎€Š Takes form data, ๎€Š Sends email ๎€Š Email is HTML; rendered from template
  • 28. #5 โ€“ notification system We want to make sure a project always has at least one edge of type โ€œauthorโ€ ๎€Š create observe_rsc_update_done/2 in tutorial.erl ๎€Š Tip: Look in zotonic_notifications.hrl for more hooks into the system! ๎€Š Add code ๎€Š Test it in the admin! ๎€Š (reload after save to see the new edge)
  • 29.
  • 30. Thank you! ๎€Š Please come to my talk on friday! ๎€Š โ€œMaking it fastโ€ - the performance of Zotonic ๎€Š โ€ฆ or chat with me & Andreas :-) ๎€Š Online resources: ๎€Š http://zotonic.com ๎€Š http://zotonic.com/docs ๎€Š IRC, XMPP, mailing lists ๎€Š Tutorial source code, tutorial slides