How to create a simple Web application with Web::App Template::Toolkit and Class::DBI - PowerPoint PPT Presentation

1 / 37
About This Presentation
Title:

How to create a simple Web application with Web::App Template::Toolkit and Class::DBI

Description:

footer.html. MyDatabase.pm - Model. CGI::Application. helloworld.cgi: use HelloWorldCgiApp; ... POST_PROCESS = 'footer.html', hw_string = 'hi there big earth! ... – PowerPoint PPT presentation

Number of Views:172
Avg rating:3.0/5.0
Slides: 38
Provided by: Leon155
Category:

less

Transcript and Presenter's Notes

Title: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI


1
How to create a simple Web application with
WebApp TemplateToolkit and ClassDBI
Leonard Miller June 17, 2008
2
Who is this talk for?
3
Who is this talk for?
  • Need to write web applications

4
Who is this talk for?
  • Need to write web applications
  • Dont want/cannot have a heavy framework

5
Who is this talk for?
  • Need to write web applications
  • Dont want/cannot have a heavy framework
  • Experienced Programmers.

6
Who is this talk for?
  • Need to write web applications
  • Dont want/cannot have a heavy framework
  • Experienced Programmers.
  • Know about CPAN, how to learn from CPANs
    documentation.

7
Who is this talk for?
  • Need to write web applications
  • Dont want/cannot have a heavy framework
  • Experienced Programmers.
  • Know about CPAN, how to learn from CPANs
    documentation.
  • Newer programmers that could use a new/better way
    to organize their code.

8
Why these three Modules?
  • Separate out the code to MVC Pieces
  • Each can be used/tested alone
  • The modules themselves are easy to use and
    understand

9
Why not Catalyst?
  • mod_perl/fastcgi You dont always have the
    access on the machine to get Catalyst to work.
  • CGIApplication is a lite framework, and as
    such is much smaller.
  • Not as big and scary.
  • Trivial to install in a local /lib dir

10
What is MVC
  • MVC stands for Model-View-Controller

11
What is MVC
  • MVC breaks the work into three parts

12
What is MVC
  • MVC breaks the work into three parts
  • Model - Short for database model. ClassDBI does
    all the database work inserts/queries.

13
What is MVC
  • MVC breaks the work into three parts
  • Model - Short for database model. ClassDBI does
    all the database work inserts/queries.
  • View - TemplateToolkit does all the view/html
    work.

14
What is MVC
  • MVC breaks the work into three parts
  • Model - Short for database model. ClassDBI does
    all the database work inserts/queries.
  • View - TemplateToolkit does all the view/html
    work.
  • Controller - CGIApplication holds all the logic
    to glue the Model and the View together.

15
What is MVC
  • Who has seen code like this
  • use DBI
  • my sql "select from users"
  • my dbh DBI-gtconnect( ds, un, pw )
  • my sth dbh-gtprepare(sql)
  • sth-gtexecute()
  • print "Content-type text/html\r\n\r\n"
  • while (my h sth-gtfetchrow_hashref())
  • print Name is".h-gt'first_name'."ltbrgt\n"

16
What is MVC
  • Who has seen code like this
  • my q new CGI
  • if (q-gt param('first_name' eq '')
  • print input_form()
  • else
  • my sql "insert into users ..."
  • my sth dbh-gtprepare(sql)
  • sth-gtexecute()
  • print submission_form()

17
What is MVC
  • Any questions regarding what MVC is?

18
CGIApplication
  • A sample program
  • Helloworld.cgi lt- config info
  • HelloWorldCgiApp.pm lt- controller
  • Html files lt- View
  • header.html
  • body.html
  • footer.html
  • MyDatabase.pm lt- Model

19
CGIApplication
  • helloworld.cgi
  • use HelloWorldCgiApp
  • my helloworld HelloWorldCgiApp-gtnew()
  • helloworld-gtrun()

20
CGIApplication
  • helloworld.cgi (with config info)
  • use HelloWorldCgiApp
  • my helloworld HelloWorldCgiApp-gtnew
  • (
  • PARAMS gt
  • tt_config gt
  • INCLUDE_PATH gt ".",
  • PRE_PROCESS gt 'header.html',
  • POST_PROCESS gt 'footer.html',
  • ,
  • hw_string gt "hi there big earth!",
  • ,
  • )
  • helloworld-gtrun()

21
CGIApplication
  • HelloWorldCgiApp (continued)
  • package HelloWorldCgiApp
  • use base 'CGIApplication'
  • use Template
  • sub setup
  • my self shift
  • self-gtrun_modes( 'mode1' gt 'start,
  • 'mode2' gt 'sec_page'
  • )
  • self-gtstart_mode('mode1')

22
CGIApplication
  • HelloWorldCgiApp (continued)
  • package HelloWorldCgiApp
  • use base 'CGIApplication'
  • use Template
  • sub setup
  • my self shift
  • self-gtrun_modes( 'mode1' gt 'start,
  • 'mode2' gt 'sec_page'
  • )
  • self-gtstart_mode('mode1')

q -gt param (rm)
23
CGIApplication
  • HelloWorldCgiApp (continued)
  • sub start
  • my self shift
  • my tt_config self-gtparam(tt_config)
  • my tt Template-gtnew( tt_config )
  • tt-gtprocess('body.html',
  • hwstr gt 'hi big planet!!!',
  • ,
  • \html)
  • return html

24
CGIApplication
  • HelloWorldCgiApp (continued)
  • sub cgiapp_prerun
  • my (self, runmode) _at__
  • my q self-gtquery

25
ClassDBI
  • The mysql table
  • CREATE TABLE users (
  • user_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  • first_name varchar(75) NOT NULL,
  • last_name varchar(75) NOT NULL,
  • country_code CHAR(2) NULL
  • )

26
ClassDBI
  • MyDatabase.pm
  • package MyDatabase
  • use base 'ClassDBI'
  • MyDatabase-gtconnection('dbimysqldb',user,pw')
  • MyDatabase -gt table('users')
  • MyDatabase -gt columns( All gt qw/user_id
  • first_name last_name country_code / )
  • MyDatabase -gt columns( Primary gt user_id )

27
ClassDBI
  • Using the ClassDBI Module
  • HelloWorldCgiApp.pm
  • use MyDatabase
  • my _at_users MyDatabase -gt retrieve_all
  • tt-gtprocess('body.html',
  • users gt _at_users ,
  • ,
  • \html)

28
ClassDBI
  • Using the Module
  • use MyDatabase
  • my _at_users MyDatabase -gt retrieve_all
  • my user MyDatabase -gt retrieve( 1 )
  • tt-gtprocess('body.html',
  • users gt _at_users ,
  • user gt user,
  • ,
  • \html)

29
ClassDBI
  • Using the Module inserts
  • use MyDatabase
  • my user MyDatabase -gt insert(
  • first_name gt Randall,
  • last_name gt Schwartz,
  • country_code gt US,
  • )

30
TemplateToolkit
  • Why use TemplateToolkit?
  • Common look and feel templates.
  • Easy to change for those people who are better at
    design than we are.
  • What type of data is It good for?
  • arrays
  • hashes
  • scalars

31
TemplateToolkit
  • Any html file is a TT file!
  • Simplest usage for a scalar
  • lthtmlgt
  • ltbodygt
  • var_name
  • lt/bodygt
  • lt/htmlgt

32
TemplateToolkit
  • Usage for an Array where users is an array
  • ltbodygt
  • FOREACH item IN users
  • item ltbr /gt
  • END
  • lt/bodygt

33
TemplateToolkit
  • Usage for an Array where users is a hash
  • ltbodygt
  • item.user_id item.first_name
    item.last_name item.country_code ltbr /gt
  • lt/bodygt

34
TemplateToolkit
  • Usage for an Array where users is an array of
    hashes (like an array of rows from a database)
  • ltbodygt
  • FOREACH item IN users
  • item.user_id item.first_name
  • item.last_name item.country_codeltbr /gt
  • END
  • lt/bodygt

35
TemplateToolkit
  • Usage for an Array where users is an array of
    hashes (like an array of rows from a database)
  • ltbodygt
  • ltformgt
  • ltinput typehidden namerm valuepage_2
  • FOREACH item IN users
  • ...
  • END
  • ltinput typesubmitgt
  • lt/formgt
  • lt/bodygt

36
  • Questions?

37
  • Thank you

Leonard Miller June 17, 2008
Write a Comment
User Comments (0)
About PowerShow.com