120 likes | 307 Views
Tirana, 7 June 2005. Zope Concepts - DTML. Valentin Baciu Finsiel Romania. Zope Scripting languages - DTML. DTML (Document Template Markup Language) is a facility for generating textual information using a template document and application information stored in Zope.
E N D
Tirana, 7 June 2005 Zope Concepts - DTML Valentin Baciu Finsiel Romania
Zope Scripting languages - DTML • DTML (Document Template Markup Language) • is a facility for generating textual information using a template document and application information stored in Zope. • It is used in Zope primarily to generate Hypertext Markup Language (HTML) files • Can be used to generate SQL commands in Zope SQL Methods. • DTML dynamically generates, controls and formats content. • DTML is server side scripting language, executed by the Zope server
DTML • DTML is a tag based presentation and scripting language • That means that tags embedded in your HTML cause parts of your page to be replaced with “computed” content • The code placed in DTML Objects is constrained by Zope’s security policy • DTML can be added in • DTML Documents – supports properties • DTML Methods – used to carry out actions • DTML makes the reuse of content and layout possible
DTML tags • The format for DTML tags is the following <dtml-tag name attribute1="value1" attribute2="value2" ... > • The nameattribute is used to obtain data by name. The data is looked up using a set of rules • When the value of a nameattribute is looked up, the value is automatically called, if possible. If the value is a Zope Document or Python document template, it is rendered before being given to the tag that uses the name attribute. <dtml-var standard_html_header> standard_html_headeris a Zope document which provides standard HTML to be included at the top of every page. When the var tag above is used, the DTML in standard_html_header is rendered and the result is inserted in the current document. • The expr attribute allows complex expressions to be evaluated.
DTML tags • dtml-var - The dtml-var tag is used to perform simple variable substitutions. • dtml-if/dtml-unless - supports the conditional insertion of text based on DTML variables or expressions. • dtml-in - is used to iterate over a sequence of objects, iterative insertion, • dtml-with- can be used to expand the namespace of a document template by adding attributes (or mapping keys) from an object which already exists in the document template namespace. • dtml-let - multiple assignments with this tag • dtml-call - evaluating names or expressions without generating text • dtml-raise – used forraising errors
DTML tags - continued • dtml-try (dtml-except/dtml-finnaly) - catch and handle these problematic exceptions within a block of DTML code, This allows you to anticipate and handle errors yourself, • dtml-comment - provides a way to exclude source text from the rendered text. • dtml-return - used to return data rather than text from a DTML method. • dtml-tree - displaying information hierarchically • dtml-sendmail - used to send an electronic message using the Simple Mail Transport Protocol (SMTP). No text in the output • dtml-mime - used in conjunction with the dtml-sendmail tag to send attachments along with electronic mail messages.
Examples of dtml-var usage • <dtml-var x> includes the value of x. x is looked up in the current DTML namespace, called, if it is callable, and the result converted to a string. • <dtml-var expr="x+y"> includes the value of x+y. • <dtml-var x html_quote missing> includes the value of x with the special HTML characters < and & quoted. If x is not bound in the current DTML namespace, no KeyError is raise but the (default) value of missing, the empty string, is included. • <dtml-var x url> includes the URL of x.
Edit the front page of your site • Suppose we have two folders ‘cars’ and ‘trucks’ in our Zope server • Homepage for the site which gives access to the above information (index_html) looks like this: <dtml-var standard_html_header> <p>This site presents information about various types of cars.</p> <ul> <li><a href="/cars">Cars</a> <li><a href="/trucks">Trucks</a> </ul> <dtml-var standard_html_footer>
How the page is constructed To provide consistent look over the site the following methods are used: • standard_html_header - The standard Zope header object. By convention this object displays an HTML header <html><head><title><dtml-var title_or_id></title></head><body bgcolor="#FFFFFF"> <h2><dtml-var site_title></h2> <dtml-var logo> • index_html - The name of the default Zope object in a Folder. When you view a Folder the index_html object will be displayed unless you specify otherwise. <dtml-var standard_html_header> <h2><dtml-var title_or_id></h2> <p>This is the section about <dtml-var title_or_id>.</p> <dtml-var standard_html_footer> • standard_html_footer - The standard Zope footer object. By convention this object displays an HTML footer <p><hr> <a href="mailto:<dtml-var webmaster>">Feedback to Webmaster</a><br> Page last modified: <dtml-var bobobase_modification_time> </p> </body></html>
Customising views • Customise default views for the folders • in the ‘cars’ folder, we should add a DTML Method • Give it the Id 'index_html' • Edit this method and replace the text with the following: <dtml-var standard_html_header> <h2><dtml-var title_or_id></h2> <p>This is the section about <dtml-var title_or_id>.</p> <ul> <dtml-in objectValues> <li><a href="<dtml-var absolute_url>"><dtml-var title_or_id></a> </dtml-in> </ul> <dtml-var standard_html_footer>
Creating a breadcrumb trail • In the Root folder, a DTML Method called 'breadcrumbtrail‘ should be added • It should contain the following text and called form the standard_html_header: <p> <dtml-call "REQUEST.set('stopshort', 0)"> <dtml-if expr="_['id'] == 'index_html' or PARENTS[0].id == _['id'] or absolute_url(1) == ''"> <dtml-call "REQUEST.set('stopshort',1)"> </dtml-if> <dtml-in PARENTS skip_unauthorized reverse> <dtml-if "stopshort and _['sequence-end']"> <dtml-var title_or_id size=25> <dtml-else> <a href="<dtml-var absolute_url>"><dtml-var title_or_id size=25></a> >> </dtml-if> </dtml-in> <dtml-unless stopshort> <dtml-var title_or_id size=25> </dtml-unless> </p>
Zope concepts Your Website structure a simple hierarchical folder structure How acquisition works from …/cars/diesel only the attributes of: • cars folder • Root folder are visible