Let’s assume you are looping over multiple articles of a blog that you want to display, here is what you can do to do
it as lightly as possible:
You first have to create an “article.tpl” template file, the name doesn’t matter really it’s up to you, here is what
You will then use this template to render all the articles.
As already mentioned, the Dwoo runtime parses the template files to generate HTML output. In this section we introduce
some common constructs to quickly get you up to speed.
As above in Dwoo simple variables are passed and rendered to HTML using an associated array, as the following example
The variables page_title and page_content are passed to the template rendering API like such:
In the above example the $params array is loaded with parameters that are substituted in the Dwoo code snippet.
In Dwoo it is possible to ‘bundle’ your variables up into arrays on the PHP side and address them separately in a
Dwoo template using the ‘dot’ operator. This helps reduce the possibility of confusion with one variable with two
different purposes being used in different places in your Dwoo template. An example will be used to illustrate this:
In the above code snippet we have two bundles, the first is the auth array that includes the user’s username.
The second is the array that contains the page content. This snippet is setup with the following PHP code:
The above two snippets generate roughly the same HTML as the first example using simple variables.
You may wonder why using arrays to bundle your parameters has any benefit:
It prevents naming collisions.
Allows you to group or encapsulate related blocks of variables.
You can have different PHP modules generating their own output arrays making your site’s design more modularized.
Dwoo has if/else constructs to allow the coder to add conditional sections to web pages. This can be used for things
such as restricting content to authorized users or displaying form components only if certain conditions are met.
Dwoo’s conditional statements are quite flexible, however we only cover the basics here.
Sometimes you need to repeat parts of a web page in a systematic manor. In Dwoo this is done using
loops. This is best illustrated by an example:
Above is a code snippet from an HTML form. The type_id is a simple variable (see above). The licensee_type_list
variable is an array containing associated arrays. What occurs is that in every iteration of the loop the
loop plugin extracts the values from an array entry with its name and id as
keys and substitutes them into the template. The above snippet (if in a file called code_snippet.tpl) would be
initialized and setup with the following PHP:
The above Dwoo snippet and PHP code extract will give the following HTML output:
This method can also be used to generate tables of data (by repeating the tr tag). It is up to you.
Breaking your webpage templates into sections
Webpages are usually broken up into sections for easy maintenance and design. An example could be a webpage which has
a header, body content, and footer sections. In this case instead of everything being in one file, the page is broken
up into components which can be reused for other page templates.
In PHP you can do this by writing functions, that when called output a section of the page. In Dwoo this is
accomplished by using the include plugin. An example would be:
Above is a generic page whose title and main body content can be extracted from a database. You may choose to do it
this way or to ‘hard code’ your content directly into a function specific template.
Looking for help?
Couldn’t find what your are looking for ? Why not join our support chat and let us help you.