2007/11/02

新版 Blogger Template 中 pageType 的Bug

在新版的Blogger Template 中 有提共
[b:if cond="data:blog.pageType == "item""]
...
[b:else/]
...
[/b:if]

像這樣的語法 來達到在不同類型的頁面顯示不同的內容
就跟舊式的[ItemPage][/ItemPage]是相同用意
不過要在 [b:widget/] 的迷你裝置前後加上上方的語法時
會出現錯誤訊息說明[b:widget/]只能被夾在[b:section]當中
因此要將[b:if]拉到[b:section]的外層
所以有可能會造成排版上得問題

因此在新版的Template說明文件中 有提到
可以使用 pageType 這一個屬性來決定此widget是否要顯示

A widget may have the following attributes:

* id - (Required) This may contain letters and numbers only, and each widget ID in your template should be unique. A widget's ID cannot be changed without deleting the widget and creating a new one.
* type - (Required) This indicates what kind of a widget it is, and should be one of the valid widget types listed below.
* locked - (Optional) Can be "yes" or "no," with "no" as the default. A locked widget cannot be moved or deleted from the Page Elements tab.
* title - (Optional) A display title for the widget. If none is specified, a default title such as "List1" will be used.
* pageType - (Optional) Can be "all," "archive," "main," or "item," with "all" as the default. The widget will only display on the designated pages of your blog. (All widgets display on the Page Elements tab, regardless of thier pageType.)

但是經過實際測試的結果 此功能完全無效
在修改完存檔之後 會自動清除此屬性

因此我想了一個迂迴的方式
先在 [body] 標籤之前 加入
[script]
var MyPageType = "[data:blog.pageType/]"
[/script]

之所以要在[body]之前 是要讓所有的widget都可以使用
然後只要在widget的編輯畫面 (不一定要寫在template的文件中)
利用 Javascript 的 判斷式來檢查
[script]
if (MyPageType == 'index')
{
document.write("要顯示的Html語法")
}
else if (MyPageType == 'item')
{
document.write("要顯示的Html語法")
}
[script]

這種方式只要在第一次改好Template
以後都不用再去修改
只要在widget的編輯畫面就可以決定
不過缺點就是 這樣的檢查方式是在client端來執行
因此無論要不要顯示 client端的原始碼中都會出現

附註 以上文章所有出現的 "[" 都要換成 "<" ,"]"要換成 ">"

0 comments:

 
Back to Top
This Site Power by JoStudio. Some Rights Reserved.