Templates project

Templates project

Martin Dougiamas發表於
Number of replies: 12
Core developers的相片 Documentation writers的相片 Moodle HQ的相片 Particularly helpful Moodlers的相片 Plugin developers的相片 Testers的相片
For discussion and reference, here is the developing spec for Templates (the next big project after Groups)

   http://moodle.com/development/plans/templates.html 



評比平均分數: -
In reply to Martin Dougiamas

Re: Templates project

John Gone發表於
Quote from:

Templates - implementation plan for Moodle version 2.0


"We will be using Smarty as a template engine. This allows templates to be written very clearly in a non-PHP language and then be compiled and cached in PHP form for speed."
In reply to John Gone

Re: Templates project

Martin Dougiamas發表於
Core developers的相片 Documentation writers的相片 Moodle HQ的相片 Particularly helpful Moodlers的相片 Plugin developers的相片 Testers的相片
Yes. You might remember there were some discussions about XSLT at one point but I think these are pretty much a future dream without any huge advantages at this point. Smarty works now on any PHP server and is a well tested, solid solution.
In reply to Martin Dougiamas

Re: Templates project

John Gone發表於
Agreed, bright future but, at this point, it's mostly a dream. IMHO.

Just wanted to start the ball rolling for the themesters by linking to Smarty. It looks like they've thought this through. I'll be spending lots of time with the manual over the holidays, between snoozes, visits, and holiday cheer.
In reply to John Gone

Re: Templates project

Janne Mikkonen發表於
Core developers的相片

Smarty rules!

I was testing it out and loved it right away. You can easily dynamically assign a class name to html/xhtml -elements.

eg.

in header.tpl

<link rel="stylesheet" href="{$style}" type="text/css">
</HEAD>
<BODY bgcolor="#ffffff">
<p {$style_class}>{$pInput}</p>

in php script:

require '../libs/Smarty.class.php';

$smarty = new Smarty;
$smarty->assign("style","/themes/liteblue/style.php");
$smarty->assign("style_class","class=\"teststyle\"");

$smarty->display('index.tpl');

I think this gives style designers wider range to create new themes or "skins".

- Janne -

In reply to John Gone

Re: Templates project

Janne Mikkonen發表於
Core developers的相片

Or perhaps more non-programmer way would be to make styles.conf file with every class name in it eg.

p_normal = normal
p_bold = pbold

load the styles.conf in template:

{config_load file=styles.conf}

and use them in like:

<p class="{#p_normal#}">{$content}</p>
<p class="{#p_bold#}">{$warning}</p>

and so on...

In reply to John Gone

Re: Templates project

Gustav W Delius發表於

Is my understanding correct that the conversion to using smarty templates can be a gradual process? One can mix pages using smarty with old pages and one can even in one and the same script print some things straight to the page and display other things with smarty's $smarty->display(). If that is the case, what is stopping us from starting to use smarty templates immediately on all new code?

In reply to Gustav W Delius

Re: Templates project

Janne Mikkonen發表於
Core developers的相片
Well mixing is ok and probably some cases necessary, but then again it would be a great time to convert all those HTML's to fully qualified XHTML at the same time?
In reply to Janne Mikkonen

Re: Templates project

Gustav W Delius發表於

You are quite right, in the end everything should be done via Smarty templates and they should be fully XHTML compliant. The question was just how to get there. Apparently a continuous, step-by-step, conversion of Moodle to templates is possible. We don't have to rewrite all pages at once.

In reply to Gustav W Delius

Re: Templates project

Janne Mikkonen發表於
Core developers的相片
I agree big grin.

When we have a definition how to call smarty and where to put the templates, I think we'll set to go. No need to rush.
In reply to Martin Dougiamas

Re: Templates project

Janne Mikkonen發表於
Core developers的相片

My experience of smarty so far.

I made one of my own projects with smarty and I did it with this "three layer model" (not exact science big grin).

Layer 1. - Datalayer (core). Contains the actual php code. Does not print anything.
Functions actually returns something that can be assigned to smarty class as a variable like
string, array or class.

Layer 2. - Smartylayer. Assigns variables to smarty class and outputs html/xhtml from templates.
Since it's combination of html/xhtml and smartycode, template/style designer can easily add all
the necessary style attributes to html/xhtml tags.

Layer 3. - Stylelayer (css) include stylesheets for current theme (whole theme if the theme is
build from several templates).

Example:

Function get_links returns an array of links from database (id, title, etc...).

Get the links.

$linkdata = get_links();

Assign it to smarty class and display template.

$smarty->assign("links",$linkdata);
$smarty->display("main.tpl");

And in main.tpl we actually loop through that $links array and print links to html table.
(this template also checks if the global variable "page" is set and
if/else statement to define what to do)

{section name=i loop=$links}
<table class="ntable" width="100%" cellpadding="2" cellspacing="0">
<tr>
   <td class="ntd"><img src="{$theme}/button.gif" alt=""></td>
   {if $smarty.get.page !="" and $smarty.get.page == $links[i].id and !$smarty.get.news}
   <td class="ntd" align="right">{$links[i].title}<img src="{$theme}/spacer.gif" width="5" height="2" alt=""></td>
   {elseif $smarty.get.page == "" and $links[i].isfp == 1 and !$smarty.get.news}
   <td class="ntd" align="right">{$links[i].title}<img src="{$theme}/spacer.gif" width="5" height="2" alt=""></td>
   {else}
   <td class="ntd" align="right"><a class="vert" href="{$url}/?page={$links[i].id}">{$links[i].title}</a></td>
   {/if}
</tr>
</table>
{/section}

I don't know if this is any help to anyone, but I hope it'll give some picture of using smarty template engine...

- Janne -

附件 3layer.gif