Codementor Events

Build A Forum With Dancer2 (Episode — 4)

Published Aug 03, 2018
Build A Forum With Dancer2 (Episode — 4)

Extend Template Toolkit

In this tutorial, we will refactor our code to make our application scalable.

We modified index.tt:
 → To view header menu
 → To see the thread topics on a web page, when the user loads Application URL.

At present, when page loads, User can see:
 → Header Menu
 → Thread-Topic

We require header menu to be standard for all the web pages. 
We may or may not show the threads on all pages, but we want header menu to be standard for all.


Header Menu and Thread-Topic View

→ Create a new file ‘ thread.tt ’ under views folder. And include the following code.

[% INCLUDE index.tt %]

<strong>
    All Topics
</strong>

<hr>

[% FOREACH thread IN threads %]
    [% thread.title %]
    <hr>
[% END %]

INCLUDE keyword is to inherit the contents of index.tt to thread.tt template.

NOTE:- Make sure to delete the above content from index.tt file.

Reload the application using plackup. Now, you see a blank page on Screen.

Why?

Because, In the PForums.lib package, we need to tell Dancer2 App, to use thread.tt template instead of index.tt.

Replace,

template 'index' => {

with

template 'thread' => {

and reload the application and refresh page. Application starts working again.

Great!!!!!

In this tutorial, we learn how to extend the template toolkit to inherit the features of the parent template.

That’s all for now. Please submit your feedback or query in the comments section.

Discover and read more posts from Ashutosh Kukreti
get started
post commentsBe the first to share your opinion
Show more replies