What is XTemplate? Template blocks and logic blocks explained.
CoTemplate allows you to store your HTML code separately from your PHP code (as opposed to compiling your template into PHP as per Smarty etc.). It has many useful features such as nested blocks and various kinds of variable interpolation, and yet the code is very short and very optimized."
CoTemplate is using blocks for outputting PHP code. You can think of them as instructions to Cotonti where to output the specific code.
Block syntax:
<!-- BEGIN: XXX --> Your code here <!-- END: XXX -->
<!-- BEGIN: XXX -->
This defines the beginning of the block, and also tells Cotonti how and when to output your code (an example below will hopefully make this clear for everyone)
"Your code here"
Pretty self explanatory, you can insert your HTML code (divs, tables, etc) and Tags ( visit TPL Tags Repository for more info)
<!-- END: XXX -->
Defines the end of block, after this block, Cotonti stops outputting your code
XXX
Cotonti defined code, you can find the codes in any .tpl file.
Example:
<!-- BEGIN: PAGE_FILE -->
<br /><br /><hr />
<div class="pageBody">
<div class="pageTop"></div>
<div class="pageText">
<!-- BEGIN: MEMBERSONLY -->
{PAGE_FILE_ICON} {PAGE_SHORTTITLE}<br/>
<!-- END: MEMBERSONLY -->
<!-- BEGIN: DOWNLOAD -->
{PAGE_FILE_ICON}<a href="{PAGE_FILE_URL}">{PHP.L.Download}: {PAGE_SHORTTITLE}</a><br/>
<!-- END: DOWNLOAD -->
{PHP.themelang.page.Filesize}: {PAGE_FILE_SIZE}{PHP.L.kb}, {PHP.themelang.page.downloaded} {PAGE_FILE_COUNT} {PHP.themelang.page.times}
</div>
</div>
<!-- END: PAGE_FILE -->
<!-- BEGIN: PAGE_FILE --> instructs Cotonti to output(display it to user) the code below if page has "File Download : Yes" and "URL" of file is entered.
If "File Download:" is set to "No", Cotonti will skip to the end of <!-- END: PAGE_FILE --> and simply continue reading the code after that ending tag (without displaying the code between <!-- BEGIN: PAGE_FILE --><!-- END: PAGE_FILE --> to user).
Image 1. File download is set to No..
Image 1.1 ... link for download won't be displayed on page
Image 2. File download is set to Yes and URL of file is entered...
Image 2.1 ... and now can see the link and download the file
You can think of Blocks as "case logic", and that's pretty much correct since CoTemplate v2 also supports "IF-ELSE statements", but blocks are also used for defining the beginning and the end of theme parts.(Template blocks)
Example:
<!-- BEGIN: HEADER --> <!-- END: HEADER --> <!-- BEGIN: MAIN --> <!-- END: MAIN --> <!-- BEGIN: FOOTER --> <!-- END: FOOTER -->
NOTE that blocks are case sensitive! e.g.
<!-- BEGIN: HEADER -->
This is valid block!
<!-- END: HEADER -->
<!-- begin: header -->
This is NOT a valid block!
<!-- end: header -->
CoTemplate v2.1 also has disk cache feature. This cache is reserved only for CoTemplate.
If you don't see changes on your site, even though you uploaded modified .tpl file back to server, you will need to clean your "Disk cache" to see the template changes.
Simply go to Administration > Other > Disk cache and clean the cache by clicking on "Purge all". Now you will be able to see the changes you made in your .tpl files.
CoTemplate supports IF-ELSE statements(logic blocks) since Cotonti 0.6.0. Someone might not agree with me, but I personally think this was a great idea. Let me show you why :)
First we have to understand what is logic block, and what is supported by new CoTemplate.
Logic block:
<!-- IF <if_statement> -->
this will display if <if_statement> is true
<!-- ENDIF -->
For <if_statement> you can use TPL tags or Global tags.
Examples:
<!-- IF {PHP.usr.profile.user_name} == "admin" -->
hello admin !
<!-- ENDIF -->
<!-- IF {PAGE_DESC}=="" -->
Description of this page is empty
<!-- ENDIF -->
<!-- IF {PAGE_DESC}=={PAGE_TITLE} AND {PAGE_COMMENTS_COUNT} > 0 -->
There is some comments here.
And title and description of this page are equal.
<!-- ENDIF -->
Blocks are NOT space sensitive! e.g.
The old version of CoTemplate wasn't space sensitive. The Cotonti team changed that in CoTemplate v2, but they changed it back in CoTemplate v2.1. Users had to port themes(make certain changes, mostly because blocks weren't space sensitive) due to the fact that old themes were based on CoTemplate v1 and that caused the problem for lots of users.
<!-- IF {PAGE_COMMENTS_COUNT} > 0 -->
This is valid block!
<!-- ENDIF -->
<!--IF {PAGE_COMMENTS_COUNT} >0-->
This is also valid block!
<!--ENDIF -->
Logical expressions are limited functionality. Supported logical operators are: AND, OR (AND has higher priority). Parenthesis is not supported. Supported comparison operators are: ==, !=, <, >, <=, >=, !. First operand of comparison must be a template variable e.g. {PHP.my.test}. Second operand may be a template variable, a number or a quoted string.
Example:
<!-- IF {USER_DETAILS_NAME} = 'Mark' AND {PHP.g} >= 6 OR !{PHP.error_string} -->
<div>The expression is true!</div>
<!-- ELSE -->
<div>The expression is false!</div>
<!--END:TEST-->
Nested logical blocks ARE supported (since CoTemplate 2.5), e.g.
<!-- IF {PHP.foo} = 'test' -->
<!-- IF {PHP.bar} -->
<div>This block is invalid!</div>
<!-- ENDIF -->
<!-- ENDIF -->
Template blocks inside of logical blocks ARE supported (since CoTemplate 2.5), e.g.
<!-- IF {PHP.foo} = 'test' -->
<!-- BEGIN: BAR -->
<div>This block is invalid!</div>
<!-- END: BAR -->
<!-- ENDIF -->
This is something you can see on almost every Wordpress blog. Comment is styled different if the comment author is also the page owner.
The regular comment styling in sed-light theme looks like this:
comments.tpl :
<!-- BEGIN: COMMENTS_ROW -->
<span class="title">
<a href="{COMMENTS_ROW_URL}" id="c{COMMENTS_ROW_ID}"><img src="themes/{PHP.theme}/img/system/icon-comment.gif" alt="" /> {COMMENTS_ROW_ORDER}.</a>
{PHP.themelang.comments.Postedby} {COMMENTS_ROW_AUTHOR}</span> {COMMENTS_ROW_DATE} {COMMENTS_ROW_ADMIN} {COMMENTS_ROW_EDIT}
<p>{COMMENTS_ROW_TEXT}</p>
<hr />
<!-- END: COMMENTS_ROW -->
And we can make it look like this:
by simply editing comments.tpl and inserting some logic blocks.
Here's the code:
<!-- BEGIN: COMMENTS_ROW -->
<!-- IF {COMMENTS_ROW_AUTHORID} == {PHP.pag.page_ownerid} -->
<div style="background-color:#C8D9E2; padding: 10px;">
<span class="title">
<a href="{COMMENTS_ROW_URL}" id="c{COMMENTS_ROW_ID}"><img src="{PHP.usr.profile.user_avatar}" width="20" height="20" alt="" /> {COMMENTS_ROW_ORDER}.</a>
{PHP.themelang.comments.Postedby} {COMMENTS_ROW_AUTHOR}</span> {COMMENTS_ROW_DATE} {COMMENTS_ROW_ADMIN} {COMMENTS_ROW_EDIT}
<p>{COMMENTS_ROW_TEXT}</p></div><hr />
<!-- ELSE -->
<span class="title">
<a href="{COMMENTS_ROW_URL}" id="c{COMMENTS_ROW_ID}"><img src="themes/{PHP.theme}/img/system/icon-comment.gif" alt="" /> {COMMENTS_ROW_ORDER}.</a>
{PHP.themelang.comments.Postedby} {COMMENTS_ROW_AUTHOR}</span> {COMMENTS_ROW_DATE} {COMMENTS_ROW_ADMIN} {COMMENTS_ROW_EDIT}
<p>{COMMENTS_ROW_TEXT}</p><hr />
<!-- ENDIF -->
<!-- END: COMMENTS_ROW -->
As you can see, I changed the background-color and I added user avatar instead of comments icon. You can do much more :)
Feel free to correct me if anything is wrong!
Total: 1, on page: 1
Переведите плис на Руский