简体   繁体   中英

show time and date with meteor using template helpers

I'm currently learning the meteor framework, and right now I can't quite understand why my code isn't working. I'm attempting to create a template called "time" that has a variable called "date" which uses new Date(); to display the date and time on my HTML file, but it isn't working. All it shows is "the time now is" without showing the time.

Here's my HTML and JS file (I tried to make it following the same logic for the first template images which my course uses):

HTML:

<head>
  <title>my_first_app</title>
</head>
<body>
  <h1>Hello from Greece!</h1>
  {{>time}}
</body>

<template name="time">
  <p>The time now is {{date}}</p>
</template>

Javascript:

import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var';
import './main.html';

var date = new Date();

Template.time.helpers({
  time: function(){
    return new Date();
  }
});

You just need to change your helper name in the template to 'time' instead of 'date' or to reduce ambiguity you can do like this:

<head>
  <title>my_first_app</title>
</head>
<body>
<h1>Hello from Greece!</h1>
{{>time}}
</body>
<template name="time">
  <p>The time now is {{timeVal}}</p>
import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var';

import './main.html';

var date = new Date();


Template.time.helpers({


timeVal: function(){ return new Date(); }});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM