2Hi.Biz
Trang chủ | TWIG | Xtscript | Templates | Xtgem
Hướng dẫn làm thời gian đếm ngược cho guestbook sử dụng js | Share code | Xtgem - 2Hi.Biz
▼ Hướng dẫn làm thời gian đếm ngược cho guestbook sử dụng js
* Godlike
- Cấp bậc: mem
03-04-2015
kết quả cho ra vd: 1 ngày trước, 1 giờ trước...
lợi ích: load nhanh hơn rất nhiều và đơn giản hơn twig
hướng dẫn:
trước hết nếu bạn đã có và dùng guestbook từ nhiều nguồn khác nhau rồi thì nên backup code lại đề phòng.
b1: bạn tạo 1 file guestbook và post code
<xt:guestbook comments_per_page="10" comments_sort="1" parse_html="0" format="r" t_guestbook="Phòng chat" t_guestbook_empty="Chưa có bình luận" t_post_a_comment="Gửi một bình luận" t_name="Tên của bạn:" t_comment="Nội dung:" t_post="Gửi" t_reply="Trả lời" t_posting_as="Bình luận bằng tên" t_comments_login_required="Đăng nhập để bình luận" t_login="Đăng nhập" t_signup="Đăng kí" t_confirm_email="Xác nhận mail đã" t_report_spam="Báo cáo" />

<script type="text/javascript"> 
// Function
function timeAgo (dateString) {
var rightNow = new Date();
var then = new Date(dateString);
var diff = rightNow - then;
var second = 1000,
minute = second * 60,
hour = minute * 60,
day = hour * 24,
week = day * 7;
if (isNaN(diff) || diff < 0) {return ""; }
if (diff < second * 2) {return "right now";}
if (diff < minute) {return Math.floor(diff / second) + " seconds ago";}
if (diff < minute * 2) {return "about 1 minute ago";}
if (diff < hour) {return Math.floor(diff / minute) + " minutes ago";}
if (diff < hour * 2) {return "about 1 hour ago";}
if (diff < day) {return  Math.floor(diff / hour) + " hours ago";}
if (diff > day && diff < day * 2) {return "yesterday";}
if (diff < day * 365) {return Math.floor(diff / day) + " days ago";}
else {return "over a year ago";} 
}
// Function call
a=document.getElementsByName('pubdate');
for (b=0;b<a.length;b++){
c=Date.parse(a[b].innerText);
a[b].innerText=timeAgo(c);}
</script>

bạn nên việt hoá lại code javascript trong code b1 ví dụ ago --> trước, minutes --> phút....
b2: tạo folder _xtgem_templates và tạo file xtguestbook.twig trong folder vừa tạo
b3: post code vào file đó
<div class="xt_forum">
  <div id="guestbook">
    {% block guestbook_comments_list %}
    <div class="breadcrumbs compo">
      <b>{% if data.comments %}{{ data.translations.t_guestbook }}{% else %}{{ data.translations.t_guestbook_empty }}{% endif %}</b>
    </div>
    {% if data.in_comments_thread %}
    <div class="breadcrumbs compo">
      <a href="{{ data.comments_thread_all }}" class="">{{ data.translations.t_show_all_comments }}</a>
    </div>
    {% endif %}
 {% macro comments_list (comments, ctx) %}
    {% for comment in comments %}
    <div class="post{% if comment.owner_of_comment %} admin_post{% endif %}">
      <span class="author {% if not comment.author_profile %}ano{% endif %}">{% if comment.author_profile %}<a href="{{ comment.author_profile.link }}"><img src="{{ comment.author_profile.avatars[16] }}" style="display:inline">&nbsp;{{ comment.author_profile.name }}</a>{% else %}{{ comment.nick }}{% endif %}</span>
      {% if comment.is_hidden %}
      {{ ctx.data.translations.t_hidden_comment }}
      {% else %}
      {{ comment.message|raw }}
      {% endif %} 
    {% spaceless %}
      <div class="meta" ><span name="pubdate">{{comment.date}}</span>
        {% if not ( ctx.data.entry.in_comments_thread and comment.childs and (comment.childs_count > 0) ) and not comment.owner_of_comment%}
        {% if ctx.data.visitor_is_admin and not comment.childs %}
        <a href="{{ comment.reply_link }}" class="xt_blog_comment_action_reply">{{ ctx.data.translations.t_reply }}</a>
        {% endif %}
        {% if ctx.data.is_logged_in and not comment.is_hidden and not data.comments_form.is_maintenance %}
        {% if ctx.data.reported_spam and ctx.data.reported_spam == comment.id %}
        {{ctx.data.translations.t_reported}}
        {% else %}
        <a href="{{ comment.spam_link }}">{{ ctx.data.translations.t_report_spam }}</a>
        {% endif %}
        {% elseif not ctx.data.is_logged_in and not comment.is_hidden and not data.comments_form.is_maintenance %}
        <a href="{{ comment.login_link }}">{{ ctx.data.translations.t_report_spam }}</a>
        {% endif %}
        {% endif %}
      </div>
    {% if comment.childs %}
      {{ _self.comments_list(comment.childs, ctx) }}
    {% endif %}
    </div>{% endspaceless %}
    {% endfor %}
    {% endmacro %}
    <div class="thread">
      {{ _self.comments_list(data.comments, _context) }}
    </div>
    {{ data.comments_pagination_html|raw }}
    {% endblock %}
    
    {% block guestbook_comments_form %}
    {% if data.comments_form.is_maintenance %}
    {{ data.comments_form.maintenante_markup|raw }}
    {% elseif data.is_logged_in and not data.community_name %}
    <div class="breadcrumbs compo">
      <b>{{ data.translations.t_provide_community_name }}</b>
    </div>
    <form action="{{ data.community_name_submit_url }}" method="post" class="post new">
      {% if data.in_comments_thread %}
      <input type="hidden" name="__xtcomments_thread_id" value="{{ data.in_comments_thread }}" />
      {% endif %}
      <input class="input" type="text" name="community_name" value="{{data.community_name_tmp}}" />
      <input class="submit" type="submit" value="{{ data.translations.t_submit }}" />
    </form>
    {% else %}
    <div class="breadcrumbs compo">
      <b>{{ data.translations.t_post_a_comment }}</b>
    </div>
    {% if data.comments_form.comments_restriction == "logged_in" and not data.is_logged_in %}
    <div class="breadcrumbs compo">
      <b>{{ data.translations.t_comments_login_required }}:</b>
      <a href="{{ data.comments_form.login_link }}"> {{ data.translations.t_login }} </a> 
      <a href="{{ data.comments_form.signup_link }}"> {{ data.translations.t_signup }} </a>
    </div>
    {% elseif data.comments_form.comments_restriction == "logged_in" and data.is_logged_in and not data.is_confirmed_email %}
    <div class="breadcrumbs compo">
      <a href="{{ data.confirm_email_link }}?redir={{ data.comments_form.url_encoded }}"> {{ data.translations.t_confirm_email }}</a>
    </div>
    {% else %}
    <form action="{{ data.comments_form.url }}" method="post" class="post new">
      {% if data.entry.in_comments_thread %}
      <input type="hidden" name="__xtcomments_thread_id" value="{{ data.entry.in_comments_thread }}" />
      {% endif %}
      <input type="hidden" name="__xtx" value="{{ data.comments_form.fields.xtx }}" />
      <input type="hidden" name="__xtxs" value="{{ data.comments_form.fields.xtxs }}" />
      <input type="text" name="__xtcomments_comments" value="" style="display:none" />
      {% if data.is_logged_in %}
      <label>{{ data.translations.t_posting_as }}: 
        <span class="author">{{ data.community_name }}</span>
      </label>
      {% else %}
      {% if data.comments_form.spam_comments_reserved_name %}
      <div class="breadcrumbs compo">{{ data.translations.t_reserved_name }}</div>
      {% endif %}
      {% if data.comments_form.poster %}
      <input type="hidden" name="__xtcomments_nick" value="{{ data.comments_form.poster }}" />
      {% else %}
      <label>{{ data.translations.t_name }} 
      <input class="input" type="text" name="__xtcomments_nick" value="{{ data.comments_form.spam_comments_nick }}" />
      </label>
      {% endif %}
      {% endif %}
      {% if data.comments_form.comments_restriction == "email_required" and not data.is_logged_in %}
      <label>{{ data.translations.t_email }} 
      <input class="input" type="email" name="__xtcomments_email" value="{{ data.comments_form.spam_comments_email }}" />
      </label>
      {% endif %}
      <label>{{ data.translations.t_comment }}
      <textarea name="__xtcomments_msg">{{ data.comments_form.spam_comments_msg }}</textarea>
      </label>
      {% if data.comments_form.spam_comments %}
      <div class="breadcrumbs compo">
        <b>Please enter the text you can see on the image below:</b>
      </div>
      <input type="hidden" name="hash" value="{{ data.comments_form.spam_hash }}" />
      <img src="{{ data.comments_form.spam_captcha_url }}" alt="" /><br />
      <label>Code: 
      <input class="input" type="text" name="code" />
      </label>
      {% endif %}
      <input class="submit" name="submit" type="submit" value="{{ data.translations.t_post }}" />
    </form>
    {% endif %}
    {% endif %}
    {% endblock %}
  </div>
</div>

code này là code gốc của xtgem, trong đó mình có chỉnh sửa lại 1 tí về html cho phù hợp với javascript..
Xong rồi.. vào file guestbook và nhận thành quả!!
2 người thích điều này!
* Out
- Cấp bậc: mod
03-04-2015
Maj test
* Admin
- Cấp bậc: admin
03-04-2015
* Godlike format="r" à.

loa nếu ko thjcj dùng js thj format="ago"

hj
* Godlike
- Cấp bậc: mem
03-04-2015
http://syntax.xtgem.com/manual/xtgem/pages/time_and_date
▲ Tổng số: 4
- Share:
BBCode:

Link:
Online: Guests: 1

Từ khóa: dụng , làm , thời , Hướng , sử